DataSet.GetXml not returning null results

江枫思渺然 提交于 2019-12-05 00:19:59

问题


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:


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();



回答2:


This works fine:

        //convert to xml with the DataSet schema:
        StringWriter writer = new StringWriter();
        ds.WriteXml(writer, XmlWriteMode.WriteSchema);
        string xml = writer.ToString();

        //Convert from xml to DataSet:
        StringReader stringReader = new StringReader(response);
        DataSet ds = new DataSet();
        ds.ReadXml(stringReader);


来源:https://stackoverflow.com/questions/963760/dataset-getxml-not-returning-null-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!