C# parse XML File

11,671

Solution 1

You could use XPath expression for that. Below is complete example on console-appliactaion - as you use xlmns namespace, it requries a little modification of ParseXML method.

using System;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load("XMLFile1.xml");
            XmlNamespaceManager xmlnm = new XmlNamespaceManager(xmlDocument.NameTable);
            xmlnm.AddNamespace("ns", "http://www.w3.org/2005/Atom");

            ParseXML(xmlDocument, xmlnm);

            Console.WriteLine("\n---XML parsed---");
            Console.ReadKey();
        }

        public static void ParseXML(XmlDocument xmlFile, XmlNamespaceManager xmlnm)
        {
            XmlNodeList nodes = xmlFile.SelectNodes("//ns:updated | //ns:expires | //ns:title | //ns:summary | //ns:state", xmlnm);

            foreach (XmlNode node in nodes)
            {
                Console.WriteLine(node.Name + " = " + node.InnerXml);
            }
        }
    }
}

// in XPath expression means, you want to select all nodes with specific name, no matter where they are.

If you want to search only <entry></entry> elements, you can use following:
"//ns:entry/ns:updated | //ns:entry/ns:expires | //ns:entry/ns:title | //ns:entry/ns:summary | //ns:entry/ns:state"

Solution 2

I believe the easiest way to parse the XML directly is using LINQ-TO-XML. You can find more info here.

Share:
11,671
user1011394
Author by

user1011394

Updated on June 04, 2022

Comments

  • user1011394
    user1011394 almost 2 years

    I've a problem to parse my XML File (RSS Feed) in C#. I just want to read out the "entry" entries (the root parent - "feed" - is not relevant). All "entry" entries are almost even, except the part "state". Some entries doesn't have that entry.

    So i just want to read out the following: "entry" nodes:

    1. updated
    2. expires
    3. title
    4. summary
    5. state (if exists)

    Any suggestions? Thank you very much.

    <?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
       <updated>2011-01-01T00:00:00+0100</updated>
       <link href="http://www.domain.com" rel="self"/>
       <author>
          <name>Mr X</name>
          <email>[email protected]</email>
       </author>
       <title>Some infos....</title>
       <id>domain.com</id>
    <entry>
       <updated>2011-01-01T00:00:00Z</updated>
       <expires>2011-01-02T00:00:00Z</expires>
       <title>My first Title</title>
       <id>First ID</id>
      <link type="text/html" rel="alternate"
            href="http://domain.com/firstElement"></link>
       <summary>My first important summary</summary>
       <rights>domain.com</rights>
       <content type="xhtml">
          <div xmlns="http://www.w3.org/1999/xhtml">
             <div>
                <img alt="second" width="32"
                     src="http://domain.com/firstElement.png"/>
             </div>
          </div>
       </content>
    </entry>
    <entry>
      <updated>2011-01-01T00:00:00Z</updated>
      <expires>2011-01-02T00:00:00Z</expires>
      <title>My second Title</title>
      <state>active</state>
      <id>Second ID</id>
      <link type="text/html" rel="alternate"
            href="http://domain.com/secondElement"></link>
      <summary>My second important summary</summary>
      <rights>domain.com</rights>
      <content type="xhtml">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div>
            <img alt="second" width="32"
                  src="http://domain.com/secondElement.png"/>
          </div>
        </div>
      </content>
      </entry>
    </feed>{<?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
       <updated>2011-01-01T00:00:00+0100</updated>
       <link href="http://www.domain.com" rel="self"/>
       <author>
          <name>Mr X</name>
          <email>[email protected]</email>
       </author>
       <title>Some infos....</title>
       <id>domain.com</id>
    <entry>
       <updated>2011-01-01T00:00:00Z</updated>
       <expires>2011-01-02T00:00:00Z</expires>
       <title>My first Title</title>
       <id>First ID</id>
      <link type="text/html" rel="alternate"
            href="http://domain.com/firstElement"></link>
       <summary>My first important summary</summary>
       <rights>domain.com</rights>
       <content type="xhtml">
          <div xmlns="http://www.w3.org/1999/xhtml">
             <div>
                <img alt="second" width="32"
                     src="http://domain.com/firstElement.png"/>
             </div>
          </div>
       </content>
    </entry>
    <entry>
      <updated>2011-01-01T00:00:00Z</updated>
      <expires>2011-01-02T00:00:00Z</expires>
      <title>My second Title</title>
      <state>active</state>
      <id>Second ID</id>
      <link type="text/html" rel="alternate"
            href="http://domain.com/secondElement"></link>
      <summary>My second important summary</summary>
      <rights>domain.com</rights>
      <content type="xhtml">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div>
            <img alt="second" width="32"
                  src="http://domain.com/secondElement.png"/>
          </div>
        </div>
      </content>
      </entry>
    </feed>
    

    My current C# code:

    public void ParseXML(XmlDocument xmlFile)
        {
            ArrayList updated = new ArrayList();
            ArrayList expires = new ArrayList();
            ArrayList title = new ArrayList();
            ArrayList summary = new ArrayList();
            ArrayList state = new ArrayList();
    
            ObservableCollection<TrafficInformation> trafInfo = new ObservableCollection<TrafficInformation>();
            myCollection = trafInfo;
            XmlNodeReader reader = new XmlNodeReader(xmlFile);
    
            StringBuilder output = new StringBuilder();
    
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if(reader.Name == "updated")
                        {
                            updated.Add(reader.ReadString());
                        }
    
                        if (reader.Name == "expires")
                        {
                            expires.Add(reader.ReadString());
                        }
    
                        if (reader.Name == "title")
                        {
                            title.Add(reader.ReadString());
                        }
    
                        if (reader.Name == "summary")
                        {
                            summary.Add(reader.ReadString());
                        }
    
                        if (reader.Name == "state")
                        {
                            state.Add(reader.ReadString());
                        }
    
                        break;
                }
            }
        }
    

    In that case, I don't have a relationship between the data (if state doesn't exists).

    • Oded
      Oded over 12 years
      What have you tried? Where are you having difficulties? What version of .NET are you using?
    • user1011394
      user1011394 over 12 years
      I'm using .net 4.0. How can I post a formatted code segment in a comment?
    • Oded
      Oded over 12 years
      Don't post formatted code in comments - instead, edit your question and add detail to it.
    • Oded
      Oded over 12 years
      Please don't use ArrayList - use List<string> instead. That way, you at least have type safety.
    • user1011394
      user1011394 over 12 years
      Mh ok, I'm going to remember this^^.
  • rdans
    rdans about 10 years
    I found this more useful for getting started with linq to xml: dotnetcurry.com/showarticle.aspx?ID=564