简体   繁体   English

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

[英]Reading and displaying data in a specific format

Here is some XML: 这是一些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>

Here is my code: 这是我的代码:

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("*******************");
}

This is a "troubleshooting" app, I am not taking both elements through the lstPoints (list box) in real life but the senario applies for my problem. 这是一个“故障排除”应用程序,我在现实生活中并没有通过lstPoints(列表框)同时使用这两个元素,但是senario适用于我的问题。 The foreach will load in the lstPoints as follows: foreach将在lstPoints中加载,如下所示:

RTU-GYM rstat/zone_temp Fri 17 Aug 2007 10:1:22:0 th true..... RTU-GYM rstat / zone_temp 2007年8月17日星期五10:1:22:0真的.....

And it will keep going through the all the way to the end of file. 并且它将一直贯穿到文件结尾。 Then: 然后:

RTU-11 Rm 157F.... RTU-11 Rm 157F ....

And will cycle through ALL the again before it will get another . 并且在获得另一个循环之前将循环遍历所有循环。

I need the lstPoints to display like this: 我需要lstPoints像这样显示:

RTU-Gym rstat/zone_temp sfan RTU-Gym rstat / zone_temp风扇


RTU-11 Rm 157F da_temp clg_stg1 RTU-11 Rm 157F da_temp clg_stg1

In this order.... 按此顺序...

A guess given name and ref attributes are required in the xsd 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);
  }
}

Here is a Linq2Xml alternative 这是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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM