![](/img/trans.png)
[英]VB.NET: XMLWriter() check if directory exists and if file exists, otherwise create them
[英]2 problems with VB Net's XMLWriter
我使用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"?>
下一個問題可能是一個愚蠢的問題:)但我無法弄明白。 我嘗試將doctype添加到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">
在此先感謝您的幫助!
問題1:
您嘗試做的事情是在XML文件中附加“處理指令” 。 處理指令(PI)是編碼特定於應用程序的信息的標記,以"<?"
開頭"<?"
並以"?>"
結尾。
要將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是Nothing
。 此關鍵字指定值類型的默認值或引用類型的空值。
除非您正在處理數據庫,否則不應使用System.DBNull
。 DBNull
表示未初始化的變體或不存在的數據庫列。 它不等於Nothing
或null
。 我同意您收到的第一條錯誤消息充其量是令人困惑的。
因此,將DocType寫入XML文件的行應該是:
XMLobj.WriteDocType("Test", Nothing, "test/my.dtd", Nothing)
這將產生:
<!DOCTYPE Test SYSTEM "test/my.dtd">
我有回答“null” - 它在VB.net中被稱為“Nothing”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.