Question

Fetching Values from XML using XQuery

Asked by: RadheRadhe

Hi,

I'm not able to fetch value from the following XML files:

Can anyone tell me to fetch - lets "Deposit Policy" information from this xml files - using XQuery - or let me know if any better method is available

<?xml version="1.0" ?>
<HotelML xmlns="www.test.com">
<Head>
 <Route Destination="01" Source="00">
  <Operation Action="Create" App="TIDispatcher" AppVer="1.12.40.2.8.1" DataPath="/HotelML" StartTime="2008-11-08T14:26:28.694+00:00" Success="true" TotalProcessTime="251" />
  </Route>
  </Head>
 <Property xml:lang="en" Code="BW;05250" Name="Best Western Gateway Inn" Token="1226154388618">
  <Amenity xml:lang="en" Code="BAR" Description="Bar/Lounge" />
  <Amenity xml:lang="en" Code="REST" Description="Restaurant" />
  <Amenity xml:lang="en" Code="POOL" Description="Pool" Property="true" />
  <Amenity xml:lang="en" Code="ROOM" Description="Room Service" Property="true" />
  <AreaAttraction Name="Restaurants:" />
  <AreaAttraction Name="Nearby Cities:" />
 <AreaAttraction Description="* In the heart of Silicon Valley * 2 miles from the Great America Theme Park * 5 miles from the Winchester Mystery House * 6 miles from Santa Clara/San Jose Convention Center * 30 miles from Oakland Coliseum * 40 miles from Monterey Peninsula * 80 miles from Napa Wine Country" Name="Local attractions, events and activities include">
  <Media Link="05250_m.gif" MIME="image/gif" Type="Map" />
  </AreaAttraction>
  <AreaAttraction Description="* 40 miles from San Francisco" Name="Nearby Cities" />
  <AreaAttraction Name="Local Attractions:" />
  <AreaAttraction Name="Transportation:" />
 <AreaAttraction Name="Area Map">
  <Media Link="05250_m.gif" MIME="image/gif" Type="Map" />
  </AreaAttraction>
 <AreaAttraction Name="Area Map">
  <Media Link="05250_m.gif" MIME="image/gif" Type="Map" />
  </AreaAttraction>
 <Brand Code="BW">
  <Email Address="nguyenn@bestwestern.com" />
  </Brand>
  <CancelPolicy Description="Cancellation policies vary from hotel to hotel. Most current and accurate cancellation policies will appear on your confirmed reservation notification." />
  <DepositPolicy AcceptedCreditCard="AX, CB, DC, DS, CA, VI, AM, JC" />
  <GeneralInfo AirportText="San Jose International Airport" AreasServed="San Jose, Santa Clara, Sunnyvale, Palo Alto, Milpitas, California" CheckInTime="3:00 P.M." CheckOutTime="12:00 Noon" HotelRating="AAA Rating: 3 Diamonds; MOBIL Rating: 2 Stars" LocationDescription="From Highway 101, take the Trimble Road Exit, and then take the first right on Seaboard Avenue and follow the signs to the Best Western Gateway Inn." NativeCurrency="USD" NumberOfFloors="2" NumberOfRooms="146" OpenEffectiveDate="1996-12-15" PropertyDescription="Gracious hospitality sets the pace at the Best Western Gateway Inn. Whether you are traveling for business or pleasure, our close attention to detail will make your stay in San Jose a memorable one. At the Best Western Gateway Inn you will find comfort and pleasure that are reminiscent of times past. Upon entering our lobby, you will be greeted by an enthusiatic helpful staff - 24 hours a day." RateDisclaimer="Indicative rates are for search purposes only. The entire range of rates may not be available during certain seasons or other specified periods of time. Check for specific rate availability when making a reservation." YearRenovatedText="1992" />
  <GeoInfo Accuracy="Property" Latitude="37.378991" Longitude="-121.938126" TimeZone="Pacific Standard Time" />
  <GuaranteePolicy Description="You must use a credit card to book online." />
  <Media Link="05250_b1a.jpg" MIME="image/jpeg" Type="Picture" />
  <Media Link="05250_b8a.jpg" MIME="image/jpeg" Type="Picture" />
  <Media Link="05250_b9a.jpg" MIME="image/jpeg" Type="Picture" />
  <Phone Number="(408)435-8800" />
  <Phone Number="1-800-437-8855" Type="Reservation" />
  <Phone Number="408-435-8879" Type="FAX" />
  <PostalAddress City="San Jose" CountryCode="US" Line1="2585 Seaboard Avenue" PostalCode="95131" StateCode="CA" />
  <RoomInfo Name="1 QUEEN BED,NO SMKNG,COFFEE MAKER,REFRIGERATOR,CON" Type="Standard Room" />
  <RoomInfo Name="1 KING BED,NO SMKNG,COFFEE MAKER,REFRIGERATOR,CONT" Type="Standard Room" />
  <RoomInfo Name="2 QUEEN BEDS,NO SMKNG,COFFEE MAKER,REFRIGERATOR,CO" Type="Standard Room" />
  </Property>
  </HotelML>
 

