Solved

Parsing an XML File

Posted on 2011-09-20
6
449 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

730 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