473,466 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to insert a node at a specific location in XML Doc

Hello.

Let's say I have the following XML document:

<ROOT><Element1>Some Text</Element1></ROOT>

I want to add a new element after Element1. I tried the following but,
naturally, it doesn't work:

Dim oElem as XmlElement
Dim oNode as XmlNode
Dim oRoot as XmlNode
Dim oDoc as New XmlDocument

oDoc.LoadXML("<ROOT><Element1>Some Text</Element1></ROOT>")

oRoot = oDoc.DocumentElement
oNode = oRoot.SelectSingleNode("//ROOT/Element1")

oElem = oDoc.CreateElement("InsertMe")
oElem.InnerText = "A New Element"
oRoot.InsertBefore(oElem, oNode) <----- This is the error

The error I get is: "The reference node is not a child of this node."

How do I go about inserting the new element at a specific point?

Thanks in advance,

Mike
Nov 12 '05 #1
4 12382
I meant to say "I want to add a new element "before" Element1.

"mike" <mi***@slomins.com> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...
Hello.

Let's say I have the following XML document:

<ROOT><Element1>Some Text</Element1></ROOT>

I want to add a new element after Element1. I tried the following but,
naturally, it doesn't work:

Dim oElem as XmlElement
Dim oNode as XmlNode
Dim oRoot as XmlNode
Dim oDoc as New XmlDocument

oDoc.LoadXML("<ROOT><Element1>Some Text</Element1></ROOT>")

oRoot = oDoc.DocumentElement
oNode = oRoot.SelectSingleNode("//ROOT/Element1")

oElem = oDoc.CreateElement("InsertMe")
oElem.InnerText = "A New Element"
oRoot.InsertBefore(oElem, oNode) <----- This is the error

The error I get is: "The reference node is not a child of this node."

How do I go about inserting the new element at a specific point?

Thanks in advance,

Mike

Nov 12 '05 #2

mike писал(а):
I meant to say "I want to add a new element "before" Element1.

"mike" <mi***@slomins.com> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...
Hello.

Let's say I have the following XML document:

<ROOT><Element1>Some Text</Element1></ROOT>

I want to add a new element after Element1. I tried the following but,
naturally, it doesn't work:

Dim oElem as XmlElement
Dim oNode as XmlNode
Dim oRoot as XmlNode
Dim oDoc as New XmlDocument

oDoc.LoadXML("<ROOT><Element1>Some Text</Element1></ROOT>")

oRoot = oDoc.DocumentElement
oNode = oRoot.SelectSingleNode("//ROOT/Element1")

oElem = oDoc.CreateElement("InsertMe")
oElem.InnerText = "A New Element"
oRoot.InsertBefore(oElem, oNode) <----- This is the error

The error I get is: "The reference node is not a child of this node."

How do I go about inserting the new element at a specific point?

Thanks in advance,

Mike


MSDN Library - XmlNode class InsertBefore method example:

Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample

Public Shared Sub Main()

Dim doc As New XmlDocument()
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"</book>")

Dim root As XmlNode = doc.DocumentElement

'Create a new node.
Dim elem As XmlElement = doc.CreateElement("price")
elem.InnerText = "19.95"

'Add the node to the document.
root.InsertBefore(elem, root.FirstChild)

Console.WriteLine("Display the modified XML...")
doc.Save(Console.Out)
End Sub 'Main
End Class 'Sample

I hope this helps you.

Best, Ed.

Nov 12 '05 #3
Hi, Ed. Thanks for the response.

I saw that example but I can't use the FirstChild property because, as the
XML doc grows (and it will grow), FirstChild may not be the place where I
want to insert an element.

I need to be able to insert an element anywhere in a document.

Any other suggestions?

Thanks again,

Mike

"Gomolyako Eduard" <ed**************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

mike ?????(?):
I meant to say "I want to add a new element "before" Element1.

"mike" <mi***@slomins.com> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...
Hello.

Let's say I have the following XML document:

<ROOT><Element1>Some Text</Element1></ROOT>

I want to add a new element after Element1. I tried the following but,
naturally, it doesn't work:

Dim oElem as XmlElement
Dim oNode as XmlNode
Dim oRoot as XmlNode
Dim oDoc as New XmlDocument

oDoc.LoadXML("<ROOT><Element1>Some Text</Element1></ROOT>")

oRoot = oDoc.DocumentElement
oNode = oRoot.SelectSingleNode("//ROOT/Element1")

oElem = oDoc.CreateElement("InsertMe")
oElem.InnerText = "A New Element"
oRoot.InsertBefore(oElem, oNode) <----- This is the error

