1

Im writing stuff to an XML file using VB .net´s XmlTextWriter

The code to start the xmlwriter is:

 Dim XMLobj As Xml.XmlTextWriter
 Dim enc As System.Text.Encoding
 enc = System.Text.Encoding.GetEncoding("ISO-8859-1")
 XMLobj = New Xml.XmlTextWriter("C:\filename.xml", enc)

Is it possible to add param="on" to the first line of the XML file? So that it will look like:

<?xml version="1.0" encoding="ISO-8859-1" param="on"?>

The next question might be a stupid one :) but I just can't figure it out. I try to add a doctype to the XML file like:

<!DOCTYPE Test SYSTEM "test/my.dtd">

However when I try to set this up I get some errors.

XMLobj.WriteDocType("Test", null, "test/my.dtd", null)

The error I get is:

'null' is not declared. 'Null' constant is no longer supported; use 'System.DBNull' instead.

However when I try to replace null with System.DBNull I get the error:

'DBNull' is a type in 'System' and cannot be used as an expression.

The result of the doctype def should be like:

<!DOCTYPE Test SYSTEM "test/my.dtd">

Thanks in advance for your help!

4

2 回答 2

3

问题一:

您正在尝试做的似乎是将“处理指令”附加到您的 XML 文件中。处理指令 (PI) 是对特定于应用程序的信息进行编码的标签,以 . 开头"<?"和结尾"?>"

要将 PI 添加到 XML 文件中,您需要使用类的WriteProcessingInstruction方法XmlTextWriter。每个 PI 有两个部分,一个目标和一个值,这是该WriteProcessingInstruction方法接受的两个参数。

因此,在您的情况下,您将编写以下代码来附加处理指令:

XMLobj.WriteProcessingInstruction("xml", "version=""1.0"" encoding=""ISO-8859-1"" param=""on""")

这将产生:

<?xml version="1.0" encoding="ISO-8859-1" param="on"?>


问题2:

C# 的 VB.NET 等价物nullNothing. 此关键字指定值类型的默认值或引用类型的空值。

System.DBNull除非您正在处理数据库,否则您不应该使用。DBNull表示未初始化的变体或不存在的数据库列。它等于Nothingor null。我同意您收到的第一条错误消息充其量是令人困惑的。

因此,将 DocType 写入 XML 文件的行应该是:

XMLobj.WriteDocType("Test",  Nothing, "test/my.dtd", Nothing)

这将产生:

<!DOCTYPE Test SYSTEM "test/my.dtd">
于 2010-11-21T02:17:42.760 回答
1

我有关于“null”的答案——它在 VB.net 中被称为“Nothing”

于 2010-11-21T01:26:39.463 回答