JaggenSWE
JaggenSWE

Reputation: 2094

Stuck in a train of thought with looping XML in C#

I've recently switched focus from VB.NET to C# due to some decisions at work. I'm now stuck at one issue while trying to read out data from a XML file.

Here it goes, the XML looks like this

<?xml version="1.0" encoding="utf-8"?>
<shipmentdata>
    <shipment shipmentid="70716481780000102" messageid="2">
        <msgcreated>2012-06-14T10:44:00</msgcreated>
        <orderdate>2012-06-14</orderdate>
        <services>
            <baseservice ediid="ZG8">Replenishment</baseservice>
            <addedservices>
                <addedservice id="2" ediid="Z47">Installation</addedservice>
                <addedservice id="3" ediid="Z45">Swap</addedservice>
            </addedservices>
        </services>
        <weight uom="KGM">2</weight>
        <bulkref>123456</bulkref>
    </shipment>
</shipmentdata>

The C#-code I've got is this:

private DataSet ReturnServices(string FileName)
{
    XmlDocument m_xmld = new XmlDocument();
    XmlNodeList m_nodelist;
    m_xmld.Load(FileName);
    m_nodelist = m_xmld.SelectNodes("/shipmentdata");

    DataRow aDR;
    DataTable aDT = new DataTable("AddedServices");
    aDT.Columns.Add("ediid",typeof(string));
    aDT.Columns.Add("shipmentid",typeof(string));
    DataSet oDS = new DataSet("EDIinfo");

    foreach (XmlElement m_node in m_nodelist)
    {
        ShipmentID = m_node["shipment"].Attributes.
            GetNamedItem("shipmentid").Value.ToString();

        XmlNodeList s_nodelist = m_xmld.SelectNodes(
            "/shipmentdata/shipment/services/addedservices");

        foreach (XmlElement s_node in s_nodelist)
        {
            aDR = aDT.NewRow();
            aDR["ediid"] = s_node.ChildNodes.Item(0).Attributes.
                GetNamedItem("ediid").Value.ToString();
            aDR["shipmentid"] = ShipmentID;
            aDT.Rows.Add(aDR);
        }
    }
    oDS.Tables.Add(aDT);
    return oDS;
}

The trouble here is that while the file contain two Added Services (Z47 and Z45) but for some reason only the first node gets stored in the DataTable (Z47). How do I go about looping through all the addedservice nodes?

Any help would be appreciated, I've been stuck on this for some time now and started to realize that I'm probably stuck in a train of thought and logic which is hard to break out of. :P

Upvotes: 2

Views: 171

Answers (1)

Filip
Filip

Reputation: 2347

This loop

foreach (XmlElement s_node in s_nodelist)
{
      aDR = aDT.NewRow();
      aDR["ediid"] = s_node.ChildNodes.Item(0).Attributes.
          GetNamedItem("ediid").Value.ToString();
      aDR["shipmentid"] = ShipmentID;
      aDT.Rows.Add(aDR);
  }

only iterates once, as there is only one node returned by the "/shipmentdata/shipment/services/addedservices" xpath statement. Then you get the first addedservice node here aDR["ediid"] = s_node.ChildNodes.Item(0).Attributes. GetNamedItem("ediid").Value.ToString(); , but never go to the second. You probably meant to loop for those, too.

Upvotes: 2

Related Questions