xmlwriter

PHP XMLReader read , edit Node , write XMLWriter

徘徊边缘 提交于 2019-12-01 04:13:08
问题 I have an XML file which is very very large (millions of records). Due to speed and memory constraints I plan to use XMLReader / XMLWriter . I need to read the file, getting one record, change its attribute, and finally save XML again. For testing I created an XML file and write some records into it using these lines: $doc = new XMLWriter(); $doc->openURI($xmlFile); $doc->startDocument('1.0','UTF-8'); $doc->setIndent(4); $doc->startElement('DBOS'); for($r=0;$r<10; $r++){ $doc->startElement(

How do I set the Settings property in XmlTextWriter, so that I can write each XML attribute on its own line?

醉酒当歌 提交于 2019-12-01 02:32:16
I have this bit of code, which serializes an object to a file. I'm trying to get each XML attribute to output on a separate line. The code looks like this: public static void ToXMLFile(Object obj, string filePath) { XmlSerializer serializer = new XmlSerializer(obj.GetType()); XmlWriterSettings settings = new XmlWriterSettings(); settings.NewLineOnAttributes = true; XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8); writer.Settings = settings; // Fails here. Property is read only. using (Stream baseStream = writer.BaseStream) { serializer.Serialize(writer, obj); } } The only

XMLWriter: WriteStartElement with a tag name and string to indicate tag name

。_饼干妹妹 提交于 2019-12-01 00:00:37
I have same tag names and different strings to different the tag name. here is the XML. <order> <ID>1001</ID> <config> <properties> <entry key="Total">10</entry> <entry key="Name">name</entry> <entry key="Config">COMMON</entry> <entry key="Delivery">15-FEBRUARY-2013</entry> <entry key="Setting">name</entry> </properties> <id>19</id> </config> <aID>58239346</aID> </order> here is my current code: public String cards(string id) { StringWriter str = new StringWriter(); XmlTextWriter xmlWriter = new XmlTextWriter(str); xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartDocument();

XMLWriter vs XMLDictionaryWriter

六月ゝ 毕业季﹏ 提交于 2019-11-30 17:23:07
What's the difference between XMLWriter and XMLDictionaryWriter ? In which cases is each one generally used? XmlWriter is an abstract class of which XmlDictionaryWriter is one of the classes that inherits from it and is itself an abstract class. I am taking a stab in the dark that you want to use it with the DataContractSerializer or with de/serialization in general. The XmlDictionaryWriter is the base class used by WCF to do its de/serialization. From that I would deduce that there must be some performance tuning in the XmlDictionaryWriter to make it more performant with WCF de/serialization

how to create an xml using xml writer without declaration element

隐身守侯 提交于 2019-11-30 00:30:11
问题 I am using XmlWriter.Create() to get a writer instance then write the XML, but the result has the <?xml version="1.0" encoding="utf-16" ?> , how do I tell my xml writer do not produce it? 回答1: Use XmlWriterSettings.OmitXmlDeclaration. Don't forget to set XmlWriterSettings.ConformanceLevel to ConformanceLevel.Fragment . 回答2: You can subclass XmlTextWriter and override the WriteStartDocument() method to do nothing: public class XmlFragmentWriter : XmlTextWriter { // Add whichever constructor(s)

Why is the XmlWriter always outputting utf-16 encoding?

江枫思渺然 提交于 2019-11-29 13:07:37
I have this extension method public static string SerializeObject<T>(this T value) { var serializer = new XmlSerializer(typeof(T)); var settings = new XmlWriterSettings { Encoding = new UTF8Encoding(true), Indent = false, OmitXmlDeclaration = false, NewLineHandling = NewLineHandling.None }; using(var stringWriter = new StringWriter()) { using(var xmlWriter = XmlWriter.Create(stringWriter, settings)) { serializer.Serialize(xmlWriter, value); } return stringWriter.ToString(); } } but whenever I call this it has an encoding of utf-16 specified, ie <?xml version="1.0" encoding="utf-16"?> . What am

Removing version from xml file

纵然是瞬间 提交于 2019-11-29 09:26:47
I am creating a Xml like format using XmlWriter . But in the output there is version information also. <?xml version="1.0" encoding="utf-8"?> I don't need this in my file. How can I do that? Is there any way to remove it by code? Use the ConformanceLevel and OmitXmlDeclaration properties. Example: XmlWriter w; w.Settings = new XmlWriterSettings(); w.Settings.ConformanceLevel = ConformanceLevel.Fragment; w.Settings.OmitXmlDeclaration = true; When creating your XmlWriter, pass through the settings you want using XmlWriterSettings: XmlWriterSettings settings = new XmlWriterSettings(); settings

Serialize Entity Framework object with children to XML file

六眼飞鱼酱① 提交于 2019-11-29 02:30:59
I'm querying data with parent/child result sets using Entity Framework and I want to export this data to an XML document. var agreement = storeops.Agreements.SingleOrDefault(a => a.AgreementNumber == AgreementTextBox.Text); XmlSerializer serializer = new XmlSerializer(agreement.GetType()); XmlWriter writer = XmlWriter.Create("Agreement.xml"); serializer.Serialize(writer, agreement); This works well except it only serializes the parent without including the related child records in the XML. How can I get the children to serialize as well? I also tried using POCO generated code and the child

Replacing the innertext of an Xml node/element

痞子三分冷 提交于 2019-11-28 13:27:22
First of all this is C#. I am creating a internet dashboard for a small group of colleages in the NHS. Below is an example xml file in which I need to change the innertext of. I need to replace a specific element for example "Workshop1." Because we have a few workshops I cannot afford to use a general writer because it will replace all the information on the XML document with this one bit of code below. <?xml version="1.0" ?> <buttons> <workshop1>hello</workshop1> <url1>www.google.co.uk</url1> I am using a switch case to select a specific workshop where you can change the name and add a URL of

How to create an XML file from a XmlReader?

耗尽温柔 提交于 2019-11-28 12:07:13
How do you write an XML file from an System.Xml.XmlReader? I thought this would be a simple question but whenever I search I seem to be ending up with reading the file to a reader or writing node by node. The XmlReader object conveys xml that was stored in a database and just needs to come out of the database to a file. Is there any easy way to do this? SqlCommand dataCmd = new SqlCommand(sqlText, Conn); System.Xml.XmlReader dataReader = null; dataCmd.CommandTimeout = 60000; Conn.Open(); dataReader = dataCmd.ExecuteXmlReader(); dataReader.Read(); You need to create an XmlWriter and call its