Regards

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-11-08 at 07:22:20ID23887775
Topics

XQuery

,

Extensible Stylesheet Language Transformation (XSLT)

,

Extensible Markup Language (XML)

Participating Experts
1
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Removing the xmlns attribute
    My root node is article and has an attribute xmlns which i would like to remove. <article xmlns="xxxxxxxxx"> <. . . . .. . . . . .. </article> Basically my question is that i want to remove the xmlns attribute of the article tag. Can i do this. I ...
  2. remove xmlns:
    why after i parse my xmldoc with an xslt I get on my result document this <?xml version=\"1.0\" encoding=\"utf-16\"?> and the xmlns:asp namespace all over the place? how do you remove it?
  3. xmlns
    Hi All Ive got an xml transformation into xhtml which starts.... <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="org.apache.xalan.xslt.ex...
  4. xmlns menace
    following is the xml file which i pass to xsl created by me. <?xml version="1.0" ?> <a xmlns="http://blah.blah/blah"> <name>again blah</name> </a> now if i dont put xmlns attribute in xml, xsl outputs the html in desired f...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: GertonePosted on 2008-11-08 at 07:57:56ID: 22912850

If all you need is the deposit policy,
you can do that in a single XPath "//xxx:DepositPolicy" if you first bind xxx to  "www.test.com", so you also need a namespace manager
whatever  language you are using to deal with this, should allow you to do that

How would you perform the XQuery? Is this in a database?

Tell me first how you were going to tackle all around the XQuery (programming language, tool or applications)
then I can give you the best fit.
XQuery seems somewhat ocverkill for this problem

 

by: RadheRadhePosted on 2008-11-08 at 08:15:44ID: 22912905

This is a response which I get from a third party website which is integrated with our site.

I have got "Web Services in C#" with integrate with this third party application

In terms on programming language - I need to find a way in C#...

Any better and faster of fetching values from this XML - as u said above XQuery might overkill it....

Thanks alot for quick reply

 

by: GertonePosted on 2008-11-08 at 12:46:18ID: 22913668

This should get you started

  	// create XML document object
  	XmlDocument xmlDocument = new XmlDocument();
  	// I assume that you want to load from a webservice instead   
    xmlDocument.Load(@"C:\Test.xml");   
 
  	// create the namespace manager here
    XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);   
    namespaceManager.AddNamespace("ns", "www.test.com");   
  	
  	// select the node, I am not sure about the value part
  	policy = xmlDocument.SelectSingleNode("//ns:DepositPolicy/@AcceptedCreditCard").value;
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

 

by: RadheRadhePosted on 2008-11-08 at 21:22:52ID: 22915186

Hi,

Thanks alot for example. You would advise not to use XQuery for this..

