我需要得到一个类的Xml序列化后的字符串
using (System.IO.MemoryStream mem = new System.IO.MemoryStream()) { XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8); XmlSerializer xz = new XmlSerializer(t.GetType()); xz.Serialize(writer, t); writer.Close(); byte[] bytes = mem.ToArray(); return System.Text.Encoding.UTF8.GetString(bytes); }
得到的字符串却被提示格式错误,放到IE会提示如下错误
无法显示 XML 页。 使用 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。 -------------------------------------------------------------------------------- 文档的顶层无效。处理资源 'file:///C:/Documents and Settings/Administrator/桌面/test.txt.xml' 时出错。第 1 行,位置: 1 <?xml version="1.0" encoding="utf-8"?><LocalUIForm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd...
百思不解!查网上,说Trim()一下得到的字符串就可以,试了确实如此。
这提供了另一种方式,利用子类StringWriter
http://stackoverflow.com/questions/1564718/using-stringwriter-for-xml-serialization
默认的StringWriter
得到的序列化的string是
<?xml version="1.0" encoding="utf-16" ?>
using (StringUTF8Writer sw = new StringUTF8Writer()) { XmlSerializer xz = new XmlSerializer(t.GetType()); xz.Serialize(sw, t); return sw.ToString(); }
我希望得到的xml是utf-8,于是简单重写EnCoding属性
public class StringUTF8Writer : System.IO.StringWriter { public override Encoding Encoding { get { return Encoding.UTF8; } } }