Return element value deep inside XDocument

I have the following XML and I am trying Descendents (). Descendents (). Descendants to retrieve the value of an element, but I can't seem to get it to work.

I want to return the first value found in the first PersonID element.

This is a string, so I do this:

XDocument XDoc = XDocument.Parse(XmlString);

    <Root>
<Code>200001</Code>
<MsgType>106</MsgType>
<PersonData>
<MSG>
<NewDataSet xmlns="PersonInstances">
      <PersonInstances>
        <PersonInstanceId>1</PersonInstanceId>
        <PersonId>400</PersonId>
        <Status>210005</Status>
        <DateChanged>2009-10-20T11:53:00+01:00</DateChanged>
      </PersonInstances>
      <PersonInstances>
        <PersonInstanceId>2</PersonInstanceId>
        <PersonId>400</PersonId>
        <Status>210005</Status>
        <DateChanged>2009-10-20T12:13:00+01:00</DateChanged>
      </PersonInstances>
      <PersonInstances>
        <PersonInstanceId>3</PersonInstanceId>
        <PersonId>400</PersonId>
        <Status>210005</Status>
        <DateChanged>2009-10-20T15:28:00+01:00</DateChanged>
      </PersonInstances>
      <PersonInstances>
        <PersonInstanceId>4</PersonInstanceId>
        <PersonId>400</PersonId>
        <Status>210005</Status>
        <DateChanged>2009-10-20T15:32:00+01:00</DateChanged>
      </PersonInstances>
      <PersonInstances>
        <PersonInstanceId>5</PersonInstanceId>
        <PersonId>400</PersonId>
        <Status>210005</Status>
        <DateChanged>2009-10-21T10:49:00+01:00</DateChanged>
      </PersonInstances>
      <PersonInstances>
        <PersonInstanceId>6</PersonInstanceId>
        <PersonId>400</PersonId>
        <Status>210005</Status>
        <DateChanged>2009-10-21T17:15:00+01:00</DateChanged>
      </PersonInstances>
      <PersonInstances>
        <PersonInstanceId>7</PersonInstanceId>
        <PersonId>400</PersonId>
        <Status>210005</Status>
        <DateChanged>2009-10-22T10:06:00+01:00</DateChanged>
      </PersonInstances>
      <PersonInstances>
        <PersonInstanceId>8</PersonInstanceId>
        <PersonId>400</PersonId>
        <Status>210005</Status>
        <DateChanged>2009-10-22T16:01:00+01:00</DateChanged>
      </PersonInstances>
    </NewDataSet></MSG></PersonData></Root>

      

+2


source to share


2 answers


In fact, what you are doing wrong is you are not including Namespaces in your fetch code:

var el = (element.Descendants(XNamespace.Get("PersonInstances") +
"PersonId").FirstOrDefault()).Value;

      

Use this, it will work.

But let me give you some quick information about namespaces:

<Persons xlmns="something">
 <Person>
  <Name>John</Name>
 </Person>
</Person>

      

In this example, the namespace is applied to all descendants, but if you want to exclude some of the descendants

<Persons xlmns="something">
 <Person>
  <Name>John</Name>
  <LastName xmlns="">Usher</LastName>
 </Person>
</Person>

      

Now this time LastName will be excluded from this namespace (something)

But if you're having a hard time, you can use prefixes to do the same with fewer problems:



<pre:Persons xlmns:pre="something">
 <pre:Person>
  <pre:Name>John</Name>
 </Person>
</Person>

      

But if you want to include all descendants in this namespace, you must prefix all descendants as shown in the example on the right above

If you want to exclude certain elements from it:

<pre:Persons xlmns:pre="something">
 <Person>
  <pre:Name>John</Name>
 </Person>
</Person>

      

Then just remove the prefix to have it.

You can also do the same for attributes:

<pre:Persons xlmns:pre="something">
 <pre:Person>
  <pre:Name pre:Value="Yahoo">John</pre:Name>
 </Person>
</Person>

      

And if so, you need to specify the namespace in your code every time you want to get what the namespace has.

+8


source


 XDocument XDoc = XDocument.Parse(xfile);
 XNamespace ns = "PersonInstances";
 if (XDoc.Root.Descendants(ns + "PersonId").Any())
 {
    Console.Write(XDoc.Root.Descendants(ns + "PersonId").First().Value);
 }
 else
 {
    Console.Write("Fail");
 }

      



0


source







All Articles