刚在CSDN上回答了一个问题,感觉蛮有代表性。原文地址:http://bbs.csdn.net/topics/390798406?page=1#post-397486465
所以在这里开题说说。
问题是分别想取下面数据
<Name>测试1</Name>中的<InnerValue>这是测试1</InnerValue>的值并放在一个TEXTBox1中,
<Name>测试2</Name>中的<InnerValue>这是测试2</InnerValue>的值并放在一个TEXTBox2中
应该如何一次搞定呢?
有以下XML文件数据
<?xml version="1.0" encoding="utf-8"?> <XTextDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EditorVersionString="1.1.1820.30544"> <Attributes /> <XElements> <Element xsi:type="XTextBody" StyleIndex="0"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XString"> <Text>测试</Text> </Element> <Element xsi:type="XParagraphFlag" /> <Element xsi:type="XTextTable" NumOfRows="3" NumOfColumns="3"> <ID /> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XTextTableRow"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XTextTableCell" StyleIndex="1"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XParagraphFlag" /> </XElements> </Element> <Element xsi:type="XTextTableCell" StyleIndex="1"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XParagraphFlag" /> </XElements> </Element> <Element xsi:type="XTextTableCell" StyleIndex="1"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XParagraphFlag" /> </XElements> </Element> </XElements> </Element> <Element xsi:type="XTextTableRow"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XTextTableCell" StyleIndex="1"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XParagraphFlag" /> </XElements> </Element> <Element xsi:type="XTextTableCell" StyleIndex="1"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XParagraphFlag" /> </XElements> </Element> <Element xsi:type="XTextTableCell" StyleIndex="1"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XParagraphFlag" /> </XElements> </Element> </XElements> </Element> <Element xsi:type="XTextTableRow"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XTextTableCell" StyleIndex="1"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XParagraphFlag" /> </XElements> </Element> <Element xsi:type="XTextTableCell" StyleIndex="1"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XParagraphFlag" /> </XElements> </Element> <Element xsi:type="XTextTableCell" StyleIndex="1"> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XParagraphFlag" /> </XElements> </Element> </XElements> </Element> </XElements> <Columns> <Element xsi:type="XTextTableColumn"> <Width>624.9167</Width> </Element> <Element xsi:type="XTextTableColumn"> <Width>624.9167</Width> </Element> <Element xsi:type="XTextTableColumn"> <Width>624.9167</Width> </Element> </Columns> </Element> <Element xsi:type="XInputField"> <ID>001</ID> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XString"> <Text>这是测试1</Text> </Element> </XElements> <SpecifyWidth>1181.10242</SpecifyWidth> <EventExpressions /> <Name>测试一</Name> <ValueBinding> <DataSource>Info</DataSource> <BindingPath>测试一</BindingPath> </ValueBinding> <ValidateStyle /> <InnerValue>这是测试1</InnerValue> <FieldSettings> <ListSource> <Items /> </ListSource> </FieldSettings> </Element> <Element xsi:type="XString"> <Text>测试</Text> </Element> <Element xsi:type="XParagraphFlag" /> <Element xsi:type="XInputField"> <ID>002</ID> <Expressions /> <ScriptItems /> <Attributes /> <XElements> <Element xsi:type="XString"> <Text>这是测试2</Text> </Element> </XElements> <Deleteable>false</Deleteable> <SpecifyWidth>590.5512</SpecifyWidth> <EventExpressions /> <Name>测试2</Name> <ValueBinding> <DataSource>Info</DataSource> <BindingPath>测试2</BindingPath> </ValueBinding> <ValidateStyle /> <InnerValue>这是测试2</InnerValue> <ToolTip>测试2</ToolTip> <FieldSettings> <ListSource> <Items /> </ListSource> </FieldSettings> </Element> </XElements> </Element> <Element xsi:type="XTextHeader" StyleIndex="0"> <Expressions /> <ScriptItems /> <Attributes /> <XElements /> </Element> <Element xsi:type="XTextFooter" StyleIndex="0"> <Expressions /> <ScriptItems /> <Attributes /> <XElements /> </Element> </XElements> <UserHistories /> <Comments /> <LocalConfig /> <PageSettings /> </XTextDocument>
由于嵌套节点过多,再加上有<Element xsi:type="XTextBody" StyleIndex="0"> (BTW:节点的命名空间的,还可以通过别名来指定。)这样的节点信息,一般情况下用Xpath只要用Descendants,或者Element查找根下的所有节点,再用Attribute排除掉不想要的就可以了。
对于这样有namespace的XML,就必须在代码里指定要查询节点的Namespace。
代码:
XElement xmlDoc = XElement.Load(@"../../XMLFile1.xml"); XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; var getXML = from p in xmlDoc.Descendants("XElements").Descendants("Element"). Descendants("XElements").Descendants("Element") where p.Attribute(xsi + "type").Value == "XInputField" select new { NodeName = p.Element("Name"),NodeValue = p.Element("InnerValue") }; List<LintToXml> lst = new List<LintToXml>();//可以放在lst里面 LintToXml xx = new LintToXml(); StringBuilder sb = new StringBuilder();//存放在字符里 foreach (var item in getXML) { //xx.Name = item.NodeName.Value; <code class="csharp plain"></code><code class="csharp comments">//第一次循环输出测试一第二次测试二</code> //xx.InnerValue = item.NodeValue.Value;<code class="csharp comments">//第一次循环输出这是测试1第二次这是测试2</code> sb.Append("Name" + item.NodeName.Value + ",NodeValue" + item.NodeValue.Value+"<br/>"); } public class LintToXml { public string Name { get; set; } public string InnerValue { get; set; } }
其实上面也可以使用序列化,反序列化的来实现,。但是由于这个XML嵌套的节点过深,么有实现
参考序列化,反序列化文章:http://kb.cnblogs.com/page/177101/
还可以用IEnumerable接口来获取所有元素
IEnumerable<XElement> elementCollection = from p in xmlDoc.Descendants("XElements").Descendants("Element"). Descendants("XElements").Descendants("Element") where p.Attribute(xsi + "type").Value == "XInputField" select p; var test = from p in elementCollection select new { NodeName = p.Element("Name"),NodeValue = p.Element("InnerValue") }; foreach(var item in test) { var ss=item.NodeValue; var dd=item.NodeName; }
资料参考:http://blog.csdn.net/duanzi_peng/article/details/24018431
XElement.Descendants 方法 http://msdn.microsoft.com/zh-tw/library/system.xml.linq.xelement.descendants
XElement.Elements 方法 http://msdn.microsoft.com/zh-tw/library/system.xml.linq.xelement.elements%28v=vs.110%29.aspx