Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 01, 2012 06:18 AM by Pankaj.Sharma
Member
65 Points
67 Posts
Mar 01, 2012 05:05 AM|LINK
Error : Cannot insert a node or any ancestor of that node as a child of itself.
XmlDocument doc = new XmlDocument(); foreach (DataRow row in dt.Rows) { XmlElement xElemRoot = doc.CreateElement("OutLet"); xElemRoot.SetAttribute("outlet",row[3].ToString()); doc.AppendChild(xElemRoot); XmlNode xItemCode = doc.CreateElement("ItemCode"); xItemCode.InnerText = row[1].ToString(); xElemRoot.AppendChild(xItemCode); XmlNode xDesc = doc.CreateElement("ItemDesc"); xDesc.InnerText = row[2].ToString(); xElemRoot.AppendChild(xDesc); doc.AppendChild(xElemRoot); XmlNode xQuantity = doc.CreateElement("Quantity"); xQuantity.InnerText = row[3].ToString(); xElemRoot.AppendChild(xQuantity); doc.DocumentElement.InsertAfter(xElemRoot, doc.DocumentElement.LastChild); } doc.Save(@"C:\test.xml");
Any one can guide me , thanks
Contributor
2350 Points
387 Posts
Mar 01, 2012 06:18 AM|LINK
Modify your code as follows -
XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateElement("Outlets")); //This will create DocumentElement. foreach (DataRow row in dt.Rows) { XmlElement xElemRoot = doc.CreateElement("OutLet"); xElemRoot.SetAttribute("outlet",row[3].ToString()); //doc.AppendChild(xElemRoot); doesn't needed as it is being added in the last line. XmlNode xItemCode = doc.CreateElement("ItemCode"); xItemCode.InnerText = row[1].ToString(); xElemRoot.AppendChild(xItemCode); XmlNode xDesc = doc.CreateElement("ItemDesc"); xDesc.InnerText = row[2].ToString(); xElemRoot.AppendChild(xDesc); //doc.AppendChild(xElemRoot); invalid line XmlNode xQuantity = doc.CreateElement("Quantity"); xQuantity.InnerText = row[3].ToString(); xElemRoot.AppendChild(xQuantity); doc.DocumentElement.InsertAfter(xElemRoot, doc.DocumentElement.LastChild); } doc.Save(@"C:\test.xml");
It will create a xml as shown below -
<Outlets> <Outlet outlet="1"> <Itemcode>Item1</Itemcode> <Itemdesc>Item1 description</Itemdesc>Quantity <Quantity>25</Quantity> </Outlet> <Outlet outlet="2"> <Itemcode>Item2</Itemcode> <Itemdesc>Item2 description</Itemdesc>Quantity <Quantity>50</Quantity> </Outlet> </Outlets>
Hope this helps.
Alex Ngan
Member
65 Points
67 Posts
XML
Mar 01, 2012 05:05 AM|LINK
Error : Cannot insert a node or any ancestor of that node as a child of itself.
XmlDocument doc = new XmlDocument(); foreach (DataRow row in dt.Rows) { XmlElement xElemRoot = doc.CreateElement("OutLet"); xElemRoot.SetAttribute("outlet",row[3].ToString()); doc.AppendChild(xElemRoot); XmlNode xItemCode = doc.CreateElement("ItemCode"); xItemCode.InnerText = row[1].ToString(); xElemRoot.AppendChild(xItemCode); XmlNode xDesc = doc.CreateElement("ItemDesc"); xDesc.InnerText = row[2].ToString(); xElemRoot.AppendChild(xDesc); doc.AppendChild(xElemRoot); XmlNode xQuantity = doc.CreateElement("Quantity"); xQuantity.InnerText = row[3].ToString(); xElemRoot.AppendChild(xQuantity); doc.DocumentElement.InsertAfter(xElemRoot, doc.DocumentElement.LastChild); } doc.Save(@"C:\test.xml");Any one can guide me , thanks
Pankaj.Sharm...
Contributor
2350 Points
387 Posts
Re: XML
Mar 01, 2012 06:18 AM|LINK
Modify your code as follows -
XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateElement("Outlets")); //This will create DocumentElement. foreach (DataRow row in dt.Rows) { XmlElement xElemRoot = doc.CreateElement("OutLet"); xElemRoot.SetAttribute("outlet",row[3].ToString()); //doc.AppendChild(xElemRoot); doesn't needed as it is being added in the last line. XmlNode xItemCode = doc.CreateElement("ItemCode"); xItemCode.InnerText = row[1].ToString(); xElemRoot.AppendChild(xItemCode); XmlNode xDesc = doc.CreateElement("ItemDesc"); xDesc.InnerText = row[2].ToString(); xElemRoot.AppendChild(xDesc); //doc.AppendChild(xElemRoot); invalid line XmlNode xQuantity = doc.CreateElement("Quantity"); xQuantity.InnerText = row[3].ToString(); xElemRoot.AppendChild(xQuantity); doc.DocumentElement.InsertAfter(xElemRoot, doc.DocumentElement.LastChild); } doc.Save(@"C:\test.xml");It will create a xml as shown below -
<Outlets>
<Outlet outlet="1">
<Itemcode>Item1</Itemcode>
<Itemdesc>Item1 description</Itemdesc>Quantity
<Quantity>25</Quantity>
</Outlet>
<Outlet outlet="2">
<Itemcode>Item2</Itemcode>
<Itemdesc>Item2 description</Itemdesc>Quantity
<Quantity>50</Quantity>
</Outlet>
</Outlets>
Hope this helps.