Question Read XML, replace section within a tag and write new updated XML

The Dude

New member
Joined
Dec 12, 2012
Messages
1
Programming Experience
10+
Tha have a fairly lage XML file ~350k lines when formatted. The file is used by a purchased application. I have a requirement to refresh the demand records in the file. All the other data in the file should remain the same.

The kludgy process I am currently have implemented is using a streamReader to read each line of the current xml file and a textwriter to write the new file. The process reads, sees if the line it just read is the beginning tag <demandList> I am looking for, if it is not is just writes the line back to the new file, if it is the <demandList> tab, it goes into a method that reads some demand from a database, and writes out the new demand (using extra kludgy logic like this).

Loop for each demand record
Outfile.WriteLine("<demand type=""""Order"""">")
Outfile.WriteLine("<demandCode>" & custNum & "</demandCode>")
Outfile.WriteLine("<customerName></customerName>")
Outfile.WriteLine("<customerNumber>" & custNum & "</customerNumber>")
Outfile.WriteLine("<shipTo></shipTo>") ...etc

Once it is done writing all the demand it goes back to the file reads each line until if finds the end tag </demandList> once the end tag is found it goes back to reading and writing so that the final result is that only the <demandList> section of the xml file has been changed.

There has to be a better way, any ideas and examples would be welcome. This is my first .Net program so be gentle

Thanks in advance
 
Hi,

There are two concepts that you can look at to better help you manage and update your XML file. The first is using the XMLDocument class to hold, manipulate and update your XML information. The second is to read the XML file into a Dataset which then allows you to do the same manipulation and update of your XML information.

I cannot give you a demonstrable example of how to better manipulate the DemandList elements of your XML file without a better example of how the XML file is structured but I guess that using LINQ will be your best option, if using an XMLDocument, when searching for the DemandList element that you need to update.

Here is an example of how you can better read and write your XML file once you have completed the updates that you need using an XMLDocument:-

VB.NET:
Public Class Form1
  Private Const XMLFileName As String = "d:\temp\YourXMLFile.xml"
  Private myReader As New StreamReader(XMLFileName, System.Text.Encoding.UTF8)
  Private myXMlReader As XmlTextReader = New XmlTextReader(myReader)
  Private myXMLDoc As New XmlDocument()
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'Read and load your XML file here
    myXMLDoc.Load(myXMlReader)
    myXMlReader.Close()
    'Manipulate, Process and Update XMLDocument class here, being the myXMLDoc variable.
    'Once all processing and manipulation is done just call the save method of the XMLDocument class
    myXMLDoc.Save(XMLFileName)
  End Sub
End Class

Hope that helps.

Cheers,

Ian
 
Back
Top