user1011394
user1011394

Reputation: 1666

C# parse XML File

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).

Upvotes: 3

Views: 11800

Answers (2)

mj82
mj82

Reputation: 5263

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"

Upvotes: 2

Alex Shtoff
Alex Shtoff

Reputation: 2640

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

Upvotes: 8

Related Questions