3

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

5
  • 3
    What have you tried? Where are you having difficulties? What version of .NET are you using? Commented Nov 19, 2011 at 12:51
  • I'm using .net 4.0. How can I post a formatted code segment in a comment? Commented Nov 19, 2011 at 12:54
  • Don't post formatted code in comments - instead, edit your question and add detail to it. Commented Nov 19, 2011 at 12:55
  • 2
    Please don't use ArrayList - use List<string> instead. That way, you at least have type safety. Commented Nov 19, 2011 at 13:00
  • Mh ok, I'm going to remember this^^. Commented Nov 19, 2011 at 13:03

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

I found this more useful for getting started with linq to xml: dotnetcurry.com/showarticle.aspx?ID=564
2

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.