以特定格式读取和显示数据

时间:2012-04-22 13:40:26

标签: c# xml winforms

这是一些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 2007年8月17日星期五 10:1:22:0 日 真.....

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

RTU-11 Rm 157F ....

并且在它将获得另一个之前将再次循环通过ALL。

我需要像这样显示lstPoints:

RTU-健身房 RSTAT / zone_temp sfan


RTU-11 Rm 157F da_temp clg_stg1

按此顺序......

2 个答案:

答案 0 :(得分:1)

xsd

中需要给出name和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);
  }
}

答案 1 :(得分: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);
    }
}