Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

c# - XML serialization force full closing tag on null or empty value

I have class

public class Testowa
{
    public string test { get; set; }
}

When I serialize it without value for test, I get

<test/>

But I want get

<test></test>

How I can do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Extend XmlWriter

From there,

If you use a code similar to the following for your serialization:

XmlSerializer s = new XmlSerializer(typeof(Testowa));
using (FileStream fs = new FileStream(File, FileMode.CreateNew))
{
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Encoding = Encoding.GetEncoding("ISO-8859-1");
    settings.NewLineChars = Environment.NewLine;
    settings.ConformanceLevel = ConformanceLevel.Document;
    settings.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(fs, settings))
    {
        s.Serialize(writer, this);
    }
}

Replace the last line with a derived version of XmlWriter which will force the serialization of full closing tags:

s.Serialize(new XmlWriterEE(writer), this);

The full XmlWriter derived class:

public class XmlWriterEE :  XmlWriter
{
    private XmlWriter baseWriter;

    public XmlWriterEE(XmlWriter w)
    {
        baseWriter = w;
    }

    //Force WriteEndElement to use WriteFullEndElement
    public override void WriteEndElement() {baseWriter.WriteFullEndElement(); }

    public override void WriteFullEndElement()
    {
        baseWriter.WriteFullEndElement();
    }

    public override void Close()
    {
        baseWriter.Close();
    }

    public override void Flush()
    {
        baseWriter.Flush();
    }

    public override string LookupPrefix(string ns)
    {
        return (baseWriter.LookupPrefix(ns));
    }

    public override void WriteBase64(byte[] buffer, int index, int count)
    {
        baseWriter.WriteBase64(buffer, index, count);
    }

    public override void WriteCData(string text)
    {
        baseWriter.WriteCData(text);
    }

    public override void WriteCharEntity(char ch)
    {
        baseWriter.WriteCharEntity(ch);
    }

    public override void WriteChars(char[] buffer, int index, int count)
    {
        baseWriter.WriteChars(buffer, index, count);
    }

    public override void WriteComment(string text)
    {
        baseWriter.WriteComment(text);
    }

    public override void WriteDocType(string name, string pubid, string sysid, string subset)
    {
        baseWriter.WriteDocType(name, pubid, sysid, subset);
    }

    public override void WriteEndAttribute()
    {
        baseWriter.WriteEndAttribute();
    }

    public override void WriteEndDocument()
    {
        baseWriter.WriteEndDocument();
    }

    public override void WriteEntityRef(string name)
    {
        baseWriter.WriteEntityRef(name);
    }

    public override void WriteProcessingInstruction(string name, string text)
    {
        baseWriter.WriteProcessingInstruction(name, text);
    }

    public override void WriteRaw(string data)
    {
        baseWriter.WriteRaw(data);
    }

    public override void WriteRaw(char[] buffer, int index, int count)
    {
        baseWriter.WriteRaw(buffer, index, count);
    }

    public override void WriteStartAttribute(string prefix, string localName, string ns)
    {
        baseWriter.WriteStartAttribute(prefix, localName, ns);
    }

    public override void WriteStartDocument(bool standalone)
    {
        baseWriter.WriteStartDocument(standalone);
    }

    public override void WriteStartDocument()
    {
        baseWriter.WriteStartDocument();
    }

    public override void WriteStartElement(string prefix, string localName, string ns)
    {
        baseWriter.WriteStartElement(prefix, localName, ns);
    }

    public override WriteState WriteState
    {
        get { return baseWriter.WriteState; }
    }

    public override void WriteString(string text)
    {
        baseWriter.WriteString(text);
    }

    public override void WriteSurrogateCharEntity(char lowChar, char highChar)
    {
        baseWriter.WriteSurrogateCharEntity(lowChar, highChar);
    }

    public override void WriteWhitespace(string ws)
    {
        baseWriter.WriteWhitespace(ws);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...