473,402 Members | 2,046 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,402 software developers and data experts.

Insert / delete ?

Hi, im trying to first insert a node in the xml file and save it.

XmlDocument mDoc = new XmlDocument();
mDoc.Load("struct.xml");
XmlNode singleNode;
XmlNode newNode = mDoc.CreateNode(XmlNodeType.Element, "page", "");
XmlAttribute mAttr = mDoc.CreateAttribute("id");
singleNode = mDoc.SelectSingleNode("//page[@id=\"" + pageid.Value +
"\"]");

if (singleNode == null)
{
Response.Write("no node found");
return;
}
mAttr.Value = "100";
newNode.Attributes.Append(mAttr);

mDoc.DocumentElement.InsertAfter(newNode, singleNode);

i do find the node with ID number 3 and can retrive it contents, but i cant
insert a new node after it.
Here is the xml file:
<?xml version="1.0"?>
<home>
<page name="Test1" id="1">
<page name="Pagetest1" id="2" src="test1.htm"></page>
<page name="pagetest3" id="3" src="test2.htm"></page>
</page>
<page name="Test2" id="4" src="test1.htm">
<page name="Test23" id="7" src="test1.htm">
<page name="pagetest1" id="10" src="test1.htm"></page>
</page>
<page name="Pagetest1" id="5" src="test1.htm"></page>
<page name="pagetest2" id="6" src="test1.htm"></page>
</page>
</home>

The error message i am getting is:
Exception Details: System.ArgumentException: The reference node is not a
child of this node.

Source Error:

Line 67: newNode.Attributes.Append(mAttr);

Line 68:

Line 69: mDoc.DocumentElement.InsertAfter(newNode, singleNode);

Second question is, how do i delete a node ex. the node with ID nr 3? what
will happen if i delete node with id number 7?

-Patrick
Jun 28 '06 #1
2 1336


Patrick wrote:

singleNode = mDoc.SelectSingleNode("//page[@id=\"" + pageid.Value +
"\"]");

if (singleNode == null)
{
Response.Write("no node found");
return;
}
mAttr.Value = "100";
newNode.Attributes.Append(mAttr);

mDoc.DocumentElement.InsertAfter(newNode, singleNode);


Use
singleNode.ParentNode.InsertAfter(newNode, singleNode);
You always have to call AppendChild or InsertAfter or InsertBefore on
the new parent node where you want to append or insert the new child.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 28 '06 #2


Patrick wrote:

Second question is, how do i delete a node ex. the node with ID nr 3? what
will happen if i delete node with id number 7?


Select the node using some DOM or XPath approach (e.g. SelectSingleNode)
then do
node.ParentNode.RemoveChild(node)

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
1
by: nomorems | last post by:
I was trying to copy a range of records from a table to the same table with a small modification. I was wondering why this takes a long time compared to just doing a subselect from that table and...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
0
by: isaac2004 | last post by:
im trying to make a page that reads simple user information and posts them to a table. the user can edit, delete and add new records, through a form on the page.using SQLs modifications like...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
1
by: abhi81 | last post by:
Hello All, I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp....
4
by: =?Utf-8?B?UmljaA==?= | last post by:
On a form - I have a datagridview which is docked to the entire form. The datagridview allows users to Delete and/or Add Rows. On the Form_Load event I Fill the datagridview source table with a...
0
by: magnolia | last post by:
i created a trigger that will record the changes made to a table .everything works fine except the insert query.whenerever i try to insert a record it fires insert and update triger at the same time...
1
by: skanemupp | last post by:
in this program when using the "c"-button it deletes the last token entered. i want to delete the token after the mousecursor. lets say the string is: 12*(81**.5+12) and i put the cursor between...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.