Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

write to XML

Posted on 2004-08-08
4
Medium Priority
?
300 Views
Last Modified: 2010-08-05
Hi !

i need a sample to add element or node in XML in a specific
location that i want in the xml using Xml.XmlDocument

10x's a lot
0
Comment
Question by:bwz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Accepted Solution

by:
Shatai earned 400 total points
ID: 11750306
It all depends on how you want to go about inserting the new node.

What you'll probably want is the InsertBefore and InsertAfter methods.  Below is the sample code from the InsertBefore method in the VB.NET help files.  This'll insert the new node (elem) as the first node in root's ChildNodes.

If you wanted to insert it as the second node, you could use either
    root.InsertAfter(elem, root.FirstNode)  ' As long as there is a FirstNode
or
   root.InsertBefore(elem, root.ChildNodes(1)  ' As long as ChildNodes.Count >=2, so that there is a ChildNodes(1)

Hope this helps.

Sample code:-
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
0
 

Author Comment

by:bwz
ID: 11750332
thank's alot ,
i'm finally used CreateDocumentFragment but its look more simple
i will try it .
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question