I have a C# program which appends a specific inner block of XML element to an existing XML file (the XML document has 457 lines), the data is coming from a hard coded xls spreadsheet. I'm using LINQ to XML with xml.Descendants(), and AddBeforeSelf() method. The program works fine only if I have two records to append, and when I upload 3 records or more than two, the new XML Element are not inserted on the right section, the 3rd and succeeding records are appending on the wrong block of inner Element.
The goal of my program is to get the records from xls spreadsheet with multiple records and insert them to specific inner XML node. For example, xml element of OrderLine is currently on line 110, the program should insert the new block before line 110, it works when I only have two records, but it won't work when I have more than two records.
Please see my XML Grid view, notice that there are 2 records under ReleaseLine, as well as under ShipUnit. The succeeding record must be inserted to the right node, or it must be nested under ReleaseLine and ShipUnit. I appreciate your help, thanks!
Here is my code sample:
public class EditOrder XElement xml = XElement.Load(@"C:\OrderTest.xml"); var childrens = xml.Descendants().ToArray(); var next_releaseline = childrens[110]; var next_shipunit = childrens[110]; Console.WriteLine(childrens[0]); //prints out the whole content next_orderline.AddBeforeSelf(new XElement("ReleaseLine", new XElement("ReleaseLineGid", new XElement("PackagedItemRef", new XElement("ItemQuantity",) newXelement(Test), ....))); next_shipunit.AddBeforeSelf(new XElement("ShipUnit", new XElement("ShipUnit", new XElement("WeightVolume", new XElement("ShipUnitContent",)))); xml.Save(@"C:\OrderTest.xml"); And I'm calling the EditOrder class from another class to execute the program.
//Call from another class to execute the XML insertion ProjectXML2.EditOrder.Main();
No comments:
Post a Comment