[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to rewrite a XML vb.NET

Posted on 2004-08-04
5
Medium Priority
?
568 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
[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
  • 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

649 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