我可以使用 C# 将 XML 直接序列化为字符串而不是 Stream 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1138414/
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
Can I Serialize XML straight to a string instead of a Stream with C#?
提问by John Bubriski
This exampleuses a StringWriter
to hold the serialized data, then calling ToString()
gives the actual string
value:
此示例使用 aStringWriter
来保存序列化数据,然后调用ToString()
给出实际string
值:
Person john = new Person();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, john);
string serializedXML = stringWriter.ToString();
Is there any easier/Cleaner way to do this? All of the Serialize()
overloads seem to use a Stream
or Writer.
有没有更简单/更清洁的方法来做到这一点?所有的Serialize()
重载似乎都使用 aStream
或 Writer。
UPDATE:Asked a similar question about serializing an IEnumerable via an Extension Method.
更新:询问了一个关于通过扩展方法序列化 IEnumerable的类似问题。
采纳答案by Matthew Whited
Fun with extension methods...
扩展方法的乐趣...
var ret = john.ToXmlString()
public static class XmlTools
{
public static string ToXmlString<T>(this T input)
{
using (var writer = new StringWriter())
{
input.ToXml(writer);
return writer.ToString();
}
}
public static void ToXml<T>(this T objectToSerialize, Stream stream)
{
new XmlSerializer(typeof(T)).Serialize(stream, objectToSerialize);
}
public static void ToXml<T>(this T objectToSerialize, StringWriter writer)
{
new XmlSerializer(typeof(T)).Serialize(writer, objectToSerialize);
}
}
回答by John Bubriski
I created this helper method, but I haven't tested it yet. Updated the code per orsogufo's comments (twice):
我创建了这个辅助方法,但我还没有测试它。根据 orsogufo 的评论更新代码(两次):
private string ConvertObjectToXml(object objectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(objectToSerialize.GetType());
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, objectToSerialize);
return stringWriter.ToString();
}
回答by Paolo Tedesco
More or less your same solution, just using an extension method:
或多或少相同的解决方案,只需使用扩展方法:
static class XmlExtensions {
// serialize an object to an XML string
public static string ToXml(this object obj) {
// remove the default namespaces
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
// serialize to string
XmlSerializer xs = new XmlSerializer(obj.GetType());
StringWriter sw = new StringWriter();
xs.Serialize(sw, obj, ns);
return sw.GetStringBuilder().ToString();
}
}
[XmlType("Element")]
public class Element {
[XmlAttribute("name")]
public string name;
}
class Program {
static void Main(string[] args) {
Element el = new Element();
el.name = "test";
Console.WriteLine(el.ToXml());
}
}
回答by Necromancer
Seems like no body actually answered his question, which is no, there is no way to generate an XML string without using a stream or writer object.
似乎没有人真正回答了他的问题,不,没有办法在不使用流或编写器对象的情况下生成 XML 字符串。