VB Net 的 XMLWriter 的 2 个问题
我使用 VB .net 的 XmlTextWriter 将内容写入 XML 文件
启动 xmlwriter 的代码是:
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)
是否可以将 param="on" 添加到 XML 文件的第一行?这样它看起来就像:
<?xml version="1.0" encoding="ISO-8859-1" param="on"?>
下一个问题可能是一个愚蠢的问题:)但我就是想不出来。 我尝试向 XML 文件添加文档类型,例如:
<!DOCTYPE Test SYSTEM "test/my.dtd">
但是,当我尝试设置它时,出现一些错误。
XMLobj.WriteDocType("Test", null, "test/my.dtd", null)
我得到的错误是:
'null' is not declared. 'Null' constant is no longer supported; use 'System.DBNull' instead.
但是,当我尝试用 System.DBNull 替换 null 时,我得到错误:
'DBNull' is a type in 'System' and cannot be used as an expression.
doctype def 的结果应该是这样的:
<!DOCTYPE Test SYSTEM "test/my.dtd">
提前感谢您的帮助!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题 1:
您想要做的是附加 开头,以
"?>"
结尾。要将 PI 添加到 XML 文件,您需要使用
XmlTextWriter
类的WriteProcessingInstruction
方法。每个 PI 都有两个部分:目标和值,这是WriteProcessingInstruction
方法接受的两个参数。因此,在您的情况下,您将编写以下代码来附加处理指令:
这将产生:
问题 2:
C# 的
null
的 VB.NET 等效项是什么都没有
。该关键字指定值类型的默认值或引用类型的空值。您不应使用
System.DBNull
除非你正在处理数据库。DBNull
表示未初始化的变体或不存在的数据库列。它不等同于Nothing
或null
。我同意您收到的第一条错误消息充其量只是令人困惑。因此,将 DocType 写入 XML 文件的行应该是:
它将生成:
Question 1:
What it looks like you're trying to do is append a "processing instruction" to your XML file. A processing instruction (PI) is a tag that encodes application-specific information, beginning with
"<?"
and ending with"?>"
.To add a PI to your XML file, you need to use the
WriteProcessingInstruction
method of theXmlTextWriter
class. There are two parts to every PI, a target and a value, and these are the two parameters accepted by theWriteProcessingInstruction
method.So, in your case, you would write the following code to append the processing instruction:
which would produce:
Question 2:
The VB.NET equivalent of C#'s
null
isNothing
. This keyword either specifies the default value of a value type, or a null value of a reference type.You should not use
System.DBNull
unless you're dealing with databases.DBNull
represents an uninitialized variant or non-existent database column. It is not equivalent toNothing
ornull
. I agree that the first error message you get is confusing at best.So, instead, the line to write the DocType to the XML file should be:
which will produce:
我有关于“null”的答案 - 在 VB.net 中它被称为“Nothing”
I have answer about "null" - it's called "Nothing" in VB.net