Vb.Net writing xml node with null value

rflorencio used Ask the Experts™
Using System.Xml.XmlTextWriter class to create a xml file, when a field is null the tag element is not closed, and causes structure error in xml.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Can you please post some code?


The code is to long, part of the code

        Dim writexml As System.Xml.XmlTextWriter
        writexm = New System.Xml.XmlTextWriter("myfile.xml", System.Text.Encoding.GetEncoding(1252))
        With writexml
            .Indentation = 4
            .IndentChar = " "
            .Formatting = .Indentation
            .WriteAttributeString("Type", "t:Type1")
            .WriteElementString("CustID", rdr("id"))
            .WriteElementString("Company", rdr("company"))
                .WriteElementString("Address", rdr("address"))
                .WriteElementString("Country", rdr("country"))

Jorge PaulinoIT Pro/Developer
Top Expert 2008

Try to change the filed rdr("id") to rdr("id").ToString()
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Jorge PaulinoIT Pro/Developer
Top Expert 2008

* filed  should be field

My friend,

I just tested your code and works perfectly. The problem is not with null values, since if any field is null, then a runtime error should occur:
Conversion from type 'DBNull' to type 'String' is not valid.

Can you please be more specific about what happens? Which element remains unclosed? Also, post a part of the xml output, which shows the problem.


In fact I think I not clarified properly the question, in fact the structure has no error.

For ex:
if db field is filled with "Refound" for ex. the xml tag appear like this:


But if db field is null the xml element appear like this:


But i want appear like this:


Well, <Type/> is exactly the same as  <Type></Type>.

Now, if you definitely want it to appear like  <Type></Type>, I am not sure you can do that with XmlTextWriter. I will look into it and get back to you.

Well, I tried a few things but all resulted in the <Type /> output, so (as far as I know), if you really want it to appear like <Type></Type> you will have to "hack" it.

in your code, whenever you see null, you replace it with something extreme, ex. zzzxxxccc and place that into your xml. So, when you Flush the XmlTextWriter, the output would be:
Then you re-open the file as text, do a string replace of "zzzxxxccc"  with "" and save the file again!

Not very pretty, but works! :-)
Jorge PaulinoIT Pro/Developer
Top Expert 2008

But what's the problem of <Type/> ?

Jorge PaulinoIT Pro/Developer
Top Expert 2008

What framework are you using?


For me no problem, but for some people is. Framework 4.0.
IT Pro/Developer
Top Expert 2008
Have you tried the ToString() method?
If you are using .NET 3.5 or above (that's the case), you can use XML Literals and it's easy that way to handle xml files. Check this article that I have wrote for EE
Here's an simple example how you can use :

        ' Null value
        Dim parameter As Integer?

        Dim xmlFile = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                      <!-- Comment -->
                          <type>"Fixed Value"</type>
                          <cust><%= parameter.ToString() %></cust>


Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial