返回 XDocument 深处的元素值
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您做错的是您没有将命名空间包含到您的获取代码中:
使用它,它会起作用。
但让我给你一个关于命名空间的简短信息:
在这个例子中,命名空间适用于所有后代,但如果你想排除一些后代,
现在这次 LastName 将从这个命名空间中排除(某事),
但如果这对你来说很难那么您可以使用前缀来减少麻烦地执行相同的操作:
但是如果您想将所有后代包含到此名称空间中,您应该在所有后代中使用前缀,如上面的示例所示
如果您想从中排除某些元素:
那么只需去掉前缀,就可以了。
您也可以对属性执行相同的操作:
如果是这样,则每次您想要获取具有名称空间的内容时,都需要在代码中指示名称空间。
Actually what you are doing wrong is you are not including Namespaces to your fetch code :
Use that, It will work.
But let me give you a brief information about namespaces :
In this example namespace applies to all the descendants but if you want to exclude some of descendants
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 :
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 :
Then simply remove the prefix, that's it.
You can also do the same thing for attributes :
And if so you need to indicate the namespace within your code every time when you want to get something that has a namespace.