472,146 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

InsertAfter does not work.

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/

Nov 12 '05 #1
2 9217
The node on which you call the InsertAfter() method should be the parent of
the reference node after which you want to add the new node.
Change xmlDoc.DocumentElement.InsertAfter(xmlSubCat, xmlNode); to the
following two lines:

XmlNode commonParent = xmlNode.ParentNode;

commonParent.InsertAfter(xmlSubCat, xmlNode);

Thanks,
Priya

"Sonu Kapoor" <vi*****@hotmail.com> wrote in message
news:b0****************************@phx.gbl...
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/

Nov 12 '05 #2
Thanks Priya,

it worked like a charm. I didnt know that I should also
take care the caller of InsertAfter(). It isnt very well
expained in the MSDN. Thanks again.

Sincerly, Sonu
My blog: http://weblogs.asp.net/sonukapoor/

-----Original Message-----
The node on which you call the InsertAfter() method should be the parent ofthe reference node after which you want to add the new node.Change xmlDoc.DocumentElement.InsertAfter(xmlSubCat, xmlNode); to thefollowing two lines:

XmlNode commonParent = xmlNode.ParentNode;

commonParent.InsertAfter(xmlSubCat, xmlNode);

Thanks,
Priya

"Sonu Kapoor" <vi*****@hotmail.com> wrote in message
news:b0****************************@phx.gbl...
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/

.

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by The Kiddie | last post: by
2 posts views Thread by Kael | last post: by
14 posts views Thread by Anoop | last post: by
reply views Thread by Michael J Martin | last post: by
14 posts views Thread by webEater | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.