473,796 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ 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.DocumentEl ement
oNode = oRoot.SelectSin gleNode("//ROOT/Element1")

oElem = oDoc.CreateElem ent("InsertMe")
oElem.InnerText = "A New Element"
oRoot.InsertBef ore(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 12395
I meant to say "I want to add a new element "before" Element1.

"mike" <mi***@slomins. com> wrote in message
news:uQ******** ******@tk2msftn gp13.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.DocumentEl ement
oNode = oRoot.SelectSin gleNode("//ROOT/Element1")

oElem = oDoc.CreateElem ent("InsertMe")
oElem.InnerText = "A New Element"
oRoot.InsertBef ore(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******** ******@tk2msftn gp13.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.DocumentEl ement
oNode = oRoot.SelectSin gleNode("//ROOT/Element1")

oElem = oDoc.CreateElem ent("InsertMe")
oElem.InnerText = "A New Element"
oRoot.InsertBef ore(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("<b ook genre='novel' ISBN='1-861001-57-5'>" & _
"<title>Pri de And Prejudice</title>" & _
"</book>")

Dim root As XmlNode = doc.DocumentEle ment

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

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

Console.WriteLi ne("Display the modified XML...")
doc.Save(Consol e.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.goo glegroups.com.. .

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

"mike" <mi***@slomins. com> wrote in message
news:uQ******** ******@tk2msftn gp13.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.DocumentEl ement
oNode = oRoot.SelectSin gleNode("//ROOT/Element1")

oElem = oDoc.CreateElem ent("InsertMe")
oElem.InnerText = "A New Element"
oRoot.InsertBef ore(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("<b ook genre='novel' ISBN='1-861001-57-5'>" & _
"<title>Pri de And Prejudice</title>" & _
"</book>")

Dim root As XmlNode = doc.DocumentEle ment

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

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

Console.WriteLi ne("Display the modified XML...")
doc.Save(Consol e.Out)
End Sub 'Main
End Class 'Sample

I hope this helps you.

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

oNode.ParentNod e.InsertBefore( oElem, oNode)

"mike" <mi***@slomins. com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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.goo glegroups.com.. .

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

"mike" <mi***@slomins. com> wrote in message
news:uQ******** ******@tk2msftn gp13.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.DocumentEl ement
> oNode = oRoot.SelectSin gleNode("//ROOT/Element1")
>
> oElem = oDoc.CreateElem ent("InsertMe")
> oElem.InnerText = "A New Element"
> oRoot.InsertBef ore(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("<b ook genre='novel' ISBN='1-861001-57-5'>" & _
"<title>Pri de And Prejudice</title>" & _
"</book>")

Dim root As XmlNode = doc.DocumentEle ment

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

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

Console.WriteLi ne("Display the modified XML...")
doc.Save(Consol e.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
2774
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() always add at the end, and as a result the resulting XML object doesn't validate against the XSD file. Is there a way to add an element at a specific location?
2
5635
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 to keep each previous form open and position the forms such that the user can see all three (overlapping somewhat). Also, I want to make them modal (can't get focus to the previous form until the current form is closed). Is this possible? Is...
1
1880
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 correct portion of the page when it loads I have a few named anchors on the page using the following format <a name="branding"></a>Then some text here. In the URL I have http://mysite.com.au/default.aspx?id=1#branding when I load this page it does...
1
1423
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 emails from the inbox folder, I can catch all properties of the emails. I want to know how to save the attached file into a specific location.
2
3051
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
1229
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 like (x,y) location. Can anyone tell me, how to do this? Sushil
2
2218
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
10510
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 between then what is the best solution.
1
6254
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 directory to which the filename should be saved. The routine adjusts registry values to achieve its aim. Now, it works, but not quite as I would like it. I originally thought that I would be able to adjust a registry key (such as...
0
1419
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, 0, "Adobe PDF", True, True, "C:\Test.pdf"
0
9527
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10453
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10223
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.