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
Solved

How to rewrite a XML vb.NET

Posted on 2004-08-04
5
557 Views
Last Modified: 2008-03-03
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
0
Comment
Question by:Jose_Cabrero
  • 2
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11722499
What do you mean by "rewrite the same file"?

Bob
0
 

Author Comment

by:Jose_Cabrero
ID: 11725481
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11725914
Are you using XML as a database?

Bob
0
 

Author Comment

by:Jose_Cabrero
ID: 11743928
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)
0
 

Accepted Solution

by:
garyolliffe earned 500 total points
ID: 12026581
Just had to translate this from C# in place, so Apologies if there is a type or two...

Gary.....

Public Function DoAdd(doc as XmlDocument, keyVal as String, value as String) as XmlDocument
    Dim element as XmlElement = doc.CreateElement("node")
    element.InnerText = value
    element.SetAttribute("key", keyVal)
    Dim node as XmlNode  = doc.SelectSingleNode("root")
    If not IsNothing(node) Then
      node.AppendChild(element)
    End If
    Return doc
End Function

Public Function RemoveByKey(doc as XmlDocument, keyVal as String) as XmlDocument
     Dim removeNode as XmlNode = doc.SelectSingleNode("root/node[@key='" & keyVal & "']")
     If Not IsNothing( removeNode ) Then
          removeNode.ParentNode.RemoveChild(removeNode)
     End If
     Return doc
End Function
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 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