Need to write an XML file

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I'm working on an application to keep track of recipes, and I'd like to write the data to an XML file so my program can retrieve it later. I've been looking for examples of this, but so far have only found examples of writing to memory. Most of what I want to do is writing textbox text as XML elements. I'd like the filename to be RName.Text ".xml". Can anyone show me a small example of writing a textbox text as an XML element to a new XML file or show me an online example? I looked for awhile today and didn't find any examples doing this. Thanks.
 
I think I found what I needed. Now the problem is getting the hierarchy figured out. I'm not sure how to save each ingredient in the XML, as in, what structure. I need to test to see if I can have multiple elements or attributes named Ingredient, but here's what I have now. It's giving me this error: "Token StartElement in state Epilog would result in an invalid XML document."

I've changed this up a bit, and it actually writes to an XML document now, but I don't like the setup, and the XML has a few things in it that I don't understand.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#000000] [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#000000] Save_Click([/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2][COLOR=#000000] sender [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Object, [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2][COLOR=#000000] e [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.EventArgs) [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#000000] Save.Click[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xtwcurrent [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlTextWriter
xtwcurrent = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Xml.XmlTextWriter([/SIZE][SIZE=2][COLOR=#800000]"C:\recipe\myrecipe.xml"[/COLOR][/SIZE][SIZE=2], System.Text.Encoding.UTF8)
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] xtwcurrent
.Formatting = Xml.Formatting.Indented
.WriteStartDocument()
.WriteDocType([/SIZE][SIZE=2][COLOR=#800000]"Recipe"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2])
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Recipe"[/COLOR][/SIZE][SIZE=2])
.WriteStartAttribute([/SIZE][SIZE=2][COLOR=#800000]"Name"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"Name"[/COLOR][/SIZE][SIZE=2], RName.Text)
.WriteEndAttribute()
[/SIZE][SIZE=2][COLOR=#008000]'.WriteEndElement()
[/COLOR][/SIZE][SIZE=2].WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"ingredient"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"ingredient"[/COLOR][/SIZE][SIZE=2])
.WriteValue([/SIZE][SIZE=2][COLOR=#800000]"test"[/COLOR][/SIZE][SIZE=2])
.WriteEndElement()
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Name"[/COLOR][/SIZE][SIZE=2])
.WriteAttributeString([/SIZE][SIZE=2][COLOR=#800000]"Ingredient"[/COLOR][/SIZE][SIZE=2], Item.Text)
.WriteAttributeString([/SIZE][SIZE=2][COLOR=#800000]"Quantity"[/COLOR][/SIZE][SIZE=2], Quantity.Text, Measure.SelectedItem)
.WriteEndDocument()
.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
I'm not writing everything I want yet, because I didn't code it all while I'm testing, but the document looks like this so far:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE Recipe (View Source for full doctype...)>
- <Recipe Name:Name="" xmlns:Name="test">
<ingredient xmlns="ingredient">test</ingredient>

<Name Ingredient="salt" d2p1:Quantity="tsp" xmlns:d2p1="1" />

</Recipe>

Why is it putting the "xmlns" stuff in there? I'd like to have the ingredient line look like this if I could:
<ingredient Name="Salt" Quantity="1" Measure="tsp">
and am also wondering what the d2p1 stuff is there for too. Thanks for any help.


[/COLOR][/SIZE]
 
Last edited:
Application overwrites XML data instead of adding new

I'm working on an application to store recipes in an XML file. I just figured out how to add new recipes to an existing XML file, but when I try to add a third, while debugging, it gives me the error "The writer is closed.". If I check the XML file after stopping, it has overwritten the second recipe in the file with the new recipe I'm trying to add. I've looked over the code many times, but I haven't worked with this before so I don't know where the problem is. Here's what the XML file looks like.
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE Recipe (View Source for full doctype...)>
- <Recipes>
- <Recipe Name="Blake1">
- <Ingredients>
<Item>1 cup water</Item>

</Ingredients>


- <Directions>
<Step>test1</Step>

</Directions>


- <Comments>
<Step>test1</Step>

</Comments>


</Recipe>


- <Recipe Name="test">
- <Ingredients>
<Item>1 tsp salt</Item>

</Ingredients>


- <Directions>
<Step>test</Step>

</Directions>


- <Comments>
<Step>test</Step>

</Comments>


</Recipe>


</Recipes>

Here's the section of code that runs if the XML file already exists
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] newdoc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Xml.XmlDataDocument
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] olddoc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Xml.XmlDocument
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xtwcurrent [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlTextWriter
olddoc.Load([/SIZE][SIZE=2][COLOR=#800000]"C:\recipe\myrecipe.xml"[/COLOR][/SIZE][SIZE=2])
newdoc.Load([/SIZE][SIZE=2][COLOR=#800000]"C:\recipe\myrecipe.xml"[/COLOR][/SIZE][SIZE=2])
xtwcurrent = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Xml.XmlTextWriter([/SIZE][SIZE=2][COLOR=#800000]"C:\recipe\myrecipe.xml"[/COLOR][/SIZE][SIZE=2], System.Text.Encoding.UTF8)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rnodes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNodeList = newdoc.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"Recipes"[/COLOR][/SIZE][SIZE=2]).ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rnode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] xtwcurrent
.Formatting = Xml.Formatting.Indented
.WriteStartDocument()
.WriteDocType([/SIZE][SIZE=2][COLOR=#800000]"Recipe"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2])
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Recipes"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] rnode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] rnodes
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] rnode.Name = [/SIZE][SIZE=2][COLOR=#800000]"Recipe"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]newdoc.CreateNode(Xml.XmlNodeType.Element, [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2], rnode.Name, [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2])
xtwcurrent.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Recipe"[/COLOR][/SIZE][SIZE=2])
xtwcurrent.WriteStartAttribute([/SIZE][SIZE=2][COLOR=#800000]"Name"[/COLOR][/SIZE][SIZE=2])
xtwcurrent.WriteValue(rnode.Attributes(0).Value)
xtwcurrent.WriteEndAttribute()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rchild [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rstuff [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNodeList = rnode.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] rchild [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] rstuff
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] rchild.Name = [/SIZE][SIZE=2][COLOR=#800000]"Ingredients"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] xtwcurrent
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Ingredients"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ingchild [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ingchildren [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNodeList = rchild.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] ingchild [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] ingchildren
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Item"[/COLOR][/SIZE][SIZE=2])
.WriteValue(ingchild.InnerText)
.WriteEndElement()
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2].WriteEndElement()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] rchild.Name = [/SIZE][SIZE=2][COLOR=#800000]"Directions"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] xtwcurrent
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Directions"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] child [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rgchild [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNodeList = rchild.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] child [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] rgchild
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Step"[/COLOR][/SIZE][SIZE=2])
.WriteValue(child.InnerText)
.WriteEndElement()
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2].WriteEndElement()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] rchild.Name = [/SIZE][SIZE=2][COLOR=#800000]"Comments"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] xtwcurrent
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] clines [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = rnode.ChildNodes.Count
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Comments"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] comchild [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] comchildren [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNodeList = rchild.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] comchild [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] comchildren
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Step"[/COLOR][/SIZE][SIZE=2])
.WriteValue(comchild.InnerText)
.WriteEndElement()
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2].WriteEndElement()
.WriteEndElement()
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Recipe"[/COLOR][/SIZE][SIZE=2])
.WriteStartAttribute([/SIZE][SIZE=2][COLOR=#800000]"Name"[/COLOR][/SIZE][SIZE=2])
.WriteValue(RName.Text)
.WriteEndAttribute()
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Ingredients"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] x = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] Ingredients.Items.Count - 1
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Item"[/COLOR][/SIZE][SIZE=2])
.WriteValue(Ingredients.Items.Item(x))
.WriteEndElement()
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] x
.WriteEndElement()
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Directions"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] lines() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Directions.Lines
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] y [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] lines.GetUpperBound(0)
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Step"[/COLOR][/SIZE][SIZE=2])
.WriteValue(lines(y))
.WriteEndElement()
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] y
.WriteEndElement()
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Comments"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] comlines() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Comments.Lines
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] z [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] comlines.GetUpperBound(0)
.WriteStartElement([/SIZE][SIZE=2][COLOR=#800000]"Step"[/COLOR][/SIZE][SIZE=2])
.WriteValue(comlines(z))
.WriteEndElement()
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] z
.WriteEndElement()
.WriteEndDocument()
[/SIZE][SIZE=2][COLOR=#008000]'newdoc.Save("C:\recipe\myrecipe.xml")
[/COLOR][/SIZE][SIZE=2].Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
I'd appreciate any help in fixing this. Thanks.
 

Latest posts

Back
Top