Click here to Skip to main content
12,252,465 members (58,211 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: XML
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 11-Jun-12 3:23am
Comments
SAKryukov 11-Jun-12 13:39pm
   
Why? And what's the problem. It seems to be too easy, with the loss of formatting information.
--SA
SAKryukov 11-Jun-12 13:40pm
   
XML tags are all "custom", there are no "existing" ones. What are you even talking about?
--SA

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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 //... 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
  Permalink  
Comments
losmac 11-Jun-12 17:45pm
   
Good one! +5!
SAKryukov 13-Jun-12 12:21pm
   
Thank you, Maciej.
--SA
VJ Reddy 13-Jun-12 11:05am
   
Good answer. 5!
SAKryukov 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 11 Jun 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100