• Status: Solved
  • Priority: High
  • Security: Private
  • Views: 80
  • Last Modified:

how to update an xml file using vb.net

I want to use the following code to update the existing .xml file and it target nodes.

Dim sf As New SoapFormatter
        Dim fs As New FileStream(filePath & "WU_GIDList.xml", FileMode.Create)
        sf.Serialize(fs, masterGIDSettings)
        fs.Close()
        fs = Nothing
        sf = Nothing

Open in new window

0
cmdolcet
Asked:
cmdolcet
1 Solution
 
Ryan ChongCommented:
depends on how your xml was constructed, you can try to implement something mentioned in this article.

How to change and update XML file in ASP.NET
http://www.devasp.net/net/articles/display/1341.html

same concept applies even your program is not for ASP.NET

the update process may not related to the codes you posted above as it seems to create the full file.
0
 
Pawan KumarDatabase ExpertCommented:
Can you show your XML -> You wanted to change your inner text or something else.

Meanwhile please go through below-

The below URL has all the examples for you- < Create and Merge Files. Edit, Add and Delete Nodes.>
http://www.vbforums.com/showthread.php?478692-VB-NET-XML-Create-and-Merge-Files-Edit-Add-and-Delete-Nodes
0
 
cmdolcetAuthor Commented:
Pawan,

The code I posted ealier creates the following .xml file. I want to be able to add addtional lines in the xml or to look for a specific Gage_ID and GageDate and Gage Description and delete it.

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:MasterGageIDSettings id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Wireless_Interface/Wireless%20Interface%2C%20Version%3D6.0.0.8%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<MasterGageID href="#ref-3"/>
</a1:MasterGageIDSettings>
<a1:MasterGageID id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Wireless_Interface/Wireless%20Interface%2C%20Version%3D6.0.0.8%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<MasterAdditionDate id="ref-4">11/27/2017 4:16:28 PM</MasterAdditionDate>
<MasterGageID_ID id="ref-5">L01554</MasterGageID_ID>
<MasterGageID_Description id="ref-6"></MasterGageID_Description>
</a1:MasterGageID>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Fernando SotoRetiredCommented:
Hi cmdolcet;

To your statement, "I want to be able to add additional lines in the xml", XML documents have a very specific structure and so to add these lines we need to know what type of lines you wish to add. For example will you always be adding a node such as the a1:MasterGageID node and its children nodes
<a1:MasterGageID id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Wireless_Interface/Wireless%20Interface%2C%20Version%3D6.0.0.8%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
    <MasterAdditionDate id="ref-4">11/27/2017 4:16:28 PM</MasterAdditionDate>
    <MasterGageID_ID id="ref-5">L01554</MasterGageID_ID>
    <MasterGageID_Description id="ref-6"></MasterGageID_Description>
</a1:MasterGageID>

Open in new window

Or a single child node such as MasterGageID_Description to add to an existing a1:MasterGageID node? And for deleting is it to a single child node or to a parent node which may have many child nodes and what info is known about the nodes to be deleted so that it can be found in the document.
0
 
cmdolcetAuthor Commented:
Sorry yes,

So the lines I would like to add would be the MasterGageID_ID and MasterGageID_Description.

I could have a GageID "L011554" and also add in another GageID "L02322" I would like to group each GageID with its date and its Description


<a1:MasterGageID id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Wireless_Interface/Wireless%20Interface%2C%20Version%3D6.0.0.8%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
    <MasterAdditionDate id="ref-4">11/27/2017 4:16:28 PM</MasterAdditionDate>
    <MasterGageID_ID id="ref-5">L01554</MasterGageID_ID>
    <MasterGageID_Description id="ref-6"></MasterGageID_Description>
</a1:MasterGageID>
  <MasterAdditionDate id="ref-4">11/27/2017 4:16:28 PM</MasterAdditionDate>
    <MasterGageID_ID id="ref-5">L02322</MasterGageID_ID>
    <MasterGageID_Description id="ref-6"></MasterGageID_Description>
</a1:MasterGageID>

Open in new window

0
 
Fernando SotoRetiredCommented:
What to you mean by this, "I would like to group each GageID with its date and its Description"? Can you show an example of that.
0
 
cmdolcetAuthor Commented:
OK I have two Gages ID "L01554" and "L02322" the gages where entered at two different times therefore

</a1:MasterGageID>
<MasterAdditionDate id="ref-4">11/27/2017 4:16:28 PM</MasterAdditionDate>
 <MasterGageID_ID id="ref-5">L01554</MasterGageID_ID>
 <MasterGageID_Description id="ref-6"></MasterGageID_Description>

<MasterAdditionDate id="ref-4">11/27/2017 4:50:28 PM</MasterAdditionDate>
 <MasterGageID_ID id="ref-5">L02322</MasterGageID_ID>
 <MasterGageID_Description id="ref-6"></MasterGageID_Description>
</a1:MasterGageID>

Open in new window



If I wanted to update them It would only update the information for L01554 or L02322
If I wanted to delete one of the gages it would look like this
</a1:MasterGageID>
<MasterAdditionDate id="ref-4">11/27/2017 4:50:28 PM</MasterAdditionDate>
 <MasterGageID_ID id="ref-5">L02322</MasterGageID_ID>
 <MasterGageID_Description id="ref-6"></MasterGageID_Description>
</a1:MasterGageID>

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi cmdolcet;

Here is some sample code that will show you what you need to do to modify the document.
'' Load the XML document into memory
Dim soapDoc = XDocument.Load("C:\Working Directory\mysoap.xml")
'' The Gage ID for the node info to modify 
Dim gageId = "L01554"
'' Info to replace what is already there
Dim dateModifyed = "11/28/2017 10:51:00 PM"

'' Linq to XML query to get the nod to be modified or delete
Dim result As Xelement = (From g In soapDoc.Descendants("MasterGageID_ID")
                          Where g.Value = gageId
                          Select g).FirstOrDefault()

'' To modify the node you can use code like this
If result IsNot Nothing Then
    Dim dateToModify = result.Parent.Element("MasterAdditionDate")
    dateToModify.SetValue(dateModifyed)
End If

'' To delete a "a1:MasterGageID" with its children you can use this code
If result IsNot Nothing Then
    result.Parent.Remove()
End If

'' Once you modified and or deleted the nodes you can save it back to the file system
'' with something like this
soapDoc.Save("C:\Working Directory\mysoapModified.xml")

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now