[英]Reading Complex XML Attributes
I have an XML file that uses a combination of attributes and tags that have data in them. 我有一个XML文件,它使用包含数据的属性和标记的组合。
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文件的结构,但我正在转向查看LINQ以解析此问题,并想知道是否有关于如何最好地处理此结构的意见:
<?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. 我可以找到大量的LINQ代码示例,但我并没有真正看到我如何获得Next_Element和element_def“name”属性的值。
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>
. 此查询将返回匿名对象的序列,其中包含
Name
属性包含element_def
名称,下一个元素的名称为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. 它将返回
IEnumerable
的匿名对象,其中elDefName
将是element_def节点的name属性的值, nextEl
将包含可能的next_element值的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.