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

cmdolcetAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.