DataSet.GetXml 不返回空结果结果、DataSet、GetXml

2023-09-06 04:56:52 作者:独揽帅氕

每当我使用 DataSet.GetXml 将 DatSet 转换为 XML 时,任何 null 值都会被忽略,所以,我期望这样:

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>

我得到了这个:

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

有什么快速而肮脏的方法来处理这个问题吗?谢谢

Any quick and dirty way to handle this? Thanks

我认为解决方案是使用 WriteXml.谁能给我提供一个使用它而不写入文件但像 GetXml 一样获取字符串的示例?谢谢

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

推荐答案

这很好用:

        //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);
 
精彩推荐