XML parsing in C#

Discussion in 'C#' started by shabbir, Sep 28, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

    Introduction



    The parser gives you a place to start to write an XML parser in C#. We have used the System.Xml.

    The code


    Code:
    public void ParseFile(string strPath)
    {
        XmlTextReader xmlReader = new XmlTextReader(strPath);
        int iTab = 0;
        // Read the line of the xml file
        while (xmlReader.Read())
        {
            switch (xmlReader.NodeType)
            {
                case XmlNodeType.Element:
                    Hashtable attributes = new Hashtable();
                    // We add the attributes to the hash tables
    
                    bool isEmptyElement = false;
                    
                    PrintWhiteSpace(iTab);
    
                    // Print the start of the element
                    Console.Write("<" + xmlReader.Name);
                    isEmptyElement = xmlReader.IsEmptyElement;
    
                    if (xmlReader.HasAttributes)
                    {
                        // If element has attributes
                        for (int i = 0; i < xmlReader.AttributeCount; i++)
                        {
                            xmlReader.MoveToAttribute(i);
                            attributes.Add(xmlReader.Name, xmlReader.Value);
                            Console.Write(" " + xmlReader.Name + "=" + xmlReader.Value);
                        }
                    }
    
                    // Prints the end of the element
                    if (isEmptyElement == true)
                    {
                        Console.WriteLine(" />");
                    }
                    else
                    {
                        Console.WriteLine(">");
                        iTab++;
                    }                        
                    break;
                case XmlNodeType.EndElement:
                    iTab--;
                    PrintWhiteSpace(iTab);
                    Console.WriteLine("</" + xmlReader.Name + ">");
                    break;
                case XmlNodeType.Text:
                    PrintWhiteSpace(iTab);
                    Console.WriteLine(xmlReader.Value);
                    break;
                default:
                    break;
            }
        }
    }
    
    I have included the comments and the code is pretty much self explanatory.

    Using the code


    Code:
     XMLParser xml = new XMLParser();
     xml.ParseFile(@"Welcome.xml");
     Console.Read();
    
     

    Attached Files:

  2. charbeltrad

    charbeltrad New Member

    Joined:
    May 11, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hey i have a problem solving this xml :
    <Arbre_Binaire>
    −<One ID="1">
    −<Two ID="2">
    <Four ID="4"/>
    <Five ID="5"/>
    </Two>
    −<Three ID="3">
    −<Six ID="6">
    <Eight ID="8"/>
    <Nine ID="9"/>
    </Six>
    <Seven ID="7"/>
    </Three>
    </One>
    </Arbre_Binaire>

    i want to read it from parents to childrens the output must be: One Two Three Four Five Six Seven Eight Nine
    and thx
     
  3. lovepc

    lovepc New Member

    Joined:
    Jan 5, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    How about using the xml data binding in C#?
    It will be easier to read and write...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice