views:

1159

answers:

3

Hi, whenever I convert a DatSet into an XML with DataSet.GetXml, any null value is ignored, so, where i expect this:

<value1>a</value1>
<value2></value2>
<value3>c</value3>

I get this instead:

<value1>a</value1>
<value3>c</value3>

Any quick and dirty way to handle this? Thanks

EDIT: I think a solution would be using WriteXml. Could anyone provide me with a sample of using it WITHOUT writing to a file but getting a string just like GetXml does? Thanks

+1  A: 

The problem is listed here in the Microsoft KB article:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q317961

The problem is that you have no schema attached to your dataset that specifies that that element should be written out.

I don't believe that using WriteXml will solve the problem, as the documentation states, "Calling this method is identical to calling WriteXml with XmlWriteMode set to IgnoreSchema." but you are free to try - here is the equivalent code:

StringWriter sw = new StringWriter();
ds.WriteXml(sw);
string outputXml = sw.ToString();
samjudson
What about calling it with XmlWriteMode set to WriteSchema?
pistacchio
I'll be honest I'm not sure - all that should do is include an inline schema in the output - I doubt it changes the actual XML itself.
samjudson
A: 

Hi Sam, I tried your function and I get the following error at the line row[column] = default(column.DataType);

and the error is: The type or namespace name 'column' could not be found(are you missing a using directive or an assembly reference?)

By the way, I am using VS 2008 and C#. I would appreciate your response in this regrrd. You can respond to [email protected]

Thanks Pankaja

A: 

I've edited this, from my previous answer.

I believe that's a limitation on how DataSet exports its data to XML. Even specifying the WriteMode to write the schema, the resulting XML still lacks elements when their values are null.

StringWriter sw = new StringWriter();
ds.WriteXml(sw, XmlWriteMode.WriteSchema);

If you can avoid having null values in your DataSet, replacing them for string.Empty or any other default value, the corresponding elements will be present:

<value1>a</value1>
<value2 />
<value3>c</value3>
Hilton Perantunes
@Hilton: this is not an answer to the question, so I'm going to downvote it. Please take the time to read the [FAQ](http://stackoverflow.com/faq). You'll see this isn't a discussion forum, so your "answer" was not appropriate.
John Saunders
@John, I probably failed to make my point. I've edited the answer to try making it clearer.
Hilton Perantunes
@Hilton: this still doesn't answer the question. And the issue isn't that the XML is not output - the issue is having a program that expects the null value to be explicitly present.
John Saunders
@John, I respectfully disagree, as @pistacchio didn't say that null values needed to be present and he just asked for "any quick and dirty way to handle this".
Hilton Perantunes
@Hilton: have you read the FAQ yet? What you wrote would be fine for a discussion forum, but this is not a discussion forum.
John Saunders
@John: Yes, I did. I've edited my answer again. I'll be glad to accept a downvote if my answer is wrong (and I'll be happy to know a way to solve a problem I'm currently having). Thank you for your help.
Hilton Perantunes
@Hilton: this is closer to it. As a nod to the new user, I'll remove the downvote.
John Saunders