473,382 Members | 1,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 9278
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
1
by: chris yoker | last post by:
hiya, I succesfully return a "nodeList" thru the "xmlDoc.SelectNodes" method. This nodeList is taken from a repetitive, uniform xml doc. I can successfully append a child node at the correct...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
4
by: The Kiddie | last post by:
Hi, I am having major headaches with XmlNode.InsertAfter. This is the format of my XML document <?xml version="1.0" encoding="utf-8"?> <Team name="Team"> <Players> <Player> <name> GK</name>...
2
by: Kael | last post by:
Hi, I’ve made a class to help me accomplish some things faster but I’m having problems inserting nodes. I’ve tried many different ways, but with no success. This is a very stripped down...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
0
by: Michael J Martin | last post by:
Any idea why PHP runkit does not allow you to extend a non-user-defined class? In PHP runkit_methods.c it seems to do a check and calls php_error_docref with E_WARNING ("class %s is not a...
14
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src=""...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.