Hi - I am trying to read an XML file using XmlTextReader and write it
out using XmlTextWriter.
The XML is XHTML. At the moment, I am trying to create a straightforward
copy of a file. The XHTML file contains nodes and attributes (with there
own namespace) that will later be used to alter the XML into a desired
output. This 'copying' is the first stage to then processing the XML to
the desired output
I have included the code below. If I put debug stops on in the
appropriate place, the reader is correctly and moving through the XML.
However, the XML writing is incorrect. It seems as though if an xml
element contains an empty element - e.g. <n1><nd/></n1>, the </n1>
appears at the end of all the following elements in the XML document.
I am little confused as to which methods of write that I should be using
when I read elements from the read. I reasonably understand using the
writer to create 'my own xml', it's in this example when I require to
write what I read.
Many thanks for any help
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
String name = reader.Name;
NameValueCollection attributes = new NameValueCollection();
while (reader.MoveToNextAttribute())
{
String attributeName = reader.Name;
String attributeValue = reader.Value;
attributes.Add(attributeName, attributeValue);
}
writer.WriteStartElement(name);
foreach (String attributeName in attributes)
{
writer.WriteAttributeString(attributeName, attributes[attributeName]);
}
break;
case XmlNodeType.EndElement:
writer.WriteEndElement();
break;
case XmlNodeType.Whitespace:
writer.WriteWhitespace(reader.Value);
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
case XmlNodeType.DocumentType:
break;
default:
break;
}
}