views:

977

answers:

2

When a typed DataSet is written to XML using ds.GetXml the columns with null values are removed from the XML. I know this is because the schema is not written etc. but is there a way to override this without having to parse through the DataSet and convert all database nulls to empty string before writing the XML?


EDIT: additional information:

1)My main task is to take the DataSet and stream it to a WebRequest. So I am creating an XML doc

Dim xmldoc As New Xml.XmlDocument()
xmldoc.LoadXml(dsUpload.GetXml)

and then uploading the xmldoc on a HTTpWebRequest(req) like this

Using xw As XmlTextWriter = 
  New XmlTextWriter(req.GetRequestStream, System.Text.Encoding.Default)
    xw.Formatting = Formatting.Indented
    xmldoc.WriteTo(xw)
    xw.Close()
End Using

2) I am talking about DBNull

3) I need these columns to appear because the site I am uploading to needs all the tags to be included so they can parse it ( must be a legacy system)


EDIT

1) I agree the code is not efficient,but what i failed to mention is the uploading method is generic method in another class. Hence it accepts a XMLDoc because this same method services the upload of dataset as xml and a hardcoded string of XML

2) Yes agree that only the System.String can be transformed to String.Empty.And thats mostly what my dataset has.

3) LOL about the legacy system comment. Here is more ridiculous stuff.I think they probably parse the XML like they would a comma separated file.Anytime there is a comma in the data it throws it off!! :)

+1  A: 

First of all, I recommend that you never process XML as a string. You should always use an API that is sensitive to the rules of XML. In this case, you should use one of the overloads of DataSet.WriteXml.

Second, when you say "null", are you referring to DBNull? That is, a NULL in database terms, or are you referring to the value null in C# and Nothing in VB.NET?

Finally, why do you want these columns to appear? They're only taking up space.


Edits:

  1. Correction to code: this code is inefficient: saving the DataSet to an XML string, then parsing the XML again to load into an XmlDocument, and then finally writing out the XML again to the request stream. It also uses XmlTextWriter, which is deprecated as of .NET 2.0. The following is a lot cleaner:

    Dim settings As New XmlWriterSettings With {.Indent = True}
    Using stream = req.GetRequestStream
        Using writer = XmlWriter.Create(stream, settings)
            dsUpload.WriteXml(writer, XmlWriteMode.IgnoreSchema)
        End Using
    End Using
    
  2. From research, there doesn't seem to be an easy way to force the columns to appear. You could replace all the DBNull.Value values with the default value for their type (note String.Empty is only going to work if the .NET type of the column is string. It's not going to work for a DateTime column, for instance). Once set to something other than DBNull.Value, I think you're going to have to go through all the columns in the table, and set their AllowDBNull property to false.

What really needs to happen is that the site you're uploading to needs to learn XML. It's disgraceful in this day and age for software to not understand XML well enough for you to supply an XML Schema in order for them to understand which columns to expect.

If you feel comfortable saying which site this is, I'm sure we'll all be glad to publicly ridicule them.

:-)

John Saunders
+1  A: 

1)My main task is to take the dataset and stream it to a webrequest. So I am creating an xml doc

Dim xmldoc As New Xml.XmlDocument()
xmldoc.LoadXml(dsUpload.GetXml)

and then uploading the xmldoc on a HTTpWebRequest(req) like this

Using xw As XmlTextWriter = New XmlTextWriter(req.GetRequestStream, System.Text.Encoding.Default)
    xw.Formatting = Formatting.Indented
    xmldoc.WriteTo(xw)
    xw.Close()
End Using

2) I am talking about DBNull

3) I need these columns to appear because the site I am uploading to needs all the tags to be included so they can parse it ( must be a legacy system)

Next time, you should edit your original question to include any new information. This time, I'll do it for you.
John Saunders
sorry.I will take note of that!