Please see my comments to the question. Tags themselves cannot be "invalid", your XML could be not well-formed, or well-formed XML might not comply with the schema, but this is a different issue. You did not explain your problem with tags made by yourself, but normally, you should make your code valid instead of looking for some compromise.
Nevertheless, making a text out of an XML file is way too easy, but it comes at the cost of lost some structural information. The simplest way is using the class
System.Xml.XmlReader
to read XML and the class
System.IO.StreamWriter
to write text:
http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[
^].
If could be something like this:
string XmlAttributeToText(int depth, string attributeName, string value) {
return
}
string XmlNodeToText(int depth, System.Xml.XmlNodeType nodeType, string name, string value) {
return
}
string textFileName =
string xmlFileName =
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(textFileName, false, System.Text.Encoding.UTF8)) {
using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(xmlFileName)) {
while (reader.Read()) {
string nodeContent = XmlNodeToText(reader.Depth, reader.NodeType, reader.Name, reader.Value);
writer.WriteLine(nodeContent);
if (reader.HasAttributes) {
reader.MoveToNextAttribute();
string attributeContent = XmlAttributeToText(reader.Depth, reader.Name, reader.Value);
writer.WriteLine(attributeContent);
}
}
}
}
Also you can use XML reader options to ignore comment, processing instructions; you can ignore node types you are not interested in the loop shown above; in other words — program the mapping rules for mapping from XML to text the ways suitable for your goal.
—SA