473,382 Members | 1,204 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.

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 12379
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: 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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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: 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...

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.