Solved

How to rewrite a XML vb.NET

Posted on 2004-08-04
5
561 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 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

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)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

707 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