Question

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!

Was it helpful?

Solution

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">

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top