473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How to read external XML with namespaces from .NET

Hi

I am trying to integrate Yahoo! shipping in one of my projects and facing the following error:

1. I am using the GET method to find data from Yahoo in XML format. A typical response is as given below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:prods" xsi:schemaLocation="urn:yahoo:prods http://api.shopping.yahoo.com/shoppingservice/v1/productsearch.xsd" totalResultsAvailable="5263" firstResultPosition="1" totalResultsReturned="10">
<Result>
<Catalog ID="3004510600">
<Url><![CDATA[http://shopping.yahoo.com/p:Harry%20Potter%20Collection%3A:3004510600]]></Url>
<ProductName><![CDATA[Harry Potter Collection:]]></ProductName>
<PriceFrom>100.13</PriceFrom>
<PriceTo>127.15</PriceTo>
<Thumbnail>
<Url><![CDATA[http://f3c.yahoofs.com/shopping/3061944/simg_t_t0439827604gif85?rm_____DFuy3NLrf]]></Url>
<Height>81</Height>
<Width>85</Width>
</Thumbnail>
<UserRating>
<MaxRating>5</MaxRating>
<NumRatings>4</NumRatings>
<AverageRating>4.5</AverageRating>
<RatingUrl><![CDATA[http://shopping.yahoo.com/p:Harry%20Potter%20Collection%3A:3004510600:page=u ser-reviews]]></RatingUrl>
<CreateRatingUrl><![CDATA[http://shopping.yahoo.com/p:Harry%20Potter%20Collection%3A:3004510600:page=p ost-reviews]]></CreateRatingUrl>
</UserRating>
</Catalog>
</Result>
</ResultSet>


2. I am trying to read the above using XMLDocument object. Using that, I am able to get a handle to the root object and also able to navigate through the child nodes sequentially.
3. However, I am not able to execute XPath queries on the XML due to the XML having namespace definitons.
4. I tried using NameSpaceManager but due to my lack of experience with XML namespaces, I am not able to continue further.

Can someone please help me out in this?

Thanks,
Rupinder
Jun 27 '07 #1
5 2131
misogsk
39
Hi

I am trying to integrate Yahoo! shipping in one of my projects and facing the following error:

1. I am using the GET method to find data from Yahoo in XML format. A typical response is as given below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:prods" xsi:schemaLocation="urn:yahoo:prods http://api.shopping.yahoo.com/shoppingservice/v1/productsearch.xsd" totalResultsAvailable="5263" firstResultPosition="1" totalResultsReturned="10">
<Result>
<Catalog ID="3004510600">
<Url><![CDATA[http://shopping.yahoo.com/p:Harry%20Potter%20Collection%3A:3004510600]]></Url>
<ProductName><![CDATA[Harry Potter Collection:]]></ProductName>
<PriceFrom>100.13</PriceFrom>
<PriceTo>127.15</PriceTo>
<Thumbnail>
<Url><![CDATA[http://f3c.yahoofs.com/shopping/3061944/simg_t_t0439827604gif85?rm_____DFuy3NLrf]]></Url>
<Height>81</Height>
<Width>85</Width>
</Thumbnail>
<UserRating>
<MaxRating>5</MaxRating>
<NumRatings>4</NumRatings>
<AverageRating>4.5</AverageRating>
<RatingUrl><![CDATA[http://shopping.yahoo.com/p:Harry%20Potter%20Collection%3A:3004510600:page=u ser-reviews]]></RatingUrl>
<CreateRatingUrl><![CDATA[http://shopping.yahoo.com/p:Harry%20Potter%20Collection%3A:3004510600:page=p ost-reviews]]></CreateRatingUrl>
</UserRating>
</Catalog>
</Result>
</ResultSet>


2. I am trying to read the above using XMLDocument object. Using that, I am able to get a handle to the root object and also able to navigate through the child nodes sequentially.
3. However, I am not able to execute XPath queries on the XML due to the XML having namespace definitons.
4. I tried using NameSpaceManager but due to my lack of experience with XML namespaces, I am not able to continue further.

Can someone please help me out in this?

Thanks,
Rupinder
Hi,

You can use:
using System.Xml;
You will need XmlDocument class to load XML document.
To read xml file use XmlReader class, for more information go to http://msdn2.microsoft.com/en-us/library/system.xml.xmlreader_members.aspx

I hope this will help you.
Jun 27 '07 #2
You have to create a NamspaceManager, providing the constructor the NameTable of the XmlDocument.

Add the required namespaces to the namespace manager, taking care that you write the correct prefix.

Then, in all the methods which use XPath, like for example SelectSingleNode, precede the xml node name with the prefix and a double-colon.

In your case, I think it would be smth like this:

XmlDocument doc = new XmlDocument();
doc.Load("myfile.xml");

XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);

man.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:prods");

After this in all your XPath expressions, instead of ResultSet, use xsi:ResultSet.

GL.
Jun 27 '07 #3
You have to create a NamspaceManager, providing the constructor the NameTable of the XmlDocument.

Add the required namespaces to the namespace manager, taking care that you write the correct prefix.

Then, in all the methods which use XPath, like for example SelectSingleNode, precede the xml node name with the prefix and a double-colon.

In your case, I think it would be smth like this:

XmlDocument doc = new XmlDocument();
doc.Load("myfile.xml");

XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);

man.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:prods");

After this in all your XPath expressions, instead of ResultSet, use xsi:ResultSet.

GL.
I forgot to escape the quotes in the namespace string.
Jun 27 '07 #4
I forgot to escape the quotes in the namespace string.

XmlDocument doc = new XmlDocument();
doc.Load("myfile.xml");

XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);

man.AddNamespace("xsi", "urn:yahoo:prods");

XMLNodeList NL_Results = doc.documentElement("//xsi:Result");

The above is working fine. However while trying to access second level nodes, I am not sure how to proceed.

I tried "//xsi:Catalog/xsi:Url" and "//xsi:Catalog/Url" but none of them helps.

Any help would be greatly appreciated.

Thanks,
Rupinder
Jun 27 '07 #5
XmlDocument doc = new XmlDocument();
doc.Load("myfile.xml");

XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);

man.AddNamespace("xsi", "urn:yahoo:prods");

XMLNodeList NL_Results = doc.documentElement("//xsi:Result");

The above is working fine. However while trying to access second level nodes, I am not sure how to proceed.

I tried "//xsi:Catalog/xsi:Url" and "//xsi:Catalog/Url" but none of them helps.

Any help would be greatly appreciated.

Thanks,
Rupinder
I think it is "//xsi:Url". Because of the // operator you don't have to specify the whole tree hierarchy.
Jun 28 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: pohmart | last post by:
I am using Xerces 2.6.2 and SAX on Windows2k. I want to validate an XML doc, which has no schema reference, with an external schema which is on my PC. I am setting the following properties and...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
4
by: Tron Thomas | last post by:
I have read that anonymous namespaces are preferred in C++ over declaring global statics because the namespace can hide information from other translation units and at the same time provide...
0
by: DDE | last post by:
Hi all, I have an application (number of web Services) in which I define different application variables, like application etc. Besides that, I have a couple of assemblies containing classes and...
4
by: Max | last post by:
Hello, I have written a VB.NET Windows application that will be used by my customers to automate another application. I want to allow my users to execute Visual Basic code that they have...
3
by: Thomas Lenarz | last post by:
Hello, please forgive me for posting this if this problem has come up before. I could not find any hint with google. I am porting a bunch of source-code from SUN-OS to AIX at the moment and...
5
by: ashley.ward | last post by:
I am attempting to write a program with VB 2005 Express Edition which accesses an Oracle 9 database and dumps the results of three SELECT queries into a spreadsheet file once every hour. ...
5
by: .rhavin grobert | last post by:
Hi group....;-) i have the following class.... _____________________________________________________ // QSharedReg.hpp typedef unsigned __int64 QUAD; using namespace boost;
3
by: Maximus | last post by:
How to import a function in another translation unit with a namespace? Let's say we have two cpp files: 1.cpp namesapce mu { int func() { return 0;} };
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.