Linq to XML Get the next node with the same name

Here is my XML

<assets>
    <asset>
        <metadata Id="ItemType" Value="Image"/>
        <metadata Id="ItemUri" Value="http://blah.png"/>
    </asset>
    <asset>
        <metadata Id="ItemType" Value="Image"/>
        <metadata Id="ItemUri" Value="http://blah2.png"/>
    </asset>
</assets>

How to get the second value <metadata>containing the URI?

List<Asset> assets = (from asset in xmlDocument.Descendants("asset")
                              select new Asset
                              {
                                  ItemType = asset.Element("metadata").Attribute("Value").Value,
                                  ItemUri = asset.Element("metadata").Attribute("Value").Value
                              }).ToList<Asset>();

Currently, my code just returns the same value from the first <metadata>, of course.

+1
source share
4 answers

This is what I ended up doing. The answers above are where good, but if <metadata>not OK, I will get the wrong data. Thus, I make a request and get the right one, regardless of order.

List<Asset> assets = (from asset in xmlDocument.Descendants("asset")
                              select new Asset
                              {
                                  ItemType = asset.Elements().Single(x => x.Attribute("Id").Value == "ItemType").Attribute("Value").Value,
                                  ItemUri = asset.Elements().Single(x => x.Attribute("Id").Value == "ItemUri").Attribute("Value").Value,    
                              }).ToList<Asset>();
+2
source

It seems that .Element (..) gets the first element with a matching name if multiple elements are present. You can edit the linq query to something like

var assets = (from asset in doc.Descendants("asset")
                 let metadata = asset.Descendants("metadata").ToArray()
                 let type = metadata[0].Attribute("Value").Value
                 let uri = metadata[1].Attribute("Value").Value
                 select new Asset { ItemType = type, ItemUri = uri }).ToList();
0
source
var assets = (from asset in xmlDocument.Descendants("asset")
              select new Asset
              {
                  ItemType = (string)asset.Element("metadata").Attribute("Value"),
                  ItemUri = (string)asset.Elements("metadata").ElementAt(1).Attribute("Value")
              }).ToList();
0

IEnumerable , , .

var assets = from asset in xd.Descendants("asset")
             from metaType in asset.Descendants("metadata")
             from metaUri in asset.Descendants("metadata")
             where metaType.Attribute("Id").Value == "ItemType"
             && metaUri.Attribute("Id").Value == "ItemUri"
             select new
             {
                 ItemType = metaType.Attribute("Value").Value,
                 ItemUri = metaUri.Attribute("Value").Value
             };
0

Source: https://habr.com/ru/post/1762615/


All Articles