How to create an XML document in memory and get a string from it

I would like to create an XML string with special character processing. However, it turned out that this is too complicated and causes problems, causing the wrong XML. Now I thought to build a string using some object from System.xml, and then stringify () or get a string from it. This, I think, will help me in special cases.

//Psudo code xmlDoc doc = new XMLDoc(); Element ele= new Element("xyz"); ele.value(Oob.property) doc.appendNode(ele); ... doc.getXMLString(); 

Can someone please let me know how to do this in C # .NET2.0 +.

+6
c # xml
source share
2 answers

I find XmlTextWriter more intuitive than XmlDocument for editing.

eg:.

 string xmlString = null; using(StringWriter sw = new StringWriter()) { XmlTextWriter writer = new XmlTextWriter(sw); writer.Formatting = Formatting.Indented; // if you want it indented writer.WriteStartDocument(); // <?xml version="1.0" encoding="utf-16"?> writer.WriteStartElement("TAG"); //<TAG> // <SUBTAG>value</SUBTAG> writer.WriteStartElement("SUBTAG"); writer.WriteString("value"); writer.WriteEndElement(); // <SUBTAG attr="hello">world</SUBTAG> writer.WriteStartElement("SUBTAG"); writer.WriteStartAttribute("attr"); writer.WriteString("hello"); writer.WriteEndAttribute(); writer.WriteString("world"); writer.WriteEndElement(); writer.WriteEndElement(); //</TAG> writer.WriteEndDocument(); xmlString = sw.ToString(); } 

after this code, xmlString will contain:

 <?xml version="1.0" encoding="utf-16"?> <TAG> <SUBTAG>value</SUBTAG> <SUBTAG attr="hello">world</SUBTAG> </TAG> 

ADDITIONAL INFORMATION:

using an XmlDocument would be:


 XmlDocument doc = new XmlDocument(); XmlNode tagNode = doc.CreateNode(XmlNodeType.Element, "TAG", null); doc.AppendChild(tagNode); XmlNode subTagNode1 = doc.CreateNode(XmlNodeType.Element, "SUBTAG", null); tagNode.AppendChild(subTagNode1); XmlText subTagNode1Value = doc.CreateTextNode("value"); subTagNode1.AppendChild(subTagNode1Value); XmlNode subTagNode2 = doc.CreateNode(XmlNodeType.Element, "SUBTAG", null); tagNode.AppendChild(subTagNode2); XmlAttribute subTagNode2Attribute = doc.CreateAttribute("attr"); subTagNode2Attribute.Value = "hello"; subTagNode2.Attributes.SetNamedItem(subTagNode2Attribute); XmlText subTagNode2Value = doc.CreateTextNode("world"); subTagNode2.AppendChild(subTagNode2Value); string xmlString = null; using(StringWriter wr = new StringWriter()) { doc.Save(wr); xmlString = wr.ToString(); } 
+28
source share

You can also refer to this wiki question community, which makes it easier to read the syntax when you need to programmatically create an XML stream.

Then you can simply call the .ToString () method to get a clean, shielded view of your XML stream.

 var xmlString = new XElement("Foo", new XAttribute("Bar", "some & value with special characters <>"), new XElement("Nested", "data")).ToString(); 

And you will get in xmlString:

 <Foo Bar="some &amp; value with special characters &lt;&gt;"> <Nested>data</Nested> </Foo> 
+6
source share

All Articles