Vb.Net writing xml node with null value

rflorencio
rflorencio used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

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

Commented:
Can you please post some code?

Author

Commented:
     
The code is to long, part of the code

 rdr.Read()
        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
            .WriteStartDocument(True)
       
            .WriteStartElement("General")
            .WriteAttributeString("Type", "t:Type1")
            .WriteStartElement("cust")
            .WriteElementString("CustID", rdr("id"))
            .WriteElementString("Company", rdr("company"))
               
                .WriteStartElement("Address")
                .WriteElementString("Address", rdr("address"))
                .WriteElementString("Country", rdr("country"))
                .WriteEndElement()
                '
           
           
            .WriteEndElement()

Jorge PaulinoIT Pro/Developer
Top Expert 2008

Commented:
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

Commented:
* filed  should be field

Commented:
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.

Author

Commented:
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:

<Type>Refound</Type>

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

<Type/>

But i want appear like this:

 <Type></Type>

Commented:
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.

Commented:
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:
<Type>zzzxxxccc</Type>
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

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


Jorge PaulinoIT Pro/Developer
Top Expert 2008

Commented:
What framework are you using?

Author

Commented:
For me no problem, but for some people is. Framework 4.0.
IT Pro/Developer
Top Expert 2008
Commented:
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
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/A_2897-Xml-Literals.html
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 -->
                      <General>
                          <type>"Fixed Value"</type>
                          <cust><%= parameter.ToString() %></cust>
                      </General>

        xmlFile.Save("d:\test.xml")

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