使用 LINQ to XML 返回子级

发布于 2024-11-02 11:09:02 字数 1226 浏览 3 评论 0原文

使用以下 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 技术交流群。

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

发布评论

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

评论(5

浅笑依然 2024-11-09 11:09:02

使用这个:

var arrayOfStockNodes = from arrayOfStock 
                        in doc.Root.Elements("ArrayOfStock") 
                        select arrayOfStock;

因为您想要拥有 ArrayOfStock 标记,所以您应该指定它......

Use this:

var arrayOfStockNodes = from arrayOfStock 
                        in doc.Root.Elements("ArrayOfStock") 
                        select arrayOfStock;

Because you want to have the ArrayOfStock tag, you should specify it...

第七度阳光i 2024-11-09 11:09:02

我相信这个问题的答案 可能对你有帮助。

I believe the answer to this question might help you.

暖伴 2024-11-09 11:09:02

如果可以有多个:

IEnumerable<XElement> arraysOfStock = doc.Root.Elements("ArrayOfStock");

如果可以只有一个:

XElement arrayOfStock = doc.Root.Element("ArrayOfStock");

If there can be more than one:

IEnumerable<XElement> arraysOfStock = doc.Root.Elements("ArrayOfStock");

If there can be only one:

XElement arrayOfStock = doc.Root.Element("ArrayOfStock");
姜生凉生 2024-11-09 11:09:02
doc.Descendants("ArrayOfStock").Concat(new[] { doc.Root });
doc.Descendants("ArrayOfStock").Concat(new[] { doc.Root });
层林尽染 2024-11-09 11:09:02

试试这个

 var result = from c in XDocument.Load("PATH_OF_XML_FILE").Descendants("ArrayOfStock")
                          select c;

try this

 var result = from c in XDocument.Load("PATH_OF_XML_FILE").Descendants("ArrayOfStock")
                          select c;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文