xmltextwriter

Memory stream is empty

余生长醉 提交于 2019-12-01 09:34:06
I need to generate a huge xml file from different sources (functions). I decide to use XmlTextWriter since it uses less memory than XmlDocument . First, initiate an XmlWriter with underlying MemoryStream MemoryStream ms = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding(false, false)); xmlWriter.Formatting = Formatting.Indented; Then I pass the XmlWriter (note xml writer is kept open until the very end) to a function to generate the beginning of the XML file: xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement(); // xmlWriter.WriteEndElement(); // Do

Memory stream is empty

风格不统一 提交于 2019-12-01 05:15:23
问题 I need to generate a huge xml file from different sources (functions). I decide to use XmlTextWriter since it uses less memory than XmlDocument . First, initiate an XmlWriter with underlying MemoryStream MemoryStream ms = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding(false, false)); xmlWriter.Formatting = Formatting.Indented; Then I pass the XmlWriter (note xml writer is kept open until the very end) to a function to generate the beginning of the XML

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

xdocument save preserve white space inside tags

╄→гoц情女王★ 提交于 2019-12-01 01:44:29
问题 I am using XDocument in LINQ to edit (insert) and save xml document. XDocument doc = XDocument.Load("c:\\sample.xml", LoadOptions.PreserveWhitespace); doc.Save("c:\\sample.xml",SaveOptions.DisableFormatting) sample.xml before doc.Save : <ELEMENT ATTRIB1="attrib1" ATTRIB2="attrib2" > value </ELEMENT> sample.xml after doc.Save <ELEMENT ATTRIB1="attrib1" ATTRIB2="attrib2"> value </ELEMENT> As you can see, there is double space after ATTRIB1 and a single space after ATTRIB2 in the original

INSERT XML into SQL Server 2008 database

拈花ヽ惹草 提交于 2019-11-29 22:59:16
Hello I'm trying to insert some XML data into a table on SQL Server 2008. However I keep getting thrown this error; XML parsing: line 1, character 39, unable to switch the encoding The database column filemeta uses the XML datatype, and I've switch the encoding to UTF-16 which I believe is necessary for adding XML data. INSERT INTO testfiles (filename, filemeta) VALUES ('test.mp3', '<?xml version="1.0" encoding="utf-16" standalone="yes"?><!--This is a test XML file--><filemeta filetype="Audio"><Comments /><AlbumTitle /><TrackNumber /><ArtistName /><Year /><Genre /><TrackTitle /></filemeta>');

INSERT XML into SQL Server 2008 database

拈花ヽ惹草 提交于 2019-11-28 19:56:59
问题 Hello I'm trying to insert some XML data into a table on SQL Server 2008. However I keep getting thrown this error; XML parsing: line 1, character 39, unable to switch the encoding The database column filemeta uses the XML datatype, and I've switch the encoding to UTF-16 which I believe is necessary for adding XML data. INSERT INTO testfiles (filename, filemeta) VALUES ('test.mp3', '<?xml version="1.0" encoding="utf-16" standalone="yes"?><!--This is a test XML file--><filemeta filetype="Audio

C#: XmlTextWriter.WriteElementString fails on empty strings?

冷暖自知 提交于 2019-11-28 00:02:45
I'm using XmlTextWriter and its WriteElementString method, for example: XmlTextWriter writer = new XmlTextWriter("filename.xml", null); writer.WriteStartElement("User"); writer.WriteElementString("Username", inputUserName); writer.WriteElementString("Email", inputEmail); writer.WriteEndElement(); writer.Close(); The expected XML output is: <User> <Username>value</Username> <Email>value</Email> </User> However, if for example inputEmail is empty, the result XML I get as as follows: <User> <Username>value</Username> <Email/> </User> Whereas I would expect it to be: <User> <Username>value<

Modify XML existing content in C#

放肆的年华 提交于 2019-11-27 08:03:33
Purpose: I plan to Create a XML file with XmlTextWriter and Modify/Update some Existing Content with XmlNode SelectSingleNode(), node.ChildNode[?].InnerText = someting, etc. After I created the XML file with XmlTextWriter as below. XmlTextWriter textWriter = new XmlTextWriter("D:\\learning\\cs\\myTest.xml", System.Text.Encoding.UTF8); I practiced the code below. But failed to save my XML file. XmlDocument doc = new XmlDocument(); doc.Load("D:\\learning\\cs\\myTest.xml"); XmlNode root = doc.DocumentElement; XmlNode myNode; myNode= root.SelectSingleNode("descendant::books"); .... textWriter

C#: XmlTextWriter.WriteElementString fails on empty strings?

最后都变了- 提交于 2019-11-26 21:39:46
问题 I'm using XmlTextWriter and its WriteElementString method, for example: XmlTextWriter writer = new XmlTextWriter("filename.xml", null); writer.WriteStartElement("User"); writer.WriteElementString("Username", inputUserName); writer.WriteElementString("Email", inputEmail); writer.WriteEndElement(); writer.Close(); The expected XML output is: <User> <Username>value</Username> <Email>value</Email> </User> However, if for example inputEmail is empty, the result XML I get as as follows: <User>

Modify XML existing content in C#

二次信任 提交于 2019-11-26 11:09:44
问题 Purpose: I plan to Create a XML file with XmlTextWriter and Modify/Update some Existing Content with XmlNode SelectSingleNode(), node.ChildNode[?].InnerText = someting, etc. After I created the XML file with XmlTextWriter as below. XmlTextWriter textWriter = new XmlTextWriter(\"D:\\\\learning\\\\cs\\\\myTest.xml\", System.Text.Encoding.UTF8); I practiced the code below. But failed to save my XML file. XmlDocument doc = new XmlDocument(); doc.Load(\"D:\\\\learning\\\\cs\\\\myTest.xml\");