XML Writing and Reading

G

Guest

I am using the following code to write to an XML file

myXmlTextWriter.Formatting = System.Xml.Formatting.Indente
myXmlTextWriter.WriteStartElement("CPViewer"
myXmlTextWriter.WriteElementString("Height", InputBoxHeight
myXmlTextWriter.WriteElementString("Width", InputBoxWidth
'myXmlTextWriter.WriteElementString("Background", (OpenFileDialog1.FileName)
myXmlTextWriter.WriteElementString("label1.Top", label1.Top
myXmlTextWriter.WriteElementString("label1.Left", label1.Left
myXmlTextWriter.WriteElementString("label1.Height", label1.Height
myXmlTextWriter.WriteElementString("label1.Width", label1.Width
myXmlTextWriter.WriteEndElement(
myXmlTextWriter.Flush(
myXmlTextWriter.Close(

Here is the XML output

<CPViewer><Height>600</Height><Width>800</Width><label1.Top>200</label1.Top><label1.Left>152</label1.Left><label1.Height>23</label1.Height><label1.Width>100</label1.Width></CPViewer

Here is the code from the reader

DataSet1.ReadXml("C:\CPViewer\LD.xml"
Me.DataBindings.Add(New Binding("Height", DataSet1, "CPViewer.Height")
Me.DataBindings.Add(New Binding("Width", DataSet1, "CPViewer.Width")
Me.DataBindings.Add(New Binding("label1.Top", DataSet1, "CPViewer.label1_Top")
'Me.DataBindings.Add(New Binding("BackgroundImage", DataSet1, "CPViewer.BackgroundImage")
Me.BackgroundImage = (Image.FromFile("C:\CPViewer\BackgroundImage.jpg")
Me.CenterToScreen(

When I try to read it in will will read the "Height" and "Width" lines and position the form properly but when it reads the "Label1.top" line I get the following error

An unhandled exception of type 'Syste,.ArgumentException' occurred i
system.windows.forms.dl

Additional Information: Cannot bind to property 'label1.Top' on target control

What am I missing here

Thank you
John
 
C

Cor Ligthert

Hi JCrouse,

I thought I adviced you to use the ds.xmlWrite and ds.xmlRead with that.

I used that method you are now using as well, however that is a lot more
work.

What is the reason you do not want to use that dataset method?

Cor
 
K

Ken Tucker [MVP]

Hi,

Two things. First Your databinding code was wrong. Second Label1.Top
isn't a valid name use Label1_Top instead.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim ds As New DataSet

SaveData()
ds.ReadXml("C:\LD.xml")

Me.DataBindings.Add("Width", ds.Tables(0), "Width")
Me.DataBindings.Add("Height", ds.Tables(0), "Height")
Label1.DataBindings.Add("Top", ds.Tables(0), "Label1_Top")
Label1.DataBindings.Add("Left", ds.Tables(0), "Label1_Left")
Label1.DataBindings.Add("Width", ds.Tables(0), "Label1_Width")
Label1.DataBindings.Add("Height", ds.Tables(0), "Label1_Height")

End Sub

Private Sub SaveData()

Dim myXmlTextWriter As New Xml.XmlTextWriter("C:\LD.xml",
System.Text.Encoding.Unicode)

myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartElement("CPViewer")
myXmlTextWriter.WriteElementString("Height", "600")
myXmlTextWriter.WriteElementString("Width", "800")
'myXmlTextWriter.WriteElementString("Background",
(OpenFileDialog1.FileName))
myXmlTextWriter.WriteElementString("label1_Top", "200")
myXmlTextWriter.WriteElementString("label1_Left", "152")
myXmlTextWriter.WriteElementString("label1_Height", "23")
myXmlTextWriter.WriteElementString("label1_Width", "100")
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.Flush()
myXmlTextWriter.Close()
End Sub

Ken
 
C

Cor Ligthert

Hi John,

Thanks to the message from Ken, I am seeing now what you are doing.
(I did not look that far when I saw the XMLwriter.)

You try to write it using the XMLwriter and read it using the dataset.read
XML
(This part only is for the writing of the dataset, see for the reading Kens
message)

Try this
Dim ds as new dataset("CPViewer")
dim dt as new datatable("table1")
ds.tables.add(dt)
dt.columns.add("Height")
etc.
than you can do
dim dr as new datarow = dt.newrow

dr("Height") = InputBoxHeight.text
etc.

dt.add(dr)
ds.writeXML(path)

Maybe it looks more difficult, my expirience is that it is very easy to use.

Cor
 
G

Guest

Cor....I have no idea what your talking about. You must have me or my post confused with someone else

John
 
C

Cor Ligthert

Hi John,

When I hadded sended it I thought that as well sorry, however see my other
post.

Cor
 
G

Guest

Well Cor....Here's the story. It doesn't seem to like the following line

Dim dr As New DataRow(dt.NewRow

It tells me that it is not accessable in this context because it is protected

Also, the following line has an error

dt.add(dt

It tells me that "add" is not a member of "system.data.datatable

What now
Joh
 
C

Cor Ligthert

Hi John,
Well Cor....Here's the story. It doesn't seem to like the following line:

Dim dr As New DataRow(dt.NewRow)
(I do really not know why I wrote this, this one does absolute not exist it
has to be)
dim dr as datarow = dt.newrow
Also, the following line has an error:
dt.add(dt)
I think a typo from you because I wrote
ds.add(dt)

I show it better to you

You declare the ds global because you need it probably in more places.
Private ds as dataset

Than in your load event of your form you check using the fileinfo if the
XMLfile exist.

When the file exist you do ds.readXML(filepath)

When not you create an empty dataset

ds = new dataset("CPViewer")
'this create an empty dataset
dim dt as new datatable("Form1")
'this create an empty table
ds.tables.add(dt)
'this set a referenence from the dataset to the table, as normaly is said
it places the table in the dataset.
dt.columns.add("Textbox1Height")
dt.columns.add("Textbox2Width")
'this add columns
You can create a lot of columns
And you create as well a lot of tables just as you like

To fill the items you do,
dim dr as datarow = ds.tables("Form1").newrow
dr("Height")=20
dr("Widht")=21
ds.tables("Form1").rows.add(dr)

I hope this goes better?

Cor
 
G

Guest

Cor....That's better. It seems to work now. Here is my code:

Dim ds As New DataSet("CPViewer")
Dim dt1 As New DataTable("P1JoyUp")
Dim dt2 As New DataTable("P1JoyLeft")
ds.Tables.Add(dt1)
dt1.Columns.Add("Height")
dt1.Columns.Add("Width")
ds.Tables.Add(dt2)
dt2.Columns.Add("Height")
dt2.Columns.Add("Width")

Dim dr1 As DataRow = ds.Tables("P1JoyUp").NewRow
dr1("Height") = lblP1JoyUp.Height
dr1("Width") = lblP1JoyUp.Width
Dim dr2 As DataRow = ds.Tables("P1JoyLeft").NewRow
dr2("Height") = lblP1JoyLeft.Width
dr2("Width") = lblP1JoyLeft.Width

ds.Tables("P1JoyUp").Rows.Add(dr1)
ds.Tables("P1JoyLeft").Rows.Add(dr2)

ds.WriteXml("C:\CPViewer\CPViewer.XML")

Now, how am I going to handle writing out the fore color, back color and font of the labels and the background image of the form? This was the part that gave me trouble before. Keep it simple, I'm a n00b.

Thank for your help,
John
 
C

Cor Ligthert

Hi John,

You mean something as this
\\\
Dim label1fontcolor As String = Me.Label1.ForeColor.ToString
Dim label1font As String = Me.Label1.Font.ToString
Dim label1backcolor As String = Me.Label1.BackColor.ToString
///
You have to extract them of course to use them again, have as well a look at
the split for that.

I saw in other messages you are not using the background image as set in the
resource so I assume that is just saving the path.

I hope this helps?

Cor
Cor....That's better. It seems to work now. Here is my code:
Now, how am I going to handle writing out the fore color, back color and
font of the labels and the background image of the form? This was the part
that gave me trouble before. Keep it simple, I'm a n00b.
 
J

John E. Crouse

Well Cor. Time to get started on the reader part. I have pretty much
followed ALL you advice and now have an XML file. After I position all the
labels where I want them and set their properties I then open a
savefiledialog box and write out the xml file. Here it is:

<?xml version="1.0" standalone="yes" ?>
- <CPViewer>
- <P1JoyUp>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1JoyUp>
- <P1JoyRight>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1JoyRight>
- <P1JoyDown>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1JoyDown>
- <P1JoyLeft>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1JoyLeft>
- <P1B1>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1B1>
- <P1B2>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1B2>
- <P1B3>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1B3>
- <P1B4>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1B4>
- <P1B5>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1B5>
- <P1B6>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1B6>
- <P1B7>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1B7>
- <P1B8>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1B8>
- <P2JoyUp>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2JoyUp>
- <P2JoyRight>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2JoyRight>
- <P2JoyDown>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2JoyDown>
- <P2JoyLeft>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2JoyLeft>
- <P2B1>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2B1>
- <P2B2>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2B2>
- <P2B3>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2B3>
- <P2B4>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2B4>
- <P2B5>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2B5>
- <P2B6>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<ForeColor>Color [White]</ForeColor>
<Visible>False</Visible>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2B6>
- <P2B7>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2B7>
- <P2B8>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2B8>
- <P1JoyType>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P1JoyType>
- <P2JoyType>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</P2JoyType>
- <GameName>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</GameName>
- <NumPlayers>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</NumPlayers>
- <History>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</History>
- <Undocumented>
<Top>10</Top>
<Left>10</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>False</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0,
GdiVerticalFont=False]</Font>
</Undocumented>
- <MainForm>
<Width>640</Width>
<Height>480</Height>
<Background>C:\CPViewer\BackgroundImage.jpg</Background>
</MainForm>
</CPViewer>

Now I want to be able to read it back in when I choose the xml file in my
openfiledialog box. It should then apply all of the settings in the xml
file. Get me started here. Give me some advice on how to start.

Thanks,
John
 
C

Cor Ligthert

Hi John,

I asume you did write it, I do not think you are able to serialize and show
it yet.
So therefore I think your question is something else than this.

Basicly it is
ds.WriteXML(at a place)
that you did
ds as new dataset
ds.ReadXML(at the same place)

Mystring as string = ds.tables("table1").Rows(0).("MyItem").ToString

And than you have to set thing, from which is the font difficult and the
hight easy.
I show only now the height after that reply again if you can not to the font
however try it first yourself, it cannot be that I write completly your
project.

label.height=CInt(Mystring)
This can of course as well in one time.

However you have to look every time to the right casting.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vagrptypeconversion.asp

Cor
 
J

jcrouse

All done Cor. I can now save a layout to xml and open a layout from xml. As
you mentioned the colors and font are a little trickier. I am trying to
figure out now how to read them back in since the are a string in the xml
file.

Thanks,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top