简体   繁体   中英

How to select a specific element using C#, Linq and XML?

I would like to read the five yellow marked ( Screenshot ) elements from an xml file.

...

Unfortunately, I don't get a result. But I also get no error. What am I doing wrong?

This is the XML File:

<?xml version='1.0' encoding='UTF-8'?>
<abc:ABC>
    <abc:Documentation>
        <abc:exporter>Exporter</abc:exporter>
    </abc:Documentation>
    <abc:Extension extender='Exporter v1.0'>
        <plugin pluginName='Exporter' pluginVersion='1.0'/>
    </abc:Extension>
    <def:DEF abc:type='def:Model' abc:id='eee_1045467100313_135436_1' name='Model'>
        <Element abc:type='def:Package' abc:id='32439355987_44278_14013' name='Package_'>
            <abc:Extension extender='Exporter 1.0'>
                <modelExtension>
                    <owned abc:type='def:' abc:id='848831_14014' name='Reuirement__' visibility='public' ownerOf='32439355987_44278_14013'>
                        <abc:Extension extender='Exporter 1.0'>
                            <Representation>
                                <daa:RepresentationObject ID='32439442709_154180_14035'>
                                    <Contents contentHash='8c30448ee161' exporterName='Exporter' exporterVersion='1.0' abc:id='_sbrrYY9GEeicdPE2nnAHIg'>
                                        <Objects href='#14684'/>
                                        <Elements>_371391_15071</Elements>
                                    </Contents>
                                </daa:RepresentationObject>
                            </Representation>
                        </abc:Extension>
                    </owned>
                </modelExtension>
            </abc:Extension>
            <Element abc:type='def:XYZ' abc:id='14056' name='0'/>
            <Element abc:type='def:XYZ' abc:id='14098' name='1a'/>
            <Element abc:type='def:XYZ' abc:id='14222' name='1b'/>
            <Element abc:type='def:XYZ' abc:id='14558' name='2a'/>
            <Element abc:type='def:XYZ' abc:id='14600' name='2b'/>
        </Element>
    </def:DEF>
</abc:ABC>

This is the Code in C#:

XNamespace abc = "abc";
XNamespace def = "def";

XDocument doc = XDocument.Load("Le.xml");
var result = from el in doc.Elements(abc + "ABC").Elements(def + "DEF").Elements()
                where el.Attribute(abc + "type").Value == "def:XYZ"
                select el;

foreach (var item in result.Elements())
{
    Console.WriteLine(item.Name + " = " + item.Value);
    Console.WriteLine("----------");
}

In your code XmlNamespaces abc, def and daa are not declared. Assuming in correct version they were declared at a top level node like this:

<?xml version='1.0' encoding='UTF-8'?>
<root xmlns:abc="abc" xmlns:def="def" xmlns:daa="daa">
<abc:ABC>
    <abc:Documentation>
        <abc:exporter>Exporter</abc:exporter>
    </abc:Documentation>
    <abc:Extension extender='Exporter v1.0'>
        <plugin pluginName='Exporter' pluginVersion='1.0'/>
    </abc:Extension>
    <def:DEF abc:type='def:Model' abc:id='eee_1045467100313_135436_1' name='Model'>
        <Element abc:type='def:Package' abc:id='32439355987_44278_14013' name='Package_'>
            <abc:Extension extender='Exporter 1.0'>
                <modelExtension>
                    <owned abc:type='def:' abc:id='848831_14014' name='Reuirement__' visibility='public' ownerOf='32439355987_44278_14013'>
                        <abc:Extension extender='Exporter 1.0'>
                            <Representation>
                                <daa:RepresentationObject ID='32439442709_154180_14035'>
                                    <Contents contentHash='8c30448ee161' exporterName='Exporter' exporterVersion='1.0' abc:id='_sbrrYY9GEeicdPE2nnAHIg'>
                                        <Objects href='#14684'/>
                                        <Elements>_371391_15071</Elements>
                                    </Contents>
                                </daa:RepresentationObject>
                            </Representation>
                        </abc:Extension>
                    </owned>
                </modelExtension>
            </abc:Extension>
            <Element abc:type='def:XYZ' abc:id='14056' name='0'/>
            <Element abc:type='def:XYZ' abc:id='14098' name='1a'/>
            <Element abc:type='def:XYZ' abc:id='14222' name='1b'/>
            <Element abc:type='def:XYZ' abc:id='14558' name='2a'/>
            <Element abc:type='def:XYZ' abc:id='14600' name='2b'/>
        </Element>
    </def:DEF>
</abc:ABC>
</root>

Then you could query those like:

XDocument doc = XDocument.Load("Le.xml");
var result = from el in doc.Descendants(abc + "ABC")
                           .Elements(def + "DEF")
                           .Descendants("Element")
             where el.Attribute(abc + "type") != null &&
                   (string)el.Attribute(abc + "type") == "def:XYZ"
             select el;
//               select new { 
//                  Id = (int)el.Attribute(abc+"id"),
//                  Name = (string)el.Attribute("name")
//               };
foreach (var item in result)
{
    foreach (var element in item.Attributes())
    {
        Console.WriteLine($"{element.Name}={element.Value}");
    }
    Console.WriteLine("------------");
}

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