Solved

write to XML

Posted on 2004-08-08
4
288 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
4 Comments
 
LVL 1

Accepted Solution

by:
Shatai earned 100 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now