! An end tag cannot be added for an empty XML Node during serialization.

Source: Internet
Author: User

To use XmlSerializer to serialize an object, it is required that the empty node be expressed as the start and end marking pairs:

Current format: <Wards/>

Expected format: <Wards> </Wards>

I searched for it online. C #-XmlSerializer Serialize empty variable to use both tags? The solution is:

Public class XmlTextWriterFull: XmlTextWriter {
Public XmlTextWriterFull (Stream stream, Encoding enc): base (stream, enc ){}
Public XmlTextWriterFull (String str, Encoding enc): base (str, enc ){}
Public XmlTextWriterFull (TextWriter sink): base (sink ){}

Public override void WriteEndElement (){
Base. WriteFullEndElement ();
}
}

 

Then I call:

MemoryStream stream = new MemoryStream ();
XmlSerializer serializer = new XmlSerializer (message. GetType ());
XmlTextWriter writer = new XmlTextWriterFull (stream, Encoding. Unicode );

Serializer. Serialize (writer, message );
StreamReader reader = new StreamReader (stream, Encoding. Unicode );
Stream. Position = 0;
String xml = reader. ReadToEnd ();
Stream. Close ();
Writer. Close ();
Reader. Close ();

 

Unfortunately, it never succeeds.

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.