Hey Guys,
I am trying to insert a xml elmement right after specific
MainCategory element in the following xml file:
<?xml version="1.0" encoding="utf-8"?>
<CategoryList>
<Category>
<MainCategory ID="3">XML</MainCategory>
<Description>List of XML articles.</Description>
<Active>Yes</Active>
</Category>
<Category>
<MainCategory ID="2">C#</MainCategory>
<Description>List of C# articles.</Description>
<Active>Yes</Active>
</Category>
<Category>
<MainCategory ID="1">VC++</MainCategory>
<Description>A list of VC</Description>
<Active>Yes</Active>
</Category>
</CategoryList>
My code looks like:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../dbase/categories.xml"));
string xmlQuery="//CategoryList/Category/MainCategory
[@ID='"+ddlMainCategory.SelectedValue.ToString()+"']";
XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlQuery);
XmlElement xmlSubCat = xmlDoc.CreateElement
("SubCategory");
xmlSubCat.InnerText = txbSubCategories.Text;
xmlDoc.DocumentElement.InsertAfter(xmlSubCat, xmlNode);
xmlDoc.Save(Server.MapPath("../dbase/categories.xml"));
The XPath expression works perfectly and it also returns
the correct node ( I tested it ). But whenever I run the
code I got the following error message.
"System.ArgumentException: The reference node is not a
child of this node."
Can somebody tell me what I did wrong ?
Thanks
Sonu
My blog: http://weblogs.asp.net/sonukapoor/