The error I get is: "The reference node is not a child of this node."

How do I go about inserting the new element at a specific point?

Thanks in advance,

Mike


MSDN Library - XmlNode class InsertBefore method example:

Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample

Public Shared Sub Main()

Dim doc As New XmlDocument()
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"</book>")

Dim root As XmlNode = doc.DocumentElement

'Create a new node.
Dim elem As XmlElement = doc.CreateElement("price")
elem.InnerText = "19.95"

'Add the node to the document.
root.InsertBefore(elem, root.FirstChild)

Console.WriteLine("Display the modified XML...")
doc.Save(Console.Out)
End Sub 'Main
End Class 'Sample

I hope this helps you.

Best, Ed.
Nov 12 '05 #4
I figured it out:

oNode.ParentNode.InsertBefore(oElem, oNode)

"mike" <mi***@slomins.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi, Ed. Thanks for the response.

I saw that example but I can't use the FirstChild property because, as the
XML doc grows (and it will grow), FirstChild may not be the place where I
want to insert an element.

I need to be able to insert an element anywhere in a document.

Any other suggestions?

Thanks again,

Mike

"Gomolyako Eduard" <ed**************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

mike ?????(?):
I meant to say "I want to add a new element "before" Element1.

"mike" <mi***@slomins.com> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...
> Hello.
>
> Let's say I have the following XML document:
>
> <ROOT><Element1>Some Text</Element1></ROOT>
>
> I want to add a new element after Element1. I tried the following but,
> naturally, it doesn't work:
>
> Dim oElem as XmlElement
> Dim oNode as XmlNode
> Dim oRoot as XmlNode
> Dim oDoc as New XmlDocument
>
> oDoc.LoadXML("<ROOT><Element1>Some Text</Element1></ROOT>")
>
> oRoot = oDoc.DocumentElement
> oNode = oRoot.SelectSingleNode("//ROOT/Element1")
>
> oElem = oDoc.CreateElement("InsertMe")
> oElem.InnerText = "A New Element"
> oRoot.InsertBefore(oElem, oNode) <----- This is the error
>
> The error I get is: "The reference node is not a child of this node."
>
> How do I go about inserting the new element at a specific point?
>
> Thanks in advance,
>
> Mike
>
>


MSDN Library - XmlNode class InsertBefore method example:

Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample

Public Shared Sub Main()

Dim doc As New XmlDocument()
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"</book>")

Dim root As XmlNode = doc.DocumentElement

'Create a new node.
Dim elem As XmlElement = doc.CreateElement("price")
elem.InnerText = "19.95"

'Add the node to the document.
root.InsertBefore(elem, root.FirstChild)

Console.WriteLine("Display the modified XML...")
doc.Save(Console.Out)
End Sub 'Main
End Class 'Sample

I hope this helps you.

Best, Ed.

Nov 12 '05 #5

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

Similar topics

1
by: chiry | last post by:
Is there a way to make Element.addContent add the content at a specific location? I need to update an existing jdom object in memory, by adding/removing some elements. problem is, addContent()...
2
by: Susan Bricker | last post by:
Greetings! Is it possible to open a form in a specific location on the screen? I have three cascading forms ... fill one in and then open another ... fill that one in and open another. I want...
1
by: Linda Wienholt | last post by:
Hi I am trying to link to a specific location on a page using a named anchor. I also have some parameters attached to the query string. I am having problems getting the page to display the...
1
by: Li Pang | last post by:
Hi, I receive daily an email with a attached file from my Outlook inbox. This file must be saved into a specific location manually. I made an app to automate this process. My app can read the...
2
by: Craig | last post by:
I have the need to write a byte of information to a specific location in a text file. eg. the file looks something like this. FYYNN Line 1 Line 2 <eof>
0
by: sushilgajbhiye | last post by:
Hi, I am new in C# programming. I have saved some data to the database and after i retrieve it, i want to print it to the printer. I want to print that text to a specific location i.e. something...
2
by: yinglcs | last post by:
HI, Is it possible to emulate a mouse click event at a specific location in the browser using JavaScript? If yes, can you please tell me where can I find some example? Thank you.
10
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in...
1
by: billelev | last post by:
Here is some code that I have adapted slightly. It allows a report to be printed to a specific location. It works by calling SaveReportAsPDF and specifying the access report name, and the root...
0
by: shaif | last post by:
Hello Friends, I am facing a problem to create PDF from excel to specific location by using Adobe PDF Printer ib VB. I used code: Dim xlbook As Excel.Workbook . . . . xlbook.PrintOut , ,...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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 projectplanning, 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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.