我正在C#VS2003/.Net 1.1应用程序中序列化一个对象.但是,我需要在没有处理指令的情况下进行序列化.XmlSerializer类推出类似这样的东西:
Data More Data
有没有办法得到类似下面的内容,而不处理生成的文本删除标签?
Data More Data
对于那些好奇的人,我的代码看起来像这样......
XmlSerializer serializer = new XmlSerializer(typeof(MyObject)); StringBuilder builder = new StringBuilder(); using ( TextWriter stringWriter = new StringWriter(builder) ) { serializer.Serialize(stringWriter, comments); return builder.ToString(); }
小智.. 9
我做了一个小小的修正
XmlSerializer serializer = new XmlSerializer(typeof(MyObject)); StringBuilder builder = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; using ( XmlWriter stringWriter = XmlWriter.Create(builder, settings) ) { serializer.Serialize(stringWriter, comments); return builder.ToString(); }
Marc Gravell.. 7
在2.0中,您将使用XmLWriterSettings.OmitXmlDeclaration,并序列化为XmlWriter - 但我认为这不存在于1.1中; 所以并不完全有用 - 但只是再考虑一次"考虑升级"的事情......是的,我发现它并不总是可行的.
我做了一个小小的修正
XmlSerializer serializer = new XmlSerializer(typeof(MyObject)); StringBuilder builder = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; using ( XmlWriter stringWriter = XmlWriter.Create(builder, settings) ) { serializer.Serialize(stringWriter, comments); return builder.ToString(); }
在2.0中,您将使用XmLWriterSettings.OmitXmlDeclaration,并序列化为XmlWriter - 但我认为这不存在于1.1中; 所以并不完全有用 - 但只是再考虑一次"考虑升级"的事情......是的,我发现它并不总是可行的.