返回 XDocument 深处的元素值

发布于 2024-08-08 19:32:04 字数 2696 浏览 4 评论 0原文

我有以下 XML,并且一直在尝试 Descendents().Descendents().Descendents 来检索元素值,但我无法让它工作。

我想返回在 PersonID 第一个元素中找到的第一个值。

它是一个字符串,所以我这样做:

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>

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.

I want to return the first value found in the first element of 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花肆意 2024-08-15 19:32:04

实际上,您做错的是您没有将命名空间包含到您的获取代码中:

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

使用它,它会起作用。

但让我给你一个关于命名空间的简短信息:

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

在这个例子中,命名空间适用于所有后代,但如果你想排除一些后代,

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

现在这次 LastName 将从这个命名空间中排除(某事),

但如果这对你来说很难那么您可以使用前缀来减少麻烦地执行相同的操作:

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

但是如果您想将所有后代包含到此名称空间中,您应该在所有后代中使用前缀,如上面的示例所示

如果您想从中排除某些元素:

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

那么只需去掉前缀,就可以了。

您也可以对属性执行相同的操作:

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

如果是这样,则每次您想要获取具有名称空间的内容时,都需要在代码中指示名称空间。

Actually what you are doing wrong is you are not including Namespaces to your fetch code :

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)

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.

冷夜 2024-08-15 19:32:04
 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");
 }
 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");
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文