使用LINQ读取XML数据,使用同名的多个元素

时间:2022-04-03 16:51:56

Visual Studio 2010, Silverlight 4, and C#. I have the following data stored in an XML file:

Visual Studio 2010, Silverlight 4和c#。我有以下数据存储在一个XML文件中:

<root>
      <element>TextHere</element>
      <element>TextHere</element>
      <element>TextHere</element>
</root>

This is my current code.

这是我当前的代码。

XDocument xmlDoc = XDocument.Load("XMLDocument.xml");
var ElementsList = from Elements in xmlDoc.Descendants("root")
                   select new
                   {
                       ElementContent = Elements.Element("Element").Value,
                   };

This code only puts the very first element in the list, leaving all of the others out. How can I rewrite this code so that it will capture ALL of the elements that are named "element" in the XML file?

这段代码只将列表中的第一个元素放入其中,而将其他所有元素都排除在外。如何重写这段代码,使它能够捕获XML文件中所有命名为“element”的元素?

2 个解决方案

#1


5  

This would do it:

这将这么做:

XDocument xmlDoc = XDocument.Load("XMLDocument.xml");
var ElementsList = from Elements in xmlDoc.Descendants("element")
                   select new
                   {
                       ElementContent = Elements.Value
                   };

Or a little more succinct in dot notation:

或者用点符号更简洁一些

var ElementsList = xmlDoc.Descendants("element")
                         .Select(x => new { ElementContent = x.Value });

Note however that you only have an enumeration of elements after this, if you want a list (as your variable name suggests) you can add a .ToList() after the Select:

但是请注意,如果您想要一个列表(如您的变量名所示),您可以在Select之后添加.ToList():

var ElementsList = xmlDoc.Descendants("element")
                         .Select(x => new { ElementContent = x.Value })
                         .ToList();

This will list will contain 3 elements (based on your example XML. ) of an anonymous type that has a ElementContent property. If you do not need that property (and I would think you don't) this is a simplified version that just returns a list of string:

这个列表将包含3个元素(基于您的示例XML)。一个具有ElementContent属性的匿名类型。如果您不需要该属性(我认为您不需要),这是一个简单的版本,只返回一个字符串列表:

var ElementsList = xmlDoc.Descendants("element")
                         .Select(x => x.Value)
                         .ToList();

#2


2  

This would do it-

这将做它

 XDocument xmlDoc = XDocument.Load("XMLDocument.xml");
 var ElementsList = from Elements in xmlDoc.Descendants("root")
 select new
 {
   Element1 = (string)Elements.Element("element"),
   Element2 = Elements.Element("element").ElementsAfterSelf("element").First().Value,
   Element3 = Elements.Element("element").ElementsAfterSelf("element").ElementAt(1).Value,
 };

#1


5  

This would do it:

这将这么做:

XDocument xmlDoc = XDocument.Load("XMLDocument.xml");
var ElementsList = from Elements in xmlDoc.Descendants("element")
                   select new
                   {
                       ElementContent = Elements.Value
                   };

Or a little more succinct in dot notation:

或者用点符号更简洁一些

var ElementsList = xmlDoc.Descendants("element")
                         .Select(x => new { ElementContent = x.Value });

Note however that you only have an enumeration of elements after this, if you want a list (as your variable name suggests) you can add a .ToList() after the Select:

但是请注意,如果您想要一个列表(如您的变量名所示),您可以在Select之后添加.ToList():

var ElementsList = xmlDoc.Descendants("element")
                         .Select(x => new { ElementContent = x.Value })
                         .ToList();

This will list will contain 3 elements (based on your example XML. ) of an anonymous type that has a ElementContent property. If you do not need that property (and I would think you don't) this is a simplified version that just returns a list of string:

这个列表将包含3个元素(基于您的示例XML)。一个具有ElementContent属性的匿名类型。如果您不需要该属性(我认为您不需要),这是一个简单的版本,只返回一个字符串列表:

var ElementsList = xmlDoc.Descendants("element")
                         .Select(x => x.Value)
                         .ToList();

#2


2  

This would do it-

这将做它

 XDocument xmlDoc = XDocument.Load("XMLDocument.xml");
 var ElementsList = from Elements in xmlDoc.Descendants("root")
 select new
 {
   Element1 = (string)Elements.Element("element"),
   Element2 = Elements.Element("element").ElementsAfterSelf("element").First().Value,
   Element3 = Elements.Element("element").ElementsAfterSelf("element").ElementAt(1).Value,
 };