VB Net 的 XMLWriter 的 2 个问题

发布于 2024-10-03 22:14:08 字数 1045 浏览 4 评论 0 原文

我使用 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">

提前感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极度宠爱 2024-10-10 22:14:08

问题 1:

您想要做的是附加 开头,以 "?>" 结尾。

要将 PI 添加到 XML 文件,您需要使用 XmlTextWriter 类的WriteProcessingInstruction 方法。每个 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# 的 null 的 VB.NET 等效项是 什么都没有。该关键字指定值类型的默认值或引用类型的空值。

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

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

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

它将生成:

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

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 the XmlTextWriter class. There are two parts to every PI, a target and a value, and these are the two parameters accepted by the WriteProcessingInstruction method.

So, in your case, you would write the following code to append the processing instruction:

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

which would produce:

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

Question 2:

The VB.NET equivalent of C#'s null is Nothing. 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 to Nothing or null. 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:

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

which will produce:

<!DOCTYPE Test SYSTEM "test/my.dtd">
可爱暴击 2024-10-10 22:14:08

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

I have answer about "null" - it's called "Nothing" in VB.net

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文