I have assigned a simple xml into a XmlReader. A switch case loops
through the nodes and writes the data to XmlWriter object which is
initialized to a xml file. The output xml file is correctly generated
and everything works fine. But if the input xml has an & then I am
unable to generate a valid output xml file.
I have pasted the code below:
public class Class1
{
public Class1()
{
}
public static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(@"<FName>First &
Name</FName>");
System.IO.StringReader sr = new
System.IO.StringReader(sb.ToString());
System.Xml.XmlTextReader xtr = new
System.Xml.XmlTextReader(sr);
System.Xml.XmlReader xr = xtr;
System.Xml.XmlTextWriter xtw = new
System.Xml.XmlTextWriter("Output.xml", new System.Text.UTF8Encoding());
System.Xml.XmlWriter xw = xtw;
while (xr.Read())
{
switch (xr.NodeType)
{
case System.Xml.XmlNodeType.Element:
xw.WriteStartElement(xr.LocalName);
break;
case System.Xml.XmlNodeType.EndElement:
xw.WriteEndElement();
break;
case System.Xml.XmlNodeType.Text:
xw.WriteRaw(xr.Value);
break;
default: break;
}
}
xr.Close();
xw.Flush();
xw.Close();
}
}
Please help.
Thanks,
Amit