0

Here is my xml. i am using XDocument in C#. I want to get the "recordSetCount".

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ShowPositionOpening xmlns="http://data.usajobs.gov">
      <ApplicationArea xmlns="http://www.openapplications.org/oagis/9">
        <CreationDateTime>2014-12-11T04:05:41</CreationDateTime>
      </ApplicationArea>
      <DataArea xmlns="http://www.hr-xml.org/3">
        <Show recordSetCount="6" recordSetTotal="6" recordSetCompleteIndicator="false" recordSetReferenceId="1" xmlns="http://www.openapplications.org/oagis/9">
          <OriginalApplicationArea>
            <CreationDateTime>2014-12-11T04:05:41</CreationDateTime>
          </OriginalApplicationArea>
        </Show>

I have tried like below

var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");

var jobCount = x.XPathSelectElement("/s:Envelope/s:Body/ShowPositionOpening/DataArea/Show/@recordSetCount", namespaceManager).Value;

but it dint work. Also tried like below

XNamespace xmlns = "http://www.openapplications.org/oagis/9";
XNamespace xmlns1 = "http://data.usajobs.gov";
XNamespace x1 = "http://www.hr-xml.org/3";

var jobCount = x.Element("ShowPositionOpening")
                .Element(xmlns1 + "DataArea")
                .Element(x1 + "Show")
                .Attribute("recordSetTotal"); 

But it didn't work. What went wrong. Can any one help me?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
backtrack
  • 7,996
  • 5
  • 52
  • 99

1 Answers1

3

Your data has multiple namespaces where each child is in a different namespace, you'll need to adjust your queries accordingly.

ShowPositionOpening    http://data.usajobs.gov
DataArea               http://www.hr-xml.org/3
Show                   http://www.openapplications.org/oagis/9
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
namespaceManager.AddNamespace("X", "http://data.usajobs.gov");
namespaceManager.AddNamespace("XX", "http://www.hr-xml.org/3");
namespaceManager.AddNamespace("XXX", "http://www.openapplications.org/oagis/9");
// you'll need to change this to XPathEvaluate
// since you're not evaluating to an element
var JobCount = x.XPathEvaluate("/s:Envelope/s:Body/X:ShowPositionOpening/XX:DataArea/XXX:Show/@recordSetCount", namespaceManager);

or using linq:

XNamespace n = "http://data.usajobs.gov";
XNamespace nn = "http://www.hr-xml.org/3";
XNamespace nnn = "http://www.openapplications.org/oagis/9";
var JobCount = x.Descendants(n + "ShowPositionOpening")
                .Elements(nn + "DataArea")
                .Elements(nnn + "Show")
                .Attributes("recordSetCount")
                .SingleOrDefault();
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • var count = (from service in x.Descendants(n + "ShowPositionOpening") select (string)service.Element(nn + "DataArea").Element(nnn + "Show").Attribute("recordSetCount").Value).FirstOrDefault(); I have did this also. Thank you a lot – backtrack Dec 16 '14 at 05:11