Solved

write to XML

Posted on 2004-08-08
4
297 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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