How to read external XML with namespaces from .NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rupinderbatra
    New Member
    • Jun 2007
    • 17

    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:yaho o:prods" xsi:schemaLocat ion="urn:yahoo: prods http://api.shopping.ya hoo.com/shoppingservice/v1/productsearch.x sd" totalResultsAva ilable="5263" firstResultPosi tion="1" totalResultsRet urned="10">
    <Result>
    <Catalog ID="3004510600" >
    <Url><![CDATA[http://shopping.yahoo. com/p:Harry%20Potte r%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_t0439827 604gif85?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%20Potte r%20Collection% 3A:3004510600:p age=user-reviews]]></RatingUrl>
    <CreateRatingUr l><![CDATA[http://shopping.yahoo. com/p:Harry%20Potte r%20Collection% 3A:3004510600:p age=post-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 NameSpaceManage r 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
  • misogsk
    New Member
    • Jan 2007
    • 39

    #2
    Originally posted by rupinderbatra
    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:yaho o:prods" xsi:schemaLocat ion="urn:yahoo: prods http://api.shopping.ya hoo.com/shoppingservice/v1/productsearch.x sd" totalResultsAva ilable="5263" firstResultPosi tion="1" totalResultsRet urned="10">
    <Result>
    <Catalog ID="3004510600" >
    <Url><![CDATA[http://shopping.yahoo. com/p:Harry%20Potte r%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_t0439827 604gif85?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%20Potte r%20Collection% 3A:3004510600:p age=user-reviews]]></RatingUrl>
    <CreateRatingUr l><![CDATA[http://shopping.yahoo. com/p:Harry%20Potte r%20Collection% 3A:3004510600:p age=post-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 NameSpaceManage r 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.xmlr eader_members.a spx

    I hope this will help you.

    Comment

    • blackjack2150
      New Member
      • Feb 2007
      • 79

      #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 SelectSingleNod e, 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("myfil e.xml");

      XmlNamespaceMan ager man = new XmlNamespaceMan ager(doc.NameTa ble);

      man.AddNamespac e("xsi", "http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yaho o:prods");

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

      GL.

      Comment

      • blackjack2150
        New Member
        • Feb 2007
        • 79

        #4
        Originally posted by blackjack2150
        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 SelectSingleNod e, 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("myfil e.xml");

        XmlNamespaceMan ager man = new XmlNamespaceMan ager(doc.NameTa ble);

        man.AddNamespac e("xsi", "http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yaho o: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.

        Comment

        • rupinderbatra
          New Member
          • Jun 2007
          • 17

          #5
          Originally posted by blackjack2150
          I forgot to escape the quotes in the namespace string.

          XmlDocument doc = new XmlDocument();
          doc.Load("myfil e.xml");

          XmlNamespaceMan ager man = new XmlNamespaceMan ager(doc.NameTa ble);

          man.AddNamespac e("xsi", "urn:yahoo:prod s");

          XMLNodeList NL_Results = doc.documentEle ment("//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

          Comment

          • blackjack2150
            New Member
            • Feb 2007
            • 79

            #6
            Originally posted by rupinderbatra
            XmlDocument doc = new XmlDocument();
            doc.Load("myfil e.xml");

            XmlNamespaceMan ager man = new XmlNamespaceMan ager(doc.NameTa ble);

            man.AddNamespac e("xsi", "urn:yahoo:prod s");

            XMLNodeList NL_Results = doc.documentEle ment("//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.

            Comment

            Working...