0

I want to grab UPC value from the following XML example node, but the current doc.SelectNodes fails to grab that value. I am using XmlDocument to process my XML. Can you fix my code to grab UPC value? What am I doing wrong here?

C# Code:

string responseStr = new StreamReader(responseStream).ReadToEnd();
responseStream.Flush();
responseStream.Close();

XmlDocument doc = new XmlDocument();
doc.LoadXml(responseStr);
if (doc.GetElementsByTagName("Ack").Item(0).InnerText != "Failure")
{
    string UPC = doc.SelectNodes("Item").Item(0).SelectNodes("UPC").Item(0).InnerText;
}

XML Sample:

<?xml version="1.0" encoding="UTF-8"?>
<GetItemResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2018-07-28T08:18:10.048Z</Timestamp>
<Ack>Success</Ack>
<Version>1069</Version>
<Build>E1069_CORE_API_18748854_R1</Build>
<Item>
    <ProductListingDetails>
        <ISBN>Not Applicable</ISBN>
        <UPC>853365007036</UPC>
        <EAN>0853365007036</EAN>
        <BrandMPN>
        <Brand>UpCart</Brand>
        <MPN>MPCB-1DX</MPN>
        </BrandMPN>
        <IncludeeBayProductDetails>true</IncludeeBayProductDetails>
    </ProductListingDetails>
</Item>
</GetItemResponse>
pfx
  • 20,323
  • 43
  • 37
  • 57
john Cogdle
  • 1,423
  • 2
  • 16
  • 26
  • Are you able to select any other value? I mean from some another node? – Eldlabs Jul 28 '18 at 09:16
  • No it says things i am selecting is not available. – john Cogdle Jul 28 '18 at 09:17
  • You need to specify the correct namespace, which is `urn:ebay:apis:eBLBaseComponents`, in your call to `SelectNodes()`. See e.g. [XmlDocument.SelectSingleNode and xmlNamespace issue](https://stackoverflow.com/q/4171451/3744182) and [Can't get XmlDocument.SelectNodes to retrieve any of my nodes?](https://stackoverflow.com/q/12607895/3744182) and [XPath with XmlDocument not finding nodes](https://stackoverflow.com/a/40580835/3744182). – dbc Jul 28 '18 at 09:18
  • All of the above is true. Also the answer below. But why do you use `SelectNodes` and not `GetElementsByTagName` like you did with `"Ack"`? – Ofir Winegarten Jul 28 '18 at 09:57

1 Answers1

0

You have a namespace that must be used to get values. Here is the easy way using xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = doc.Root.GetDefaultNamespace();

            string UPC = (string)doc.Descendants(ns + "UPC").FirstOrDefault();
        }
    }
}

If you have nulls use this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = doc.Root.GetDefaultNamespace();

            var upcs = doc.Descendants(ns + "UPC");
            if (upcs != null)
            {
                string upc = (string)upcs.FirstOrDefault();
            }
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • `doc` does not contains a definition for `Root` – john Cogdle Jul 28 '18 at 09:25
  • You are using XmlDocument which is old. I'm using XDocument which is the new msdn library. Look at the header in my code. – jdweng Jul 28 '18 at 09:39
  • Ok but you will see in my code i also used a `if` condition. How can i convert this to your XDoc version then? if(doc.GetElementsByTagName("Ack").Item(0).InnerText != "Failure"){} – john Cogdle Jul 28 '18 at 10:02