XML序列化强制null或空值上的完整结束标记

时间:2016-11-04 12:34:44

标签: c# xml serialization

我有课

sum((component-constant)**2)

当我将其序列化而没有测试值时,我得到了

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

但我想得到

<test/>

我怎么做?

2 个答案:

答案 0 :(得分:8)

扩展XmlWriter

来自there

如果您使用类似于以下代码进行序列化:

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);
    }
}

将最后一行替换为XmlWriter的派生版本,这将强制完整结束标记的序列化:

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

完整的XmlWriter派生类:

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);
    }
}

答案 1 :(得分:0)

如果您不使用FileStream,则可以使用StringBuilder。 Fab帖子中的XmlWriterEE类。

    public static string SerializeObject(this object obj)
    {
        var settings = new XmlWriterSettings
        {
            Encoding = Encoding.GetEncoding("ISO-8859-1"),
            NewLineChars = Environment.NewLine,
            ConformanceLevel = ConformanceLevel.Auto,
            OmitXmlDeclaration = true,
            WriteEndDocumentOnClose = true,
            Indent = true
        };           

        var stream = new StringBuilder();

        using (var writer = XmlWriter.Create(stream, settings))
        {
            var xmlSerializer = new XmlSerializer(obj.GetType());
            var namespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
            xmlSerializer.Serialize(new XmlWriterEE(writer), obj, namespaces);                
        }
        return stream.ToString();
    }