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

Krumbz

New Member
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\[code\]<?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>\[/code\]The C#-code I've got is this:\[code\]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;}\[/code\]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
 
Back
Top