Solved

Parsing an XML File

Posted on 2011-09-20
6
450 Views
Last Modified: 2013-11-18
I need to get currency exchange rates from an RSS feed. I am having trouble (1) parsing the nodes and (2) getting the currency value.

I have successfully saved the xml file. The full file is attached :

The code section I am stuck with is:

 XmlNamespaceManager man = new XmlNamespaceManager(responseXML.NameTable);
                man.AddNamespace("rdf", "http://www.w3.org/1999/XSL/Transform");

                XmlNodeList xnList = responseXML.SelectNodes("rdf:RDF/item", man);
         
                foreach (XmlNode xn in xnList)
                {
                    if (xn["targetCurrency"].InnerText != "EUR")
                               //Get the value of the item :node"
                }

The xnList.Count = 0 which may indicate selectNodes list is not correct.

Please help

J
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  <channel rdf:about="http://xurrency.com/aud/feed">
    <title>Xurrency.com | AUD</title>
    <link>http://xurrency.com/aud/feed</link>
    <description>Australian Dollar</description>
    <dc:language>en-us</dc:language>
    <dc:rights>Creative Commons</dc:rights>
    <dc:date>2011-09-20T17:06:43Z</dc:date>
    <dc:creator>Xurrency.com</dc:creator>
    <dc:subject>Currency Values</dc:subject>
  </channel>
  <item rdf:about="http://xurrency.com/eur">
    <title xml:lang="en"><![CDATA[1 AUD = 0.7516 EUR]]></title>
    <link>http://xurrency.com/eur</link>
    <description xml:lang="en"><![CDATA[1 AUD = 0.7516 EUR]]></description>
    <dc:date>Tue Sep 20 18:03:00 UTC 2011</dc:date>
    <dc:language>en</dc:language>
    <dc:creator>Xurrency.com</dc:creator>
    <dc:simpleTitle>1 AUD = 0.7516 EUR</dc:simpleTitle>
    <dc:baseCurrency>AUD</dc:baseCurrency>
    <dc:targetCurrency>EUR</dc:targetCurrency>
    <dc:value frequency="daily" decimal="4">0.7516</dc:value>
  </item>
  <item rdf:about="http://xurrency.com/usd">
    <title xml:lang="en"><![CDATA[1 AUD = 1.0279 USD]]></title>
    <link>http://xurrency.com/usd</link>
    <description xml:lang="en"><![CDATA[1 AUD = 1.0279 USD]]></description>
    <dc:date>Tue Sep 20 18:03:00 UTC 2011</dc:date>
    <dc:language>en</dc:language>
    <dc:creator>Xurrency.com</dc:creator>
    <dc:simpleTitle>1 AUD = 1.0279 USD</dc:simpleTitle>
    <dc:baseCurrency>AUD</dc:baseCurrency>
    <dc:targetCurrency>USD</dc:targetCurrency>
    <dc:value frequency="daily" decimal="4">1.0279</dc:value>
  </item>


and so on

Open in new window

20110921-ExchangeRates.xml
0
Comment
Question by:JenebyM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 7

Accepted Solution

by:
dimaj earned 500 total points
ID: 36568982
I wrote a quick article on how to parse/modify xml using linq... see if it'll help you: http://blog.dimaj.net/2011/02/howto-update-xml-file-using-linq/
0
 

Author Comment

by:JenebyM
ID: 36569148
I need to identify what is wrong with parsing this specific file.

The node names appear peculiar and are not being parsed judging by the node.count = 0.

Using LINQ does not solve that problem and I will get nowhere by simply changing to using LInqXML if I cant get past this.

Please have a look at the XML file itself and see whether you can determine what Is the issue.

Many thanks
0
 
LVL 7

Assisted Solution

by:dimaj
dimaj earned 500 total points
ID: 36569363
try this:
      private void TestSomething ()
      {
         string fname = @"C:\temp\20110921-ExchangeRates.xml";
         XDocument myDoc = XDocument.Load(fname);

         IEnumerable<XElement> enumElements = myDoc.Root.Elements();
         IEnumerator enumer = enumElements.GetEnumerator();
         while (enumer.MoveNext() != null)
         {
            XElement curItem = (XElement)enumer.Current;
            var item = (from a in curItem.Attributes()
                             where !a.Value.Contains("EUR")
                             select a).ToList();
            if (item.Count > 0)
            {
               // get the value

            }
         }
      }

Open in new window

0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:JenebyM
ID: 36570075
Not really what I am looking for.

My problem is that i need to work with rdf and I was unable to read the xml file.

I have found what was missing:

1. I did not include all the required namespace reference for the RSS.
i.e
                man.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
                man.AddNamespace("slash", "http://purl.org/rss/1.0/modules/slash/");
                man.AddNamespace("taxo", "http://purl.org/rss/1.0/modules/taxonomy/");
                man.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
                man.AddNamespace("syn", "http://purl.org/rss/1.0/modules/syndication/");
                man.AddNamespace("admin", "http://webns.net/mvcb/");
                man.AddNamespace("feedburner", "http://rssnamespace.org/feedburner/ext/1.0");
                man.AddNamespace("rss", "http://purl.org/rss/1.0/");


2. The nodes list is obtained by:

 XmlNodeList xnList = responseXML.SelectNodes("rdf:RDF//rss:item", man);

These two changes have given me the results I need.

Will award partial marks for the answer.

Regards

0
 

Author Closing Comment

by:JenebyM
ID: 36570086
research further and found solution I was looking for elsewhere
0
 
LVL 7

Expert Comment

by:dimaj
ID: 36570088
Glad you've got it resolved!

dimaj
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question