Link to home
Start Free TrialLog in
Avatar of Jose_Cabrero
Jose_Cabrero

asked on

How to rewrite a XML vb.NET

Hi Guys I have this function for write a XML file but I dont know how to rewrite the same file could anybody show me how to do this

Private Sub createXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ensalada_btn.Click
        Try
            'Initialize a XmlTextWrite Object
            Dim xmlDoc As XmlDocument
            'Instantiate an xmlDocument
            xmlDoc = New XmlDocument
            'Create a XmlDeclaration & append Declaration
            Dim xmlDec As XmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", Nothing, "no")
            xmlDoc.AppendChild(xmlDec)
            'Create a root Element
            Dim rootElement As XmlElement = xmlDoc.CreateElement("Transaccion_Temp", Nothing)
            'Create First Level
            Dim elementLevelOne As XmlElement = xmlDoc.CreateElement("Order", Nothing)
            elementLevelOne.SetAttribute("id", "1")
            Dim hsAttr As New Hashtable
            hsAttr.Add("badge", "1235")
            hsAttr.Add("date", "2004/08/03")
            elementLevelOne.AppendChild(myElement(xmlDoc, "Worker", "595893", hsAttr))
            rootElement.AppendChild(elementLevelOne)
            xmlDoc.AppendChild(rootElement)
            xmlDoc.Save("c:/test1.xml")

        Catch ex As XmlException
            MsgBox(ex.Message)
        End Try


     
    End Sub

    Function myElement(ByVal xmlDocument As XmlDocument, ByVal ElementName As String, ByVal elementString As String, ByVal htAttr As Hashtable) As XmlElement
        'Add Element & Attributes to a XML Element
        Dim objElement As XmlElement = xmlDocument.CreateElement(ElementName)
        Dim enumAttr As IDictionaryEnumerator
        If Not htAttr Is Nothing And htAttr.Count <> 0 Then
            enumAttr = htAttr.GetEnumerator
            While enumAttr.MoveNext
                objElement.SetAttribute(enumAttr.Key.ToString(), enumAttr.Value.ToString())
            End While
        End If
        objElement.AppendChild(xmlDocument.CreateTextNode(elementString))
        Return objElement

    End Function
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What do you mean by "rewrite the same file"?

Bob
Avatar of Jose_Cabrero
Jose_Cabrero

ASKER

Add more nodes to the same root for example

<root>
     <node>Example1 </node>
     <node>Example2 </node>
</root>

<root>
     <node>Example1 </node>
     <node>Example2 </node>
     <node>Example3</node>
</root>

Something like insert a new record in the DB
Are you using XML as a database?

Bob
Yes I'm but now I have another question for you guys do you know how to delete a specific childNode on the XML database

for example

<root>
     <node key=1>Example1 </node>
     <node key=2>Example2 </node>
     <node key=3>Example3</node>
</root>


how can I delete the key 2 from the roop parent

(I'm using VB.NET and XML db)
ASKER CERTIFIED SOLUTION
Avatar of garyolliffe
garyolliffe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial