0

这是一些 XML:

<CHECKOUT>
   <EQUIPMENT path="#rtu1_130" name="RTU-1 Gym">
   <POINT ref="rstat/zone_temp">
     <notation />
     <date>Fri 17 Aug 2007</date>
     <time>10:1:22:0</time>
     <operator>th</operator>
     <done>true</done>
   </POINT>
   <POINT ref="sfan">
     <operator>th</operator>
     <done>true</done>
     <notation />
     <time>10:15:36:0</time>
     <date>Fri 17 Aug 2007</date>
   </POINT>
<EQUIPMENT path="#rtu11_130" name="RTU-11 Rm 157F">
   <POINT ref="da_temp">
     <done>true</done>
     <notation />
     <date>Mon 9 Jul 2007</date>
     <time>13:44:10:0</time>
     <operator>th</operator>
   </POINT>
   <POINT ref="clg_stg1">
     <notation />
     <done>true</done>
     <time>10:42:7:0</time>
     <date>Fri 17 Aug 2007</date>
     <operator>th</operator>
   </POINT>  
 </EQUIPMENT>
</CHECKOUT>

这是我的代码:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:/Users/David/Desktop/co.xml");
XmlNodeList lstEquip = xmlDoc.GetElementsByTagName("EQUIPMENT");
XmlNodeList lstPoint = xmlDoc.SelectNodes("/CHECKOUT/EQUIPMENT/POINT");

foreach (XmlNode node1 in lstEquip)
{
  XmlElement companyElement = (XmlElement)node1;
  lstPoints.Items.Add(companyElement.Attributes["name"].InnerText);

  foreach (XmlNode node2 in lstPoint)
  {
    XmlElement companyElement2 = (XmlElement)node2;
    lstPoints.Items.Add(companyElement2.Attributes["ref"].InnerText);

  }
  lstPoints.Items.Add("*******************");
}

这是一个“故障排除”应用程序,我不会在现实生活中通过 lstPoints(列表框)来处理这两个元素,但 senario 适用于我的问题。foreach 将按如下方式加载 lstPoints:

RTU-GYM rstat/zone_temp Fri 17 Aug 2007 10:1:22:0 th true.....

它将一直持续到文件末尾。然后:

RTU-11 Rm 157F....

并且会在它得到另一个之前循环一遍。

我需要 lstPoints 显示如下:

RTU-Gym rstat/zone_temp sfan


RTU-11 Rm 157F da_temp clg_stg1

按照这个顺序......

4

2 回答 2

1

xsd 中需要猜测的给定名称和 ref 属性

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:/Users/David/Desktop/co.xml");
foreach(XmlNode equipmentNode in xmlDoc.DocumentElement.SelectNodes("EQUIPMENT"))
{
  lstPoints.Items.Add(equipmentNode.Attributes["name"].Value);
  foreach(XmlNode pointNode in equipmentNode.SelectNodes("POINT"))
  {
    lstPoints.Items.Add(pointNode.Attributes["ref"]).Value);
  }
}
于 2012-04-22T15:25:49.457 回答
0

这是一个 Linq2Xml 替代方案

XDocument xDoc = XDocument.Load(....);
var equipments = xDoc.Descendants("EQUIPMENT")
                .Select(eq => new
                {
                    Name = eq.Attribute("name").Value,
                    Path = eq.Attribute("path").Value,
                    Points = eq.Descendants("POINT")
                            .Select(p=>new 
                            {
                                Ref = p.Attribute("ref"),
                                Operator = p.Element("operator").Value
                            })
                            .ToArray()

                })
                .ToArray();

-

foreach (var eq in equipments)
{
    Console.WriteLine(eq.Name);
    foreach (var p in eq.Points)
    {
        Console.WriteLine("\t" + p.Ref);
    }
}
于 2012-04-22T21:34:36.650 回答