2012-04-22 37 views
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适用于我的问题。

RTU-GYM RSTAT/zone_temp 星期五2007年8月17日 10:1:22: 在foreach将在lstPoints如下加载0 第 真.....

而且它将一直持续到文件结尾。然后:

RTU-11室157F ....

,并会循环使用所有再之前,它会得到另一个。

我需要lstPoints显示这样的:

RTU-健身房 RSTAT/zone_temp sfan


RTU-11室157F da_temp clg_stg1

在这种顺序。 ...

+0

是你的XML有效的,因为我没有看到结束标记为第一个设备 – Habib 2012-04-22 13:43:48

+0

在foreach将加载在以下列表中: RTU-GYM rstat/zone_temp 星期五17八月2007 10:1:22:0 th true .....并且它会一直持续经历一直到文件末尾。然后: RTU-11 Rm 157F ....并且将在所有再次循环,然后它将获得另一个<设备名称>。 我需要显示这样的lstPoints: RTU-健身房 RSTAT/zone_temp sfan *************** RTU-11室157F da_temp clg_stg1 在这个命令.... 我使用C#窗体。我希望你们能够伸出援手。在此先感谢 – DarkWing 2012-04-22 13:46:01

+0

是的,它是有效的缩短它,但所有的XML都有效。谢谢 – DarkWing 2012-04-22 14:06:48

回答

1

给定的名称和ref属性的猜测是必需的IRED在XSD

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); 
    } 
} 
+0

托尼,谢谢!对不起,我复制了XML,并缩短了文件,但它的合法性。你的答案是现货..谢谢! – DarkWing 2012-04-22 17:40:38

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); 
    } 
}