C# DataSet.WriteXml 到字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/963870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:21:18  来源:igfitidea点击:

DataSet.WriteXml to string

c#.netdataset

提问by pistacchio

I'm tring to get a string from a DataSet withoutusing GetXml. I'm using WriteXml, instead. How to use it to get a string? Thanks

我想在使用 GetXml 的情况下从 DataSet 中获取字符串。我正在使用 WriteXml。如何使用它来获取字符串?谢谢

采纳答案by Mehrdad Afshari

StringWriter sw = new StringWriter();
dataSet.WriteXml(sw);
string result = sw.ToString();

回答by Jon Skeet

Write to a StringWriter, and then call ToStringon that.

写入 a StringWriter,然后调用ToString它。

Note that if you want the generated XML declaration to specify UTF-8 instead of UTF-16, you'll need something like my Utf8StringWriter.

请注意,如果您希望生成的 XML 声明指定 UTF-8 而不是 UTF-16,您将需要类似 my Utf8StringWriter.

回答by DareDevil

here is the vb.net code:

这是 vb.net 代码:

 Private Function GenerateXML(ByVal ds As DataSet) As String
    Dim obj As New StringWriter()
    Dim xmlstring As String
    ds.WriteXml(obj)
    xmlstring  = obj.ToString()
    Return xmlstring 
End Function