使用 LINQ to XML 返回子级
使用以下 XML。
<?xml version="1.0"?>
<Message>
<ArrayOfStock xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Stock>
<StockID>9cddb639-25ee-4415-be07-3109e5ae9883</StockID>
<Description>Stock Item 0</Description>
</Stock>
<Stock>
<StockID>f89f02f9-b359-48c8-8d2f-3a950837f4fb</StockID>
<Description>Stock Item 1</Description>
</Stock>
<Stock>
<StockID>3338ec80-f59e-4979-a04c-f7d52e386bb7</StockID>
<Description>Stock Item 2</Description>
</Stock>
</ArrayOfStock>
</Message>
有人可以告诉我如何仅返回 ArrayOfStock XML 吗?
我已经使用
using (MemoryStream memStream = new MemoryStream(this.Message))
{
XDocument doc = XDocument.Load(memStream);
var message = from arrayOfStock in doc.Elements("Message")
select arrayOfStock;
}
它似乎返回 ArrayOfStock 但也包括消息节点本身。
Using the following XML.
<?xml version="1.0"?>
<Message>
<ArrayOfStock xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Stock>
<StockID>9cddb639-25ee-4415-be07-3109e5ae9883</StockID>
<Description>Stock Item 0</Description>
</Stock>
<Stock>
<StockID>f89f02f9-b359-48c8-8d2f-3a950837f4fb</StockID>
<Description>Stock Item 1</Description>
</Stock>
<Stock>
<StockID>3338ec80-f59e-4979-a04c-f7d52e386bb7</StockID>
<Description>Stock Item 2</Description>
</Stock>
</ArrayOfStock>
</Message>
Could someone please show me how I would return just the ArrayOfStock XML?
I have used
using (MemoryStream memStream = new MemoryStream(this.Message))
{
XDocument doc = XDocument.Load(memStream);
var message = from arrayOfStock in doc.Elements("Message")
select arrayOfStock;
}
And it seems to return the ArrayOfStock but also includes the message node itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用这个:
因为您想要拥有
ArrayOfStock
标记,所以您应该指定它......Use this:
Because you want to have the
ArrayOfStock
tag, you should specify it...我相信这个问题的答案 可能对你有帮助。
I believe the answer to this question might help you.
如果可以有多个:
如果可以只有一个:
If there can be more than one:
If there can be only one:
试试这个
try this