Click here to Skip to main content
15,745,794 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I would like to read the content of an XML file into whatever webpage or textfile, these XML files are the result of parsing them from EDI using a parser I made myself, however, the tags are not valid since I made them myself, so is it conventional to make new tags or should i be using existing tags? if yes, how can I read my new tags?

cheers,
Posted
Comments
Sergey Alexandrovich Kryukov 11-Jun-12 13:39pm    
Why? And what's the problem. It seems to be too easy, with the loss of formatting information.
--SA
Sergey Alexandrovich Kryukov 11-Jun-12 13:40pm    
XML tags are all "custom", there are no "existing" ones. What are you even talking about?
--SA

1 solution

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:
C#
string XmlAttributeToText(int depth, string attributeName, string value) {
        //...
        return //... whatever you want
} //XmlAttributeToText

string XmlNodeToText(int depth, System.Xml.XmlNodeType nodeType, string name, string value) {
    //...
    return //... whatever you want
} //XmlNodeToText

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); //or writer.Write, whatever is required
            if (reader.HasAttributes) {
                reader.MoveToNextAttribute();
                string attributeContent = XmlAttributeToText(reader.Depth, reader.Name, reader.Value);
                writer.WriteLine(attributeContent); //or writer.Write, whatever is required
            } //if
        } //loop
    } //using XmlReader // reader is disposed here
} //using StreamWriter // writer is disposed here, file buffer is flashed and file is closed, which is even more important


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
 
Share this answer
 
Comments
Maciej Los 11-Jun-12 17:45pm    
Good one! +5!
Sergey Alexandrovich Kryukov 13-Jun-12 12:21pm    
Thank you, Maciej.
--SA
VJ Reddy 13-Jun-12 11:05am    
Good answer. 5!
Sergey Alexandrovich Kryukov 13-Jun-12 12:21pm    
Thank you, VJ.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900