Omitting XML processing instruction when serializing an object

前端 未结 6 1843
别那么骄傲
别那么骄傲 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

    This works in .NET 1.1. (But you should still consider upgrading)

        XmlSerializer s1= new XmlSerializer(typeof(MyClass)); 
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add( "", "" );
    
    
        MyClass c= new MyClass();
        c.PropertyFromDerivedClass= "Hallo";
    
        sw = new System.IO.StringWriter();
        s1.Serialize(new XTWND(sw), c, ns);
        ....
    
       /// XmlTextWriterFormattedNoDeclaration
       /// helper class : eliminates the XML Documentation at the
       /// start of a XML doc. 
       /// XTWFND = XmlTextWriterFormattedNoDeclaration
       public class XTWFND : System.Xml.XmlTextWriter
       {
         public XTWFND(System.IO.TextWriter w) : base(w) { Formatting = System.Xml.Formatting.Indented; }
         public override void WriteStartDocument() { }
       }
    

提交回复
热议问题