Wouter
Wouter

Reputation: 3

Can't find a certain node in XML

My problem is that I can't seem to get a list of nodes I need. I have tried multiple solutions and it doesn't seem to work. This is a part of my xml file:

<findItemsByCategoryResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
    <ack>Success</ack>
    <version>1.13.0</version>
    <timestamp>2016-08-23T07:33:22.497Z</timestamp>
    <searchResult count="100">
        <item>
            <itemId>152210133431</itemId>
            <title>...</title>
            <globalId>EBAY-GB</globalId>
            <primaryCategory>
                <categoryId>218</categoryId>
                <categoryName>Magic the Gathering</categoryName>
            </primaryCategory>
            <galleryURL>
http://thumbs4.ebaystatic.com/m/meIDrVqhmbpQMYCxzeUvR9Q/140.jpg
</galleryURL>
            <viewItemURL>
http://www.ebay.co.uk/itm/MTG-See-Unwritten-Khans-Tarkir-MYTHIC-MINT-/152210133431
</viewItemURL>
            <paymentMethod>PayPal</paymentMethod>
            <autoPay>false</autoPay>
            <location>London,United Kingdom</location>
            <country>GB</country>
            <shippingInfo>
                <shippingServiceCost currencyId="GBP">1.1</shippingServiceCost>
                <shippingType>Flat</shippingType>
                <shipToLocations>GB</shipToLocations>
            </shippingInfo>
            <sellingStatus>
                <currentPrice currencyId="GBP">0.5</currentPrice>
                <convertedCurrentPrice currencyId="GBP">0.5</convertedCurrentPrice>
                <bidCount>0</bidCount>
                <sellingState>Active</sellingState>
                <timeLeft>P0DT0H19M12S</timeLeft>
            </sellingStatus>
            <listingInfo>
                <bestOfferEnabled>false</bestOfferEnabled>
                <buyItNowAvailable>false</buyItNowAvailable>
                <startTime>2016-08-18T07:52:34.000Z</startTime>
                <endTime>2016-08-23T07:52:34.000Z</endTime>
                <listingType>Auction</listingType>
                <gift>false</gift>
            </listingInfo>
            <condition>
                <conditionId>1000</conditionId>
                <conditionDisplayName>New</conditionDisplayName>
            </condition>
            <isMultiVariationListing>false</isMultiVariationListing>
            <topRatedListing>false</topRatedListing>
        </item>
    </searchResult>
    <paginationOutput>...</paginationOutput>
    <itemSearchURL>...</itemSearchURL>
</findItemsByCategoryResponse>

Normally there are 100 item nodes in this xml file, I just shorted the file. I tried to get the ''item'' nodes and everything inside in a XmlNodeList, but when I debug it says it contains 0 items. After this I want to retrieve some data, as you can see in the code. Note: I tried a lot of xpath values! Here is the code I used:

            XmlDocument xmlText = new XmlDocument();
            xmlText.Load(basicUrl);
            XmlElement root = xmlText.DocumentElement;
            XmlNodeList xnList = root.SelectNodes("//item");
            foreach(XmlNode item in xnList)
            {
                string title = item["title"].InnerText;
                Console.WriteLine(title);
            }

Upvotes: 0

Views: 1286

Answers (1)

Quentin Roger
Quentin Roger

Reputation: 6538

XPath Expressions are always namespace aware (if the Element has a Namespace then the XPath must reference it by namespace). Also, in XPath expressions, the 'default' namespace is always in the URI "". In your case you should do something like this :

        XmlDocument xmlText = new XmlDocument();
        xmlText.Load(yourXml);
        XmlElement root = xmlText.DocumentElement;
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlText.NameTable);
        nsmgr.AddNamespace("t", "http://www.ebay.com/marketplace/search/v1/services");
        XmlNodeList xnList = xmlText.SelectNodes("//t:item", nsmgr);
        foreach (XmlNode item in xnList)
        {
            string title = item["title"].InnerText;
            Console.WriteLine(title);
        }

SelectNodes with namespace :

https://msdn.microsoft.com/en-US/library/4bektfx9(v=vs.110).aspx

Upvotes: 1

Related Questions