Solved

Parsing an XML File

Posted on 2011-09-20
6
440 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data is not showing from images 15 37
C#  Radio button search for Date not DateTime 4 35
Wav problem 4 13
Reset asp.net login password 4 29
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now