Solved

write to XML

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

636 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