简体   繁体   English

在XDocument内部返回元素值

[英]Returning Element value deep inside XDocument

I have the following XML and I have been trying Descendents().Descendents().Descendents to retrieve an element value but I can't get it to work. 我有以下XML,我一直在尝试Descendents()。后代()。后代检索元素值,但我不能让它工作。

I want to return the first value found in the first element of PersonID. 我想返回在PersonID的第一个元素中找到的第一个值。

It is a string so I am doing 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>

Actually what you are doing wrong is you are not including Namespaces to your fetch code : 实际上你所做错的是你没有将命名空间包含在你的fetch代码中:

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

Use that, It will work. 使用它,它会工作。

But let me give you a brief information about namespaces : 但是,让我向您提供有关命名空间的简要信息:

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

In this example namespace applies to all the descendants but if you want to exclude some of 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) 现在这次LastName将从这个命名空间中排除(某事)

But if it is hard for you then you can use prefixes to do same thing with less hassle : 但是,如果你很难,那么你可以使用前缀来做同样的事情而不那么麻烦:

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

But if you want to include all the descendants into this name space you should use prefix within all descendants as shown the example right above 但是如果要将所有后代包含到此名称空间中,则应在所有后代中使用前缀,如上面的示例所示

If you want to exclude some elements from it : 如果要从中排除某些元素:

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

Then simply remove the prefix, that's it. 然后只需删除前缀即可。

You can also do the same thing 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 indicate the namespace within your code every time when you want to get something that has a namespace. 如果是这样,每当你想要获得具有命名空间的东西时,你需要在代码中指明命名空间。

 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");
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM