8

I would like to create the XML string with special characters handling. However it turned out to be too complicated and causing issues by generating wrong XML. Now i was thinking to build the string using some object from System.xml and then stringify() or get string from it. This will i guess help me from special character cases.

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

doc.getXMLString();

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

2 Answers 2

32

I find XmlTextWriter more intuitive than XmlDocument for editing.

e.g.:

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 INFO:

using 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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just for additional information i would like to know how it can be done using the XmlDocument
8

You can also refer to this community wiki question, which leads to easier-to-read syntax when you need to build an xml stream programatically.

You can then just call the .ToString() method to get a clean escaped representation of your XML stream.

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

And you would get in xmlString:

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

1 Comment

This syntax is only available starting from .Net 3.5+, as you need to reference System.Xml.Linq which was introduced back then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.