Hello all! Hopefully there is someone out there who can help me with what I need to achieve.

Goal: Loop through XML file, reading node by node searching for a node with a specific attribute with a specific value, then write to another attribute of said node found.

I feel that isn't as clear as it could be, so hopefully further reading will clarify.

The XML FILE:

<root>
    <level id="1">
        <prop n="LookVertSpeed_Pad" v="120.0"/>
        <prop n="LookHorzSpeed_Pad" v="260.0"/>
        <prop n="LookVertAcc_Pad" v="2500.0"/>
        <prop n="LookHorzAcc_Pad" v="2500.0"/>
        <prop n="LookVertDec_Pad" v="50000.0"/>
        <prop n="LookHorzDec_Pad" v="50000.0"/>
    </level>
</root>

Basically I need to load the file that contains a more elaborte yet consistant group of nodes identical to the XML code above and loop through looking for, lets say for example sake, LookVertAcc_Pad in attribute 'n' and update its 'v' attribute to 3000.0. Specifying any <level> node isn't necessary, needed, or warranted. All attributes that I will need to modify will be in this format. I am completely new to reading/writing XML and would highly appreciate any help.

HI, I have to use a simple code to read & write xml file like initial parameters, maybe this code can help you:

Imports System.Linq
Imports System.Xml

Sub DataXml_Create(ByVal sFile As String)

Dim xDoc As New XmlDocument
Try
  'Create structur XML
   Dim xRoot As XmlElement = xDoc.CreateElement("ControlPanel")
   xDoc.AppendChild(xRoot)

   '------------------------------------------------------------------
   Dim CnnControl As XmlNode = xDoc.CreateElement("Control")
   xRoot.AppendChild(Control)

   Dim sSrvP As XmlNode = xDoc.CreateElement("SrvP")
   Control.AppendChild(sSrvP)
   Dim sSrvT As XmlNode = xDoc.CreateElement("SrvT")
   Control.AppendChild(sSrvT)

   '------------------------------------------------------------------
   System.IO.File.Create(sFile).Close()
   If My.Computer.FileSystem.FileExists(sFile) Then xDoc.Save(sFile)

 Catch Ex As Exception

   Dim strMsg As String = "Error creating XML file"
   MsgBox(strMsg, MsgBoxStyle.OkOnly, "DatosXml_Crear")

 End Try

End Sub

Sub DataXML_Upload(byval strXmlFile As String)

Try

  If Not My.Computer.FileSystem.FileExists(strXmlFile) Then DataXml_Create(strXmlFile)

  If My.Computer.FileSystem.FileExists(strXmlFile) Then
     Dim xmlDoc As XDocument = XDocument.Load(strXmlFile)

     For Each sControl In xmlDoc.Descendants("Control")
       If sControl.HasElements Then
         txtConnSrvP.Text = sControl.Element("SrvP").Value
         txtConnSrvT.Text = sControl.Element("SrvT").Value
       End If
     Next

  Catch Ex As Exception

    MsgBox(Ex.Message)

  End Try

End Sub

Sub DataXML_Save(BYVal strXmlFile As String)

Try
   If My.Computer.FileSystem.FileExists(strXmlFile) Then
     Dim xmlDoc As XDocument = XDocument.Load(strXmlFile)

     For Each sControl In xmlDoc.Descendants("Control")
        If sControl.HasElements Then
          sControl.Element("SrvP").Value = txtSrvP.Text
          sControl.Element("SrvT").Value = txtSrvT.Text
        End If
     Next

     xmlDoc.Save(strXmlFile)

   End If

Catch ex As Exception
   MsgBox(ex.Message)
End Try

End Sub

Regards

Think I have it figured out now I just need to optimize the code. Thank you all for the assistance you have provided!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.