How can I make the xmlserializer only serialize plain xml?

前端 未结 4 438
遇见更好的自我
遇见更好的自我 2020-12-02 09:54

I need to get plain xml, without the at the beginning and xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-i

4条回答
  •  执笔经年
    2020-12-02 10:07

    This will write the XML to a file instead of a string. Object ticket is the object that I am serializing.

    Namespaces used:

    using System.Xml;
    using System.Xml.Serialization;
    

    Code:

    XmlSerializerNamespaces emptyNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
    
    XmlSerializer serializer = new XmlSerializer(typeof(ticket));
    
    XmlWriterSettings settings = new XmlWriterSettings
    {
        Indent = true,
        OmitXmlDeclaration = true
    };
    
    using (XmlWriter xmlWriter = XmlWriter.Create(fullPathFileName, settings))
    {
        serializer.Serialize(xmlWriter, ticket, emptyNamespaces); 
    }
    

提交回复
热议问题