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 (リスト ボックス) から取得していませんが、シナリオは私の問題に当てはまります。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 には、推定名と参照属性が必要です。

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 に答える