简体   繁体   中英

Linq to XML get nested child attribute based on parent attribute

I need some help with some LINQ to XML. I'm tempted to revert to XPath but am quite keen to learn LINQ to XML properly. So below is a stripped down version of my XML:

<SData Key="626">
<Event OID="INITIALVISIT">
    <FData FID="MODAL_V10">
        <IGData IOID="MODALITYTABLE">
            <Item OID="I_TABLE_MODAL_DATE_TABLE" Value="2015-06-03" />
            <Item OID="I_TABLE_MODAL_TYPE_TABLE" Value="1" />
        </IGData>
        <IGData IOID="MODALITYTABLE2">
            <Item OID="I_TABLE_MODAL_DATE_TABLE2" Value="2015-06-09" />
            <Item OID="I_TABLE_MODAL_TYPE_TABLE2" Value="1" />
        </IGData>
    </FData>
    <FData FID="UPLOAD_V40">
        <IGData IOID="IG_IMAGE_UNGROUPED">
            <Item OID="I_IMAGE_UPLOAD_XNAT" Value="1" />
            <Item OID="I_IMAGE_UPLOAD_COMPLETE" Value="1" />
        </IGData>
        <IGData IOID="IG_IMAGE_UPLOAD">
            <Item OID="I_IMAGE_UPLOAD_XNAT" Value="1" />
            <Item OID="I_IMAGE_UPLOAD_COMPLETE" Value="1" />
        </IGData>
    </FData>
</Event>
<Event OID="FOLLOWUPVISIT">
...
...
</Event>

What i want to achieve is for every event get the value of Item of attributeOID="I_IMAGE_UPLOAD_COMPLETE". This is what I have so far.

            XNamespace occ = "http://www.cdisc.org/ns/odm/v1.3";
            XNamespace aoc = "http://www.openclinica.org/ns/odm_ext_v130/v3.1";

            var datasubject = data_oc.Descendants(occ + "SData");
            var event = datasubject.Elements(occ + "Event").Attributes("OID");

            foreach (var items in event)
            {
                string EventTypes = items.Value.ToString();
                foreach (var itemData in EventTypes)
                {
                    var Data = from el in datasubject.Descendants(occ + "Event")
                               where (string)el.Attribute("OID=") == itemData
                               select(string)el;
                }
            }

Thank you.

var doc=XDocument.Parse(@"your xml");

//Via Linq
var viaLinq=doc.Elements()
  .SelectMany(e => e.Elements())
  .SelectMany(e => e.Elements())
  .SelectMany(e => e.Elements())
  .SelectMany(e => e.Elements())
  .Where(w => w.Attribute("OID").Value=="I_IMAGE_UPLOAD_XNAT")
  .Select(e => e.Attribute("Value").Value);

//Even better use Xpath. It makes all this very simple
var viaXPath=doc.XPathSelectElements("//Item[@OID='I_IMAGE_UPLOAD_XNAT']")
  .Select(e => e.Attribute("Value").Value);

Try this, not sure how it works good in terms of performance.

 XDocument po = XDocument.Load("XMLFile1.xml");
        // LINQ to XML query
        var list1 = po.Descendants("Event")
                        .Select(x =>
                            x.Descendants("Item").Where(t =>
                                (string)t.Attribute("OID") == "I_IMAGE_UPLOAD_COMPLETE").Select(m => 
                                    new { Event = x, AttributeValue = m.Attribute("Value") })
                                ).Where(f => f.FirstOrDefault() != null).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM