Another XML Writing Question

G

Guest

Here is some of my code

Dim myXmlTextWriter As XmlTextWriter = New XmlTextWriter("C:\CPViewer\CPViewer.xml", System.Text.Encoding.Unicode

myXmlTextWriter.Formatting = System.Xml.Formatting.Indente
myXmlTextWriter.WriteStartElement("CPViewer"
myXmlTextWriter.WriteElementString("Height", Me.Height
myXmlTextWriter.WriteElementString("Width", Me.Width
'myXmlTextWriter.WriteElementString("Background", Me.BackgroundImage
myXmlTextWriter.WriteElementString("label2_Top", Label2.Top
myXmlTextWriter.WriteElementString("label2_Left", Label2.Left
myXmlTextWriter.WriteElementString("label2_Height", Label2.Height
myXmlTextWriter.WriteElementString("label2_Width", Label2.Width
'myXmlTextWriter.WriteElementString("label2_Font", Label2.Font
'myXmlTextWriter.WriteElementString("label2_ForeColor", Label2.ForeColor
'myXmlTextWriter.WriteElementString("label2_BackColor", Label2.BackColor

myXmlTextWriter.WriteEndElement(
myXmlTextWriter.Flush(
myXmlTextWriter.Close(

As you can tell by the comments I'm not sure how to write out anything but text strings. How do I write out other properties such as font types, color and images

Thank you
Joh
 
J

Jay B. Harlow [MVP - Outlook]

John,
With an XmlTextWriter, I would just are you are for the label. For an Image,
I would use WriteBase64 or WriteBinHex. For Font I would write each element
of the Font, For Color I would use Color.ToString. You may be able to use
ToString on Font also...

However I consider using nested elements, something like:
Dim myXmlTextWriter As XmlTextWriter = New
XmlTextWriter("C:\CPViewer\CPViewer.xml", System.Text.Encoding.Unicode)
myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartElement("CPViewer")
myXmlTextWriter.WriteElementString("Height", Me.Height)
myXmlTextWriter.WriteElementString("Width", Me.Width)
'myXmlTextWriter.WriteElementString("Background",
Me.BackgroundImage)
myXmlTextWriter.WriteBinHex("Background", form.BackgroundImage)
myXmlTextWriter.WriteStartElement("label2")
myXmlTextWriter.WriteElementString("Top", label2.Top.ToString())
myXmlTextWriter.WriteElementString("Left", label2.Left.ToString())
myXmlTextWriter.WriteElementString("Height",
label2.Height.ToString())
myXmlTextWriter.WriteElementString("Width", label2.Width.ToString())
myXmlTextWriter.WriteStartElement("Font")
myXmlTextWriter.WriteElementString("Bold",
label2.Font.Bold.ToString())
...
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteElementString("ForeColor",
label2.ForeColor.ToString())
myXmlTextWriter.WriteElementString("BackColor",
label2.BackColor.ToString())
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.Flush()
myXmlTextWriter.Close()

Hope this helps
Jay



jcrouse said:
Here is some of my code:

Dim myXmlTextWriter As XmlTextWriter = New
XmlTextWriter("C:\CPViewer\CPViewer.xml", System.Text.Encoding.Unicode)
myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartElement("CPViewer")
myXmlTextWriter.WriteElementString("Height", Me.Height)
myXmlTextWriter.WriteElementString("Width", Me.Width)
'myXmlTextWriter.WriteElementString("Background", Me.BackgroundImage)
myXmlTextWriter.WriteElementString("label2_Top", Label2.Top)
myXmlTextWriter.WriteElementString("label2_Left", Label2.Left)
myXmlTextWriter.WriteElementString("label2_Height", Label2.Height)
myXmlTextWriter.WriteElementString("label2_Width", Label2.Width)
'myXmlTextWriter.WriteElementString("label2_Font", Label2.Font)
'myXmlTextWriter.WriteElementString("label2_ForeColor", Label2.ForeColor)
Label2.BackColor)

myXmlTextWriter.WriteEndElement()
myXmlTextWriter.Flush()
myXmlTextWriter.Close()

As you can tell by the comments I'm not sure how to write out anything but
text strings. How do I write out other properties such as font types, color
and images?
 
G

Guest

Well, I got the nested elements working. The fonts and color are not working, and neither is the background image. I can write the parameters to the XML file but can't read it in. I can't figure out the syntax. Here is my code to write out the parameters

Dim myXmlTextWriter As XmlTextWriter = New XmlTextWriter("C:\CPViewer\CPViewer.xml", System.Text.Encoding.Unicode

myXmlTextWriter.Formatting = System.Xml.Formatting.Indente
myXmlTextWriter.WriteStartElement("CPViewer"
myXmlTextWriter.WriteStartElement("Form"
myXmlTextWriter.WriteElementString("Height", Me.Height
myXmlTextWriter.WriteElementString("Width", Me.Width
myXmlTextWriter.WriteElementString("Background", OpenFileDialog1.FileName
myXmlTextWriter.WriteEndElement(
myXmlTextWriter.WriteStartElement("Label2"
myXmlTextWriter.WriteElementString("Top", Label2.Top.ToString()
myXmlTextWriter.WriteElementString("Left", Label2.Left.ToString()
myXmlTextWriter.WriteElementString("Height", Label2.Height.ToString()
myXmlTextWriter.WriteElementString("Width", Label2.Width.ToString()
myXmlTextWriter.WriteElementString("Font", Label2.Font.ToString()
myXmlTextWriter.WriteElementString("ForeColor", Label2.ForeColor.ToString()
myXmlTextWriter.WriteElementString("BackColor", Label2.BackColor.ToString()
myXmlTextWriter.WriteEndElement(
myXmlTextWriter.WriteEndElement(

myXmlTextWriter.Flush(
myXmlTextWriter.Close(

And here is the XML file it creates

<CPViewer><Form><Height>600</Height><Width>800</Width><Background>C:\CPViewer\BackgroundImage.jpg</Background></Form><Label2><Top>58</Top><Left>42</Left><Height>40</Height><Width>140</Width><Font>[Font: Name=Arial, Size=9.75, Units=3, GdiCharSet=0, GdiVerticalFont=False]</Font><ForeColor>Color [White]</ForeColor><BackColor>Color [Red]</BackColor></Label2></CPViewer

But I can't read in

The background line gives this error

An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dl
Additional information: Cannot bind to property 'Background' on target control

And the font and color lines give this error

An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dl
Additional information: Invalid cast from System.String to System.Drawing.Font

I tried your suggestions but guess I didn't get the syntax correct

Thank you
Joh
 
C

Cor Ligthert

Hi John,

Now you should have readed my text that it is easier when you try to read an
XML dataset to write that as well as a XML dataset. However you did not give
any comments on that in my opinion.

(To give Jay a little extra information as well)

Cor
 
J

Jay B. Harlow [MVP - Outlook]

John,
Hmm...

I was concerned that the Font would add extra info, which it did. Rather
then using Font.ToString it might be easier to use each property of Font, as
you do with Label.

I was not expecting Color to add extra info! If your colors are only named
colors you could use Color.Name to write & Color.FromName to read. However
this fails with non-named colors. You could always use Color.ToArgb &
Color.FromArgb instead.

Hope this helps
Jay

jcrouse said:
Well, I got the nested elements working. The fonts and color are not
working, and neither is the background image. I can write the parameters to
the XML file but can't read it in. I can't figure out the syntax. Here is my
code to write out the parameters:
Dim myXmlTextWriter As XmlTextWriter = New
XmlTextWriter("C:\CPViewer\CPViewer.xml", System.Text.Encoding.Unicode)
myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartElement("CPViewer")
myXmlTextWriter.WriteStartElement("Form")
myXmlTextWriter.WriteElementString("Height", Me.Height)
myXmlTextWriter.WriteElementString("Width", Me.Width)
myXmlTextWriter.WriteElementString("Background", OpenFileDialog1.FileName)
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteStartElement("Label2")
myXmlTextWriter.WriteElementString("Top", Label2.Top.ToString())
myXmlTextWriter.WriteElementString("Left", Label2.Left.ToString())
myXmlTextWriter.WriteElementString("Height", Label2.Height.ToString())
Label2.Width.ToString())
myXmlTextWriter.WriteElementString("Font", Label2.Font.ToString())
myXmlTextWriter.WriteElementString("ForeColor", Label2.ForeColor.ToString())
Label2.BackColor.ToString())
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteEndElement()

myXmlTextWriter.Flush()
myXmlTextWriter.Close()

And here is the XML file it creates:
<Height>40</Height><Width>140</Width><Font>[Font: Name=Arial, Size=9.75,
Units=3 said:
But I can't read in.

The background line gives this error:

An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Cannot bind to property 'Background' on target control.

And the font and color lines give this error:

An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll
Additional information: Invalid cast from System.String to System.Drawing.Font.

I tried your suggestions but guess I didn't get the syntax correct.

Thank you,
John
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
Yes using a DataSet is an option, as is using XmlSerialization.

My concern with XmlSerialization & DataSets, is that they still will not
save Colors, Fonts & Controls, so John is back to where he started. Hence I
was staying with the XmlWriter, as you can manually write out the Colors,
Fonts and Controls.

Hope this helps
Jay
 
C

Cor Ligthert

Hi Jay,
Yes using a DataSet is an option, as is using XmlSerialization.
My concern with XmlSerialization & DataSets, is that they still will not
save Colors, Fonts & Controls, so John is back to where he started. Hence I
was staying with the XmlWriter, as you can manually write out the Colors,
Fonts and Controls.

It is posible to serialize a font object and put that in a dataset however
it can in seperate string characters as well in my opinion.

I do not know if you saw it, Tom supplied this code some weeks ago to this
newsgroup. So the font can just be stored as a string and than be
deserialized.

However I will choose for 3 items in a table that makes the dataset as well
(human) readable.

Cor

\\\Tom Shelton
Private Function SerializeFontObject(ByVal fnt As Font) As String
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream
Try
bf.Serialize(mem, fnt)
Return Convert.ToBase64String(mem.ToArray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function
Private Function DeserializeFontObject(ByVal fnt As String) As Font
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Convert.FromBase64String(fnt))
Try
Return DirectCast(bf.Deserialize(mem), Font)
Finally
If Not mem Is Nothing Then
mem.Close()
End If
End Try
End Function
///
 
C

Cor Ligthert

Hi Jay,

However this is not a should, this is what I prefer because working with a
dataset is so simple, and working with XMLdocuments gives often problems.

(As well with me while I an not unexpirienced with it)

However that is the nice thing from dotNet as I always says, do it the way
that fits you the most (when it is not real bad.)

However when I am not using the dataset, than I would also not use the
ds.ReadXML as that is now done by John,

My reason, the same as I interpretted what you wrote, the complete
flexibility of XML.

Before you understand me wrong

Cor
 
G

Guest

Well I tried to use the ToArgB and FromArgB parameters but I don't seem to understand the syntax

Any more Ideas
John
 
G

Guest

I also tried to use the individual parameters for the font, like, bold, size, name and such. I was able to write them out but still couldn't read them in

Help
John
 
J

Jay B. Harlow [MVP - Outlook]

John,
Did you look up the functions in Help to see samples of how to use them?

To write the file you would use something like:
myXmlTextWriter.WriteElementString("ForeColor",
label2.ForeColor.ToArgB())

In your reader you would use something like:
If myXmlTextReader.NodeType = XmlNodeType.Element AndAlso
myXmlTextReader.Name = "ForeColor" Then
label2.ForColor = Color.FromArgB(myXmlTextReader.Value)
End If


The Font you would need to do the same thing. In your read code create a new
font from the values stored in the XML.

Hope this helps
Jay
 

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

XML Writing and Reading 12
ERROR saving Grid as XML 3

Top