C # XML serialization removes the XML default namespace and declaration header

Source: Internet
Author: User

Removing XML namespaces and declaring headers does not affect deserialization.

Directly on the code:

Serialize this object int obj = 1; XmlSerializer serializer = new XmlSerializer (obj. GetType ());//Serialize the object output to a file FileStream stream = new FileStream ("Hh.xml", FileMode.Create); XmlWriterSettings settings = new XmlWriterSettings (); settings. Indent = true;settings. IndentChars = "    "; settings. Newlinechars = "\ r \ n"; settings. Encoding = encoding.utf8;//settings. Omitxmldeclaration = true;  does not generate a declaration header using (XmlWriter XmlWriter = XmlWriter.Create (stream, Settings)) {    //enforces the specified namespace, overriding the default namespace    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces ();    namespaces. ADD (String. Empty, String. Empty);    Serializer. Serialize (XmlWriter, obj, namespaces);    Xmlwriter.close ();}; Stream. Close ();


C # XML serialization removes the XML default namespace and declaration header

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.