1

I have a xml file that looks like this:

            <item>
                  <title>Lore ipsum etc </title>
                  <description>lorem ipsum etc etc</description>
                  <link>http://url.com</link>
                  <guid isPermaLink="false">http://url.com</guid>
                  <pubDate>Sat, 27 Apr 2013 14:56:50 GMT</pubDate>
                  <media:thumbnail width="66" height="49" url="http://url.com/media/images/67275000/jpg/_67275945_017846423.jpg" />
          <media:thumbnail width="144" height="81" url="http://http://url.com/media/images/67276000/jpg/_67276200_017846423.jpg" />
           </item>
        <item>
              <title>Lore ipsum etc </title>
              <description>lorem ipsum etc etc</description>
              <link>http://url.com</link>
              <guid isPermaLink="false">http://url.com</guid>
              <pubDate>Sat, 28 Apr 2013 14:56:50 GMT</pubDate>
 <media:thumbnail width="66" height="49" url="http://url.com/media/images/67275000/jpg/_67275945_017846423.jpg" />
      <media:thumbnail width="144" height="81" url="http://http://url.com/media/images/67276000/jpg/_67276200_017846423.jpg" />
       </item>

the list have around 50 items.

How can I give my variables the value that is inside <title>, <description>, <link> and <PubDate>.

I have done this so far:

url = "http://url.com/file.xml";
XmlDocument doc = new XmlDocument();
doc.Load(url);

Any kind of help is appreciated

Obsivus
  • 8,231
  • 13
  • 52
  • 97

3 Answers3

0

Linq to XML (http://msdn.microsoft.com/en-us/library/bb387098.aspx) is a good start.

And some other examples:

How does one parse XML files?

Reading data from XML

Parse XML document in C#

Community
  • 1
  • 1
Kai
  • 1,953
  • 2
  • 13
  • 18
0

It seems you try to parse an RSS feed. you can use SyndicationFeed for this.

But anyway, it is a simple xml and you can use Linq To Xml

var xdoc = XDocument.Load(url);
var items = xdoc.Descendants("item")
            .Select(item => new
            {
                Title = item.Element("title").Value,
                description = item.Element("description").Value,
                Link = item.Element("link").Value
            })
            .ToList();

PS: You haven't posted the complete xml, (for ex, no root element) you may still have issues with Xml namespaces.

I4V
  • 34,891
  • 6
  • 67
  • 79
  • Inside the I have How can I fill the img url this one also? – Obsivus Apr 27 '13 at 22:01
  • @Obsivus As i said in the answer `you may still have issues with Xml namespaces`. Without seeing your full xml, It is not possible to give an answer. – I4V Apr 27 '13 at 22:02
  • I tried the syndicationfeed it worked very good no issues at all but there is no property for images – Obsivus Apr 27 '13 at 22:02
  • @Obsivus Because they are provider-dependent properties. You may need to use Linq To Xml to get these properties. – I4V Apr 27 '13 at 22:04
  • I see, I tried the linq to xml it worked also but I cant seem to get the second thumbnail media image to get filled :( Do you know how I can do that? – Obsivus Apr 27 '13 at 22:05
  • I updated my code you can see it on the question, I want the last thumbail image url not the first one – Obsivus Apr 27 '13 at 22:06
  • You should use `XNameSpace`. the part of xml you posted is not enough. There must be a declaration for `media` somewhere at the beginning of your xml. – I4V Apr 27 '13 at 22:07
  • @Obsivus I am trying to give an answer with limited informations you posts. But, sorry, I am not willing to continue this discussion. – I4V Apr 27 '13 at 22:12
0

To deserialize xml try this approach: http://undefinedvalue.com/2011/11/22/deserializing-objects-xml-c

Adwyr
  • 16
  • 6