简体   繁体   中英

Reading Complex XML Attributes

I have an XML file that uses a combination of attributes and tags that have data in them.

I don't have control over the structure of the XML file but I am currently switching gears to look at LINQ for parsing this and wondering if there are opinions on how best to approach this structure:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE call_flow SYSTEM "../../../../dtds/Callflow_1-1.dtd">
<call_flow version="1.1" serial="13903" first_element="Element1">
  <element_def name="Element1">
    <voice class="com.classname.Name">
      <static>Element1.xml</static>
    </voice>
    <exit_state name="done">
      <next_element>Element2</next_element>
    </exit_state>
  </element_def>
  <element_def name="Element2">
    <voice class="com.classname.Name2">
      <static>Element2.xml</static>
    </voice>
    <exit_state name="option1">
      <next_element>Element3</next_element>
    </exit_state>
    <exit_state name="option2">
      <next_element>Element4</next_element>
    </exit_state>
  </element_def>
  <element_def name="Element3">
    <voice class="com.classname.Name3">
      <static>Element3.xml</static>
    </voice>
    <exit_state name="done">
      <next_element>Element4</next_element>
    </exit_state>
  </element_def>
  <element_def name="Element4">
    <decision>
      <class src="com.classname.Name4"/>
    </decision>
    <exit_state name="0">
      <next_element>Element3</next_element>
    </exit_state>
    <exit_state name="1">
      <next_element>Element5</next_element>
    </exit_state>
  </element_def>
  <element_def name="Element5">
    <voice class="com.classname.Name5">
      <static>element5.xml</static>
    </voice>
  </element_def>
</call_flow>

I can find plenty of samples of LINQ code but am not really seeing how I'd get both the values for Next_Element and the element_def "name" property.

If someone can assist and maybe point me in the right direction, I'd appreciate it.

XDocument xdoc = XDocument.Load(path_to_xml);
var query = from ed in xdoc.Descendants("element_def")
            select new
            {
               Name = (string)ed.Attribute("name"),
               NextElements = ed.Elements("exit_state")
                                .Select(es => (string)es.Element("next_element"))
            };

This query will return sequence of anonymous objects, which will have Name property containing element_def name, and names of next elements as IEnumerable<string> .

What about this:

var doc =  XDocument.Load("myXml.xml");
var list = doc.Descendants("element_def")
              .Select(x => new
              {
                 elDefName = x.Attribute("name").Value,
                 nextEl = x.Descendants("next_element").Select(y=> y.Value).ToList()
              });

It will return an IEnumerable of anonymous objects, where elDefName will be value of the name attribute of the element_def node and nextEl will contain list of possible next_element values.

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