Help with simple XML - Noobie question

Capt_Ron

Active member
Joined
Apr 29, 2005
Messages
39
Programming Experience
1-3
First of all thank you for looking.

Here's what I'm trying to do:

I have a program that I'd like to store some values in a simple XML file. I need for the program to check to see if the file exists, create it if it doesn't and then fill in the values. I also need to read the values into an object.

Below is the XML file. I know how to check for it's existence but I can't figure out how to create, modify, read from it. I've searched and do not understand the solutions others have found.

VB.NET:
[size=2][color=#0000ff]<?[/color][/size][size=2][color=#800000]xml[/color][/size][size=2][color=#ff00ff] [/color][/size][size=2][color=#ff0000]version[/color][/size][size=2][color=#0000ff]="1.0"[/color][/size][size=2][color=#ff00ff] [/color][/size][size=2][color=#ff0000]encoding[/color][/size][size=2][color=#0000ff]="iso-8859-1"?>
<[/color][/size][size=2][color=#800000]WSLY[/color][/size][size=2][color=#0000ff]>
[/color][/size][size=2][color=#0000ff]  <[/color][/size][size=2][color=#800000]Value1[/color][/size][size=2][color=#0000ff]>Value</[/color][/size][size=2][color=#800000]Value1[/color][/size][size=2][color=#0000ff]>
[/color][/size][size=2][color=#0000ff]  <[/color][/size][size=2][color=#800000]Value2[/color][/size][size=2][color=#0000ff]>Another Value</[/color][/size][size=2][color=#800000]Value2[/color][/size][size=2][color=#0000ff]>
[/color][/size][size=2][color=#0000ff]  <[/color][/size][size=2][color=#800000]Value3[/color][/size][size=2][color=#0000ff]>Still Another One</[/color][/size][size=2][color=#800000]Value3[/color][/size][size=2][color=#0000ff]>
[/color][/size][size=2][color=#0000ff]  <[/color][/size][size=2][color=#800000]L1[/color][/size][size=2][color=#0000ff]>[/color][/size][size=2]0[/size][size=2][color=#0000ff]</[/color][/size][size=2][color=#800000]L1[/color][/size][size=2][color=#0000ff]>
[/color][/size][size=2][color=#0000ff]  <[/color][/size][size=2][color=#800000]L2[/color][/size][size=2][color=#0000ff]>[/color][/size][size=2]0[/size][size=2][color=#0000ff]</[/color][/size][size=2][color=#800000]L2[/color][/size][size=2][color=#0000ff]>
[/color][/size][size=2][color=#0000ff]  <[/color][/size][size=2][color=#800000]L3[/color][/size][size=2][color=#0000ff]>[/color][/size][size=2]0[/size][size=2][color=#0000ff]</[/color][/size][size=2][color=#800000]L3[/color][/size][size=2][color=#0000ff]>
[/color][/size][size=2][color=#0000ff]  <[/color][/size][size=2][color=#800000]LS[/color][/size][size=2][color=#0000ff]>[/color][/size][size=2]0[/size][size=2][color=#0000ff]</[/color][/size][size=2][color=#800000]LS[/color][/size][size=2][color=#0000ff]>
</[/color][/size][size=2][color=#800000]WSLY[/color][/size][size=2][color=#0000ff]>
[/color][/size]

Thanks again for helping.
Ron
 
If your data corresponds directly to an object then you would be better off using serialisation to read and write the XML. If you need to massage the data a bit to get it into your object then you will need to create an XmlDocument to read the data. Can you give a little more info on the correspondence between the data in the file and the properties of your object and we can try to determine the best way to proceed?
 
More information

The data would need to be altered before loading the object.

I have a custom SQL object that the XML document stores values for. I need to be able to change the values before the object is loaded.

I'm trying to write a class (dll) that'll create the XML file and also apply changes to the file as I need them.

Hope this helps
Thanks
Ron.
 
Half way there

I was, after some playing, able to change the values in an existing XML file.
Now I just need help creating the file.

Thanks
Ron.
 
The easiest way to do this sort of thing is to create a class that has properties that correspond directly to the nodes of the XML file. To edit the XML you simply deserialise it to an object, which takes four lines of code regardless of the complexity of the object, then edit the properties of the object and serialise it back to XML, which takes another four lines, e.g.
VB.NET:
		Dim myObject As SomeClass
		Dim myStream As IO.FileStream
		Dim mySerialiser As New Xml.Serialization.XmlSerializer(GetType(SomeClass))
		Dim myPath As String = "C:\MyFile.xml"

		If IO.File.Exists(myPath) Then
			'Deserialise the existing file to an object.
			myStream = New IO.FileStream(myPath, IO.FileMode.Open)
		    myObject = CType(mySerialiser.Deserialize(myStream), SomeClass)
			myStream.Close()
		Else
			'Create a new object.
			myObject = New SomeClass
		End If

		myObject.Property1 = 100
		myObject.Property2 = "Hello World"

		'Serialise the object to a file.
		myStream = New IO.FileStream(myPath, IO.FileMode.Create)
		mySerialiser.Serialize(myStream, myObject)
		myStream.Close()
You can create the file in the first place simply by serialising a new object. If serialization is not suitable for your situation, I'd suggest a help search for terms like "create XML", which should yield suitable information.
 
Back
Top