DarkWing
DarkWing

Reputation: 77

Reading and displaying data in a specific format

Here is some 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. The foreach will load in the lstPoints as follows:

RTU-GYM rstat/zone_temp Fri 17 Aug 2007 10:1:22:0 th true.....

And it will keep going through the all the way to the end of file. Then:

RTU-11 Rm 157F....

And will cycle through ALL the again before it will get another .

I need the lstPoints to display like this:

RTU-Gym rstat/zone_temp sfan


RTU-11 Rm 157F da_temp clg_stg1

In this order....

Upvotes: 0

Views: 115

Answers (2)

L.B
L.B

Reputation: 116098

Here is a Linq2Xml alternative

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

Upvotes: 0

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

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

Upvotes: 1

Related Questions