Also just one small request: Let's say I want to fetch all MEDIA stuff from it - then how it write some sort of loop which fetches all the data for me.

I'm very new to .net so apologies for asking may be siimple questions.

Regards

 

by: RadheRadhePosted on 2008-11-08 at 22:45:31ID: 22915326

When I write this code in C#, I'm get following error - any advise:
XPathException: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.]
   MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree() +63
   System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression expr, XPathNodeIterator context) +37
   System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression expr) +9
   System.Xml.XPath.XPathNavigator.Select(XPathExpression expr) +8
   System.Xml.XPath.XPathNavigator.Select(String xpath) +24
   System.Xml.XmlNode.SelectSingleNode(String xpath) +29
   _Default.Page_Load(Object sender, EventArgs e) +103
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +43
   System.Web.UI.Control.OnLoad(EventArgs e) +80
   System.Web.UI.Control.LoadRecursive() +49
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3750

 

 

by: GertonePosted on 2008-11-09 at 01:02:57ID: 22915601

If you need very complex queries you could start thinking about XQuery.
If you need to reconstruct a different XML, using complex queries, you could think of XQuery or even XSLT

If you simply need a list of all the media elements, you can build an iterator
policy = xmlDocument.SelectSingleNode("//ns:DepositPolicy/@AcceptedCreditCard").value;
would then become
list = xmlDocument.SelectNodes("//ns:Media");
Then you have a list of nodes, and you can work on them, maybe get the @link from them

I forgot to pass the namespacemanager in the XPath, sorry
policy = xmlDocument.SelectSingleNode("//ns:DepositPolicy/@AcceptedCreditCard").value;
should be
policy = xmlDocument.SelectSingleNode("//ns:DepositPolicy/@AcceptedCreditCard", namespaceManager).value;

Note that I don't have much C# experience myself, so we might run in a block at some point, but so far it is OK

 

by: RadheRadhePosted on 2008-11-09 at 01:55:38ID: 22915695

Hi,

I just tried this:

  // create XML document object
        XmlDocument xmlDocument = new XmlDocument();
        // I assume that you want to load from a webservice instead  
        xmlDocument.Load(@"C:\test.xml");

        // create the namespace manager here
        XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
        namespaceManager.AddNamespace("ns", "http://www.xpegs.com/v2001Q3/HotelML");

        Response.Write(xmlDocument.SelectSingleNode("//ns:DepositPolicy/@AcceptedCreditCard", namespaceManager));


And Result I'm getting is : System.Xml.XmlAttribute

Any advise... - Means I'm not able to fetch the "AcceptedCreditCard" details.

Regards
 

 

by: GertonePosted on 2008-11-09 at 02:58:44ID: 22915773

that means you are writing the type of the node to the response,
that is good, you are actually fetching an attribute
try
Response.Write(xmlDocument.SelectSingleNode("//ns:DepositPolicy/@AcceptedCreditCard", namespaceManager).value);

 

by: RadheRadhePosted on 2008-11-09 at 03:10:38ID: 22915804

Then how to get the value of that attribute.....

 

by: RadheRadhePosted on 2008-11-09 at 03:14:02ID: 22915811

Excellent Resolved.

Any thing else you want to add - which i should i keep in mind while fetching the value....

As I mean new to XML - the XML which im receiving is the one mentioned above.

I need to fetch value and send this back to our application server.

So any advise - would be highly appreciated...

 

by: RadheRadhePosted on 2008-11-09 at 03:15:44ID: 22915814

Can you please advise how to iterate through this XML file: and fetch all values related to MEDIA

 

by: GertonePosted on 2008-11-10 at 03:20:16ID: 22920506

Here is how you could handle nodelists with an iterator
(this one lists the attribute values for the @Link)

        XmlNodeList nodes = xmlDocument.SelectNodes("//ns:Media/@Link", namespaceManager);
        foreach (XmlNode node in nodes)
        {
            Console.WriteLine(node.value);
        }

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...