這裏顯示的數據是一些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
在這種順序。 ...
是你的XML有效的,因爲我沒有看到結束標記爲第一個設備 – Habib 2012-04-22 13:43:48
在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
是的,它是有效的縮短它,但所有的XML都有效。謝謝 – DarkWing 2012-04-22 14:06:48