1

我的代码如下:

XDocument xmlDoc = XDocument.Load("myXML.xml");

        var data = from items in xmlDoc.Root.Elements("category")
                   where items.Attribute("value").Value == "idxCategoryPlatformEngine"
                   select new
                   {
                       attribute = items.Element("attributes").Element("attribute").Element("name").Value,
                       trigger = items.Element("attributes").Element("attribute").Element("trigger").Value,
                       value = items.Element("attributes").Element("attribute").Element("value").Value,
                       minutes = items.Element("attributes").Element("attribute").Element("minutes").Value
                   };

我拥有的XML如下:

<?xml version="1.0" encoding="utf-8"?>
<GMS>
    <category value="idxCategoryPlatformEngine">
        <attributes>
            <attribute>
                <name>CheckpointFileCorruptionAlarm.InAlarm</name>
                <trigger>EQ</trigger>
                 <value>TRUE</value>
                 <minutes>0</minutes>
            </attribute>
            <attribute>
                 <name>CPULoad</name>
                <trigger>GT</trigger>
                <value>60</value>
                <minutes>5</minutes>
            </attribute>
            <attribute>
                <name>Engine.Historian.InStoreForward</name>
                <trigger>EQ</trigger>
                <value>TRUE</value>
                <minutes>0</minutes>
            </attribute>
        </attributes>
    </category>
    <category value="idxCategoryApplicationEngine">
        <attributes>
            <attribute>
                <name>CheckpointFileCorruptionAlarm.InAlarm</name>
                <trigger>EQ</trigger>
                 <value>TRUE</value>
                 <minutes>0</minutes>
            </attribute>
            <attribute>
                 <name>CPULoad</name>
                <trigger>GT</trigger>
                <value>60</value>
                <minutes>5</minutes>
            </attribute>
            <attribute>
                <name>Engine.Historian.InStoreForward</name>
                <trigger>EQ</trigger>
                <value>TRUE</value>
                <minutes>0</minutes>
            </attribute>
        </attributes>
    </category>
</GMS>

现在,当我运行代码时,它确实执行了查询,但是它只返回我真正想要所有属性的第一个属性。

我将不胜感激任何帮助,因为它让我发疯,我为尝试解决此问题所做的每一次更改只会导致更多问题!

4

3 回答 3

2

问题是通过向下选择到 select 语句中的属性元素,您只执行一次。换句话说,您将一个集合传递给 select,并且 select 正在运行诸如items.Element("attributes").Element("attribute").Element("name").Value获取单个值和单个匿名对象的语句。它应该看起来更像这样(有效):

var data2 = doc.Root.Elements("category")
    .Where(x => x.Attribute("value").Value == "idxCategoryPlatformEngine")
    .Elements("attributes")
    .Elements("attribute")
    .Select(x => new
    {
        attribute = x.Element("name").Value,
        trigger = x.Element("trigger").Value,
        value = x.Element("value").Value,
        minutes = x.Element("minutes").Value
    });

我对 Linq 的扩展语法更满意,但这可以很容易地转换为其他语法。

于 2013-05-17T12:55:37.830 回答
0

您应该在 中添加一个附加值selectselect new { }现在您只返回第一个值attribute

var data = from item in xmlDoc.Root.Elements("category")
           where item.Attribute("value").Value == "idxCategoryPlatformEngine"
           select new
               {
                   category = item.Attribute("value").Value,
                   attributes = from attr in item.Element("attributes")
                                select new {
                                   attribute = attr.Element("name").Value,
                                   trigger = attr.Element("trigger").Value,
                                   ...
                                }
               };
于 2013-05-17T12:56:59.197 回答
0

首先,您需要获取Attributes元素,然后迭代其Attribute下的所有元素。

你可以这样做:

  XDocument xmlDoc = XDocument.Load("myXML.xml");

  var data = (from items in xmlDoc.Root.Elements("category")
              where items.Attribute("value").Value == "idxCategoryPlatformEngine"
              select new {attrs  = items.Element("attributes")})
              .SelectMany(a => a.attrs.Elements("attribute"))
              .Select(item => new {
                       attribute = item.Element("name").Value,
                       trigger = item.Element("trigger").Value,
                       value = item.Element("value").Value,
                       minutes = item.Element("minutes").Value
              });
于 2013-05-17T12:57:45.407 回答