简体   繁体   English

如何编写单个LINQ to XML查询以遍历所有子元素和子元素的所有属性?

[英]How to write a single LINQ to XML query to iterate through all the child elements & all the attributes of the child elements?

I am developing asp.net mobile application. 我正在开发asp.net移动应用程序。 I am using XML as a database. 我正在使用XML作为数据库。 I am querying on the XML to access the required elements & attributes by using LINQ to XML in .net. 我正在查询XML,以通过.net中的LINQ to XML访问所需的元素和属性。 I have the follwing part in my XML file. 我的XML文件中包含以下内容。

<VALVES>
     <VALVE NAME="PWV">
      <DISPLAY-NAME>
       Production Master Valve
      </DISPLAY-NAME>
      <COMMANDS USE-TEMPLATE="TRUE" TEAMPLATE-NAME="ValveCommands.xml"></COMMANDS>
    </VALVE>
    <VALVE NAME="PMV" >
     <DISPLAY-NAME>
      Production Wing Valve
     </DISPLAY-NAME>
     <COMMANDS USE-TEMPLATE="TRUE" TEAMPLATE-NAME="ValveCommands.xml"></COMMANDS>
    </VALVE>
</VALVES>

In the above XML file I can access the the child element node 'VALVE' & its attribute 'NAME' dynamically without explicitly specifying the name of the attribute by using following code. 在上面的XML文件中,我可以使用以下代码动态访问子元素节点“ VALVE”及其属性“ NAME”,而无需显式指定属性的名称。 Thus as any new attribute is added in the child node 'VALVE' I can access it without modifying my existing code. 因此,由于在子节点“ VALVE”中添加了任何新属性,因此无需修改现有代码就可以访问它。 For such logic I am using following code. 对于这种逻辑,我正在使用以下代码。 In the following code ValvesProperties is the Hashtable which is defined in the class 'Valves'. 在以下代码中,ValvesProperties是哈希表,该哈希表在类“阀门”中定义。

 static int count = 0;
 List<Valves> LstValves = new List<Valves>();

 string AttName = null;
 string AttValue = null;

 XDocument FieldDef = XDocument.Load(@"F:\Shailesh from user OPC\XML\KK3.xml");
 XElement FieldRoot = FieldDef.Element("KK");

 count = FieldRoot.Element("FIELD-DEFINITION").Element("VALVES").Elements("VALVE").Count();
 Valves[] Valveobj = new Valves[count];

 count = 0;

 foreach (var element in FieldRoot.Element("FIELD-DEFINITION").Element("VALVES").Elements())
 {
     Valveobj[count] = new Valves();

     foreach (var attribute in element.Attributes())
     {
         AttName = attribute.Name.ToString();
         AttValue = attribute.Value.ToString();
         Valveobj[count].ValvesProperties.Add(AttName, AttValue);
         LstValves.Add(Valveobj[count]);
     }

     count++;
  }

return LstValves;

The similar logic I need for the above defined XML part. 对于上面定义的XML部分,我需要类似的逻辑。 In the above XML I want to write the LINQ to XML query which can access the NAME attribute of the 'VALVE' node ( <VALVE NAME="PWV"> ), then it should access the text between 'DISPLAY-NODE' ( <DISPLAY-NAME> Production Master Valve </DISPLAY-NAME> ), & then it should access the all the attributes of the 'COMMAND' node dynamically ( <COMMANDS USE-TEMPLATE="TRUE" TEAMPLATE-NAME="ValveCommands.xml"></COMMANDS> ). 在上面的XML中,我想编写LINQ to XML查询,它可以访问'VALVE'节点的NAME属性( <VALVE NAME="PWV"> ),然后它应该访问'DISPLAY-NODE'( <DISPLAY-NAME> Production Master Valve </DISPLAY-NAME> ),然后它应动态访问“ COMMAND”节点的所有属性( <COMMANDS USE-TEMPLATE="TRUE" TEAMPLATE-NAME="ValveCommands.xml"></COMMANDS> )。 All these I want dynamically without explicitly specifying the name of the child node as well as name of their attributes ( similar to the way I written the above query ) Can we write such a code by using LINQ to XML ? 我要动态地进行所有这些操作,而无需显式指定子节点的名称及其属性的名称(类似于我编写上述查询的方式),是否可以使用LINQ to XML编写这样的代码? It will be better if we can write code for above issue in a single logic ( similar to the way I written the above query ). 如果我们可以用单个逻辑为上述问题编写代码,那会更好(类似于我编写上述查询的方式)。 Can you provide me the any code or link through which I can resolve the above issue ? 您能提供解决上述问题的任何代码或链接吗?

You could just iterate through all the elements and retrieve the attributes for each: 您可以遍历所有元素并检索每个元素的属性:

var allDescendants = myElement.DescendantsAndSelf();
var allDescendantsWithAttributes = allDescendants.SelectMany(elem =>
    new[] { elem }.Concat(elem.Attributes().Cast<XContainer>()));

foreach (XContainer elementOrAttribute in allDescendantsWithAttributes)
{
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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