Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

System.Xml.XmlNodeList not working

Posted on 2006-06-03
7
Medium Priority
?
355 Views
Last Modified: 2012-05-05
Ok view the source of the following xml, its very simple layout:

http://news.google.com/news?ned=us&topic=h&output=atom

here is my code that works fine:

            System.Net.WebRequest myRequest = System.Net.WebRequest.Create(rssURL);      
            System.Net.WebResponse myResponse = myRequest.GetResponse();
         
            System.IO.Stream rssStream = myResponse.GetResponseStream();
            System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();

            rssDoc.Load(rssStream);

here is the next line of code that is the problem:

System.Xml.XmlNodeList myXmlNodeList = rssDoc.SelectNodes("feed/entry");

it keeps coming back empty even though you can clearly see "feed" as the first node and plenty of child nodes called "entry".  
Please help.......



0
Comment
Question by:joshuadavidlee
  • 4
  • 3
7 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 16824480
Actually to be more precise there is a "feed" element that belongs to a namespace, which is why you aren't getting any matches with your current code.

            System.Net.WebRequest myRequest = System.Net.WebRequest.Create(rssURL);      
            System.Net.WebResponse myResponse = myRequest.GetResponse();
         
            System.IO.Stream rssStream = myResponse.GetResponseStream();
            System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();

            rssDoc.Load(rssStream);

            System.Xml.XmlNamespaceManager ns = new System.Xml.XmlNamespaceManager(rssDoc.NameTable);
            ns.AddNamespace("x", "http://purl.org/atom/ns#");

            System.Xml.XmlNodeList myXmlNodeList = rssDoc.SelectNodes("x:feed/x:entry", ns);
0
 

Author Comment

by:joshuadavidlee
ID: 16824514
ok now that worked but i have a question, i have seen examples where the "entry" will also have a namespace for each "entry"  will this cause a problem also if I come across a feed that does this?
0
 

Author Comment

by:joshuadavidlee
ID: 16824525
http://www.atomenabled.org/atom.xml  is an example where each "entry" has a namespace fro soem reason
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16824551
That shouldn't be a problem because, if you notice, they all have the same namespace. They all just have the namespace explicitly stated because of the way it will have been put together. As long as the namespace is the same the previous code will work fine.

If for some reason they do have a different namespace then you can change your code to grab all nodes irrespective of the namespace using:

    System.Xml.XmlNodeList myXmlNodeList = rssDoc.SelectNodes("*[local-name()='feed']/*[local-name()='entry']");

But if the namespace doesn't change then the first code sample will be faster, you should only use this second method as a last resort.
0
 

Author Comment

by:joshuadavidlee
ID: 16824566
ok thank you, could u explain why i even have to add the namespace myself ? since it is in the document when i load it anyways? i will be giving u the points btw
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16824592
You're not actually adding a namespace, you are specifying an alias that refers to nodes belonging to the namespace. So, in the sample:

    ns.AddNamespace("x", "http://purl.org/atom/ns#");

You are saying that you will identify any nodes in the "http://purl.org/atom/ns#" namespace with the prefix "x". In XPath you have to use a prefix to refer to any nodes belonging to a namespace, even if it is the default namespace.
0
 

Author Comment

by:joshuadavidlee
ID: 16824605
thanks you
0

Featured Post

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!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

810 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