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+ .
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();
}
                        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 & value with special characters <>">
  <Nested>data</Nested>
</Foo>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With