Writiing an xml file.

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Hi,
I know how to write data to an xml file but the problem is how do I add data to the xml instead of over writing the current data?
VB.NET:
[size=2][color=#0000ff]Protected[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ButtonUpdate_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] ButtonUpdate.Click

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] textWriter [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] XmlTextWriter(Server.MapPath([/size][size=2][color=#800000]"PersonData.xml"[/color][/size][size=2]), [/size][size=2][color=#0000ff]Nothing[/color][/size][size=2])
textWriter.Formatting = System.Xml.Formatting.Indented
[/size][size=2][color=#008000]'Start New Document
[/color][/size][size=2]textWriter.WriteStartDocument()
[/size][size=2][color=#008000]'Write a Comment
[/color][/size][size=2]textWriter.WriteComment([/size][size=2][color=#800000]"This is a comment"[/color][/size][size=2])
[/size][size=2][color=#008000]'Insert Start Element
[/color][/size][size=2]textWriter.WriteStartElement([/size][size=2][color=#800000]"PersonalData"[/color][/size][size=2])
[/size][size=2][color=#008000]'Write Attribute for Start Element
[/color][/size][size=2]textWriter.WriteAttributeString([/size][size=2][color=#800000]"DataType"[/color][/size][size=2], [/size][size=2][color=#800000]"NameAndAddress"[/color][/size][size=2])
[/size][size=2][color=#008000]'Write LastName Element and Data
[/color][/size][size=2]textWriter.WriteStartElement([/size][size=2][color=#800000]"LastName"[/color][/size][size=2], [/size][size=2][color=#800000]""[/color][/size][size=2])
textWriter.WriteString([/size][size=2][color=#800000]"Levy"[/color][/size][size=2])
textWriter.WriteEndElement()
[/size][size=2][color=#008000]'Write FirstName Element and Data
[/color][/size][size=2]textWriter.WriteStartElement([/size][size=2][color=#800000]"FirstName"[/color][/size][size=2], [/size][size=2][color=#800000]""[/color][/size][size=2])
textWriter.WriteString([/size][size=2][color=#800000]"Jon"[/color][/size][size=2])
textWriter.WriteEndElement()
[/size][size=2][color=#008000]'Write MiddleName Element and Data
[/color][/size][size=2]textWriter.WriteStartElement([/size][size=2][color=#800000]"MiddleName"[/color][/size][size=2], [/size][size=2][color=#800000]""[/color][/size][size=2])
textWriter.WriteString([/size][size=2][color=#800000]""[/color][/size][size=2])
textWriter.WriteEndElement()
[/size][size=2][color=#008000]'Write Address Element and Data
[/color][/size][size=2]textWriter.WriteStartElement([/size][size=2][color=#800000]"Address"[/color][/size][size=2], [/size][size=2][color=#800000]""[/color][/size][size=2])
textWriter.WriteString([/size][size=2][color=#800000]"Pandy"[/color][/size][size=2])
textWriter.WriteEndElement()
[/size][size=2][color=#008000]'Write City Element and Data
[/color][/size][size=2]textWriter.WriteStartElement([/size][size=2][color=#800000]"City"[/color][/size][size=2], [/size][size=2][color=#800000]""[/color][/size][size=2])
textWriter.WriteString([/size][size=2][color=#800000]"Pandy"[/color][/size][size=2])
textWriter.WriteEndElement()
[/size][size=2][color=#008000]'Write State Element and Data
[/color][/size][size=2]textWriter.WriteStartElement([/size][size=2][color=#800000]"State"[/color][/size][size=2], [/size][size=2][color=#800000]""[/color][/size][size=2])
textWriter.WriteString([/size][size=2][color=#800000]"RCT"[/color][/size][size=2])
textWriter.WriteEndElement()
[/size][size=2][color=#008000]'Write ZipCode Elment and Data[/color][/size]
[size=2]textWriter.WriteStartElement([/size][size=2][color=#800000]"ZipCode"[/color][/size][size=2], [/size][size=2][color=#800000]""[/color][/size][size=2])
textWriter.WriteString([/size][size=2][color=#800000]"Postcode"[/color][/size][size=2])
textWriter.WriteEndElement()
[/size][size=2][color=#008000]'End Everything
[/color][/size][size=2]textWriter.WriteEndDocument()
[/size][size=2][color=#008000]'Clean up
[/color][/size][size=2]textWriter.Flush()
textWriter.Close()
[/size][size=2][color=#008000]'Display the XML Document
[/color][/size][size=2]Response.Redirect(Server.MapPath([/size][size=2][color=#800000]"PersonData.xml"[/color][/size][size=2]))
[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub
[/color][/size]

That is my current code, all it does is create the file "Persondta.xml" and add the one piece of information. I would like to add more than one piece of information. For instance another person.

How can i do this?

Also how can I retrieve this information into a webform?
 

TechGnome

Well-known member
Joined
May 23, 2005
Messages
896
Programming Experience
10+
kulrom said:
Hi levy,
When we want to add data into the XML-file we insert nodes.

PHP:
 Dim docXML As XmlDocument 
Dim newNode As XmlNode
newNode = docXML.CreateNode(XmlNodeType.Element, "NodeThatWillBeAdded", "")
{...}

Let me know if you need additional assistance :)

Cheers ;)

The key would be to load the XML document first.... then add to it.... then save it...

Tg
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Hey dude thanks. That is much easier to understand than the tutorials i've seen floating around the internet. I will have another question for you soon enough on xml so be warned :p
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Knew I would be back with a question.

Lets looka t this XML file.
VB.NET:
[size=2][color=#0000ff]<?[/color][/size][size=2][color=#800000]xml[/color][/size][size=2][color=#ff0000]version[/color][/size][size=2][color=#0000ff]=[/color][/size][size=2]"[/size][size=2][color=#0000ff]1.0[/color][/size][size=2]"[/size][size=2][color=#ff0000]encoding[/color][/size][size=2][color=#0000ff]=[/color][/size][size=2]"[/size][size=2][color=#0000ff]utf-8[/color][/size][size=2]"[/size][size=2][color=#0000ff]?>
<[/color][/size][size=2][color=#800000]Deliveries[/color][/size][size=2][color=#0000ff]>
<[/color][/size][size=2][color=#800000]TenToTwelve[/color][/size][size=2][color=#0000ff]>[/color][/size][size=2]12[/size][size=2][color=#0000ff]</[/color][/size][size=2][color=#800000]TenToTwelve[/color][/size][size=2][color=#0000ff]>
<[/color][/size][size=2][color=#800000]TwelveToTwo[/color][/size][size=2][color=#0000ff]>[/color][/size][size=2]14[/size][size=2][color=#0000ff]</[/color][/size][size=2][color=#800000]TwelveToTwo[/color][/size][size=2][color=#0000ff]>
<[/color][/size][size=2][color=#800000]TwoToFour[/color][/size][size=2][color=#0000ff]>[/color][/size][size=2]18[/size][size=2][color=#0000ff]</[/color][/size][size=2][color=#800000]TwoToFour[/color][/size][size=2][color=#0000ff]>
<[/color][/size][size=2][color=#800000]FourToSix[/color][/size][size=2][color=#0000ff]>[/color][/size][size=2]18[/size][size=2][color=#0000ff]</[/color][/size][size=2][color=#800000]FourToSix[/color][/size][size=2][color=#0000ff]>
<[/color][/size][size=2][color=#800000]SixToEight[/color][/size][size=2][color=#0000ff]>[/color][/size][size=2]14[/size][size=2][color=#0000ff]</[/color][/size][size=2][color=#800000]SixToEight[/color][/size][size=2][color=#0000ff]>
</[/color][/size][size=2][color=#800000]Deliveries[/color][/size][size=2][color=#0000ff]>[/color][/size]
[size=2][color=#0000ff]
[/color][/size]


I would like to load this file on page_load(ASP.net) and assaign each value into a textfield. once that is done I would like to be able to alter the value of the text field and then save the changes to the XML file. How can I do it :D
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
I'm affraid I haven't got it. Ignore the xml file at the start of this thread, just use the newest one I have. Firstly I don't know how to get the data out of the xml file and into textboxes. Secondly I would need an example on how to update the xml file. But for now I am going to bed because I'm very tired.
Good night kul
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Ok I looked at that and figured out how it works but I couldn't get the data into textboxes. I've had enough of it now so I'm just going to use a database table. At least I can do that.

Cheers or your help though kul :D
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
If you are comfortable with dataSets/dataTables but need to use XML files as storage, the dataSet class has functions that read and write XML files (ReadXml and WriteXml).
This is the way that I generally work with XML files because I too am more comfortable with datasets than manually parsing XML files.
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Well, you can find many sites about this but in your case it's only wasting resources cuz you deal with extremly small data ... anyway it's up to you :p

couple links:

http://www.codeguru.com/vb/gen/vb_database/adonet/article.php/c5153/

http://www.vbcity.com/forums/topic.asp?tid=22190 'code included

Cheers ;)

edit: maybe i've got you wrong. It seems to me that in your previous post told me you have figured out and you don't need any help with this ... but, if you need something else plz feel free to ask ... i'll be glad to help you out ;)
 
Last edited:

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Yeah I figured out how your app worked but I could't adapt it. Don't forget this is an asp.net page. So the listview doesn't work. So I tried to change it and get it to put the data into a textbox instead but thats where it failed. In the end I had enough of it and used a database. Which is working but it has caused another problem, which I will look at after the weekend. Been too busy this week to thik about it.
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
As far as using dataSets with XML files:
It seems that you are familiar with using dataSets. If that's correct, all you need to do is read the XML file into a dataSet, work with the data (display in textBoxs using databindings, modify, etc...) then just write the dataSet back to the XML file.
VB.NET:
Dim ds As New DataSet()
ds.ReadXml("C:\SomePath\Persondta.xml")
'display in textBoxs using databindings, modify, etc...
ds.WriteXml("C:\SomePath\Persondtq.xml")
This article on CodeProject is where I first learned of the ability to use XML with dataSets: Managing configuration settings persistence in .NET applications. The article also shows how to deal with the possibility that the file doesn't exist and how to create the dataSet/dataTable in such a case.
 
Top Bottom