Solved

Parsing an XML File

Posted on 2011-09-20
6
438 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:JenebyM
Comment Utility
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
Comment Utility
research further and found solution I was looking for elsewhere
0
 
LVL 7

Expert Comment

by:dimaj
Comment Utility
Glad you've got it resolved!

dimaj
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 109
Using MS Code on my Mac 6 44
asp.net mvc5 3 35
Problem to picture file 3 37
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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.
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

771 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

13 Experts available now in Live!

Get 1:1 Help Now