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>
开发者_Python百科 <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.
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 believe the answer to this question might help you.
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");
doc.Descendants("ArrayOfStock").Concat(new[] { doc.Root });
try this
var result = from c in XDocument.Load("PATH_OF_XML_FILE").Descendants("ArrayOfStock")
select c;
精彩评论