Solved

How to rewrite a XML vb.NET

Posted on 2004-08-04
5
546 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
Comment Utility
What do you mean by "rewrite the same file"?

Bob
0
 

Author Comment

by:Jose_Cabrero
Comment Utility
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
Comment Utility
Are you using XML as a database?

Bob
0
 

Author Comment

by:Jose_Cabrero
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now