另一个XML写作问题 [英] Another XML Writing Question

查看:58
本文介绍了另一个XML写作问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的一些代码


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_ForeCo lor",Label2.ForeColor

''myXmlTextWriter.WriteElementString(" label2_BackCo lor",Label2.BackColor


myXmlTextWriter.WriteEndElement(

myXmlTextWriter.Flush(

myXmlTextWriter.Close(


)正如你所知道的那样评论我不知道如何写出除文本字符串以外的任何内容。如何写出其他属性,如字体类型,颜色和图像


谢谢

Joh

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_ForeCo lor", Label2.ForeColor
''myXmlTextWriter.WriteElementString("label2_BackCo lor", 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

推荐答案

John,

使用XmlTextWriter,我就是你的标签。对于一个图像,

我会使用WriteBase64或WriteBinHex。对于Font我会写每个元素

的Font,For Color我会使用Color.ToString。你也许可以在Font上使用

ToString ...


但我考虑使用嵌套元素,例如:
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.WriteElementStrin g(" 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",& br />
label2.BackColor.ToString())

myXmlTextWriter.WriteEndElement()

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


希望这有帮助

Jay


" jcrouse" <一个******* @ discussions.microsoft.com>在消息中写道

news:35 ********************************** @ microsof t.com ...以下是我的一些代码:

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&q uot;,Label2.Width)
''myXmlTextWriter.WriteElementString(" label2_Font",Label2.Font)
''myXmlTextWriter.WriteElementString(" label2_ForeCo lor",
Label2.ForeColor) ''myXmlTextWriter.WriteElementString(" label2_BackCo lor",
Label2.BackColor)
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.Flush()
myXmlTextWriter.Close()

你可以通过评论告诉我,我不知道除了
文本字符串之外怎么写出来。如何写出其他属性,如字体类型,颜色

和图像?
谢谢,
John
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" <an*******@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com... 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_ForeCo lor", Label2.ForeColor) ''myXmlTextWriter.WriteElementString("label2_BackCo lor", 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,
John



好吧,我得到了嵌套元素。字体和颜色不起作用,背景图像也不起作用。我可以将参数写入XML文件但无法读取。我无法弄清楚语法。这是我写出参数的代码


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 (表格

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.H eight.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) 。关闭(


这里是它创建的XML文件


< CPViewer>< Form>< Height> 600< /高度><宽度> 800℃; /宽度><背景和GT; C:\CPViewer\BackgroundImage.jpg< /背景与GT;< /表格>< Label2的><顶部和GT; 58< /顶部和GT;<左,GT ; 42< / Left><高度> 40< /高度><宽度> 140< /宽度><字体> [字体:名称= Arial,尺寸= 9.75,单位= 3,GdiCharSet = 0,GdiVe rticalFont = False]< / Font>< ForeColor>颜色[白色]< / ForeColor>< BackColor>颜色[红色]< / BackColor>< / Label2>< / CPViewer


但我无法阅读


背景行给出了这个错误


未处理的类型'异常' 'System.ArgumentException''发生在system.windows.forms.dl

附加信息:无法绑定到目标控件上的属性''Background''

并且字体和颜色行会出现此错误


mscorlib.dl中出现未处理的System.InvalidCastException类型异常

其他信息:无效从System.String转换为System.Drawing.Font


我尝试了你的建议,但我猜我的语法不正确


谢谢

Joh

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


我真的不知道你要做什么,通过,它看起来像一个工作

表示动态属性。 - Jared
http://msdn.microsoft.com/library/de...ingstorage.asp


" jcrouse" <一个******* @ discussions.microsoft.com>在留言中写道

新闻:3C ********************************** @ microsof t.com ...
I don''t really know what you are trying to do, through, it looks like a job
for dynamic properties. - Jared
http://msdn.microsoft.com/library/de...ingstorage.asp

"jcrouse" <an*******@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
好吧,我得到了嵌套元素。字体和颜色不起作用,背景图像也不起作用。我可以将参数
写入XML文件,但无法读取。我无法弄清楚语法。这里
是我写出参数的代码:

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",&br /> OpenFileDialog1.FileName)
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteStartElement(" Label2")
myXmlTextWriter.WriteElementString(" Top",Label2.Top.ToString())
myXmlTextWriter.WriteElementString(" Left",Label2.Left.ToString())
myXmlTextWr iter.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()

这是它创建的XML文件:

< CPViewer>< Form>< Height> 600< / Height>< ;宽度> 800℃; /宽度><背景和GT; C:\CPViewer\BackgroundImage.jpg< /背景与GT;< /表格>< Label2的><顶部和GT; 58< /顶部和GT;<左和GT; 42℃; /左><身高> 40℃; /高度><宽度&克t; 140< / Width>< Font> [字体:
Name = Arial,Size = 9.75,Units = 3,GdiCharSet = 0,
GdiVerticalFont = False]< / Font>< ForeColor> ;颜色
[白色]< / ForeColor>< BackColor>颜色[红色]< / BackColor>< / Label2>< / CPViewer>

但我可以''读入。

背景行给出了这个错误:

系统中出现未处理的System.ArgumentException类型异常。
system.windows.forms .dll
附加信息:无法绑定到目标
控件上的属性背景。

字体和颜色线会出现此错误:

mscorlib.dll中发生了'System.InvalidCastException'类型的未处理异常
附加信息:从System.String无效转换为
System.Drawing.Font。

我尝试了你的建议但猜测我的语法不正确。

谢谢你,
John
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())
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.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



这篇关于另一个XML写作问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