Omitting XML processing instruction when serializing an object

前端 未结 6 1882
别那么骄傲
别那么骄傲 2020-12-14 23:44

I\'m serializing an object in a C# VS2003 / .Net 1.1 application. I need it serialized without the processing instruction, however. The XmlSerializer class puts out somethin

6条回答
  •  情话喂你
    2020-12-14 23:46

    If by "processing instruction" you mean the xml declaration, then you can avoid this by setting the OmitXmlDeclaration property of XmlWriterSettings. You'll need to serialize using an XmlWriter, to accomplish this.

    XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
    StringBuilder builder = new StringBuilder();
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;
    
    using ( XmlWriter stringWriter = new StringWriter(builder, settings) )
    {
        serializer.Serialize(stringWriter, comments);
        return builder.ToString();
    }
    

    But ah, this doesn't answer your question for 1.1. Well, for reference to others.

提交回复
热议问题