简体   繁体   English

使用XmlSerializer创建和使用自定义XML特殊字符

[英]Creating and using custom XML special characters with XmlSerializer

I have a ColorFormat class that stores basic information about a color format. 我有一个ColorFormat类,用于存储有关颜色格式的基本信息。 The aim is to be able to serialize and deserialize to and from XML. 目的是能够对XML进行序列化和反序列化。 To represent Red, Green and Blue I use special color string identifiers: 为了表示红色,绿色和蓝色,我使用特殊的颜色字符串标识符:

public const string RedColorIdentifier = "&red;";
public const string GreenColorIdentifier = "&green;";
public const string BlueColorIdentifier = "&blue;";

For a format like "#RGB", the class format string is as such: 对于“ #RGB”之类的格式,类格式字符串如下:

colorFormat.Format = "#" + ColorFormat.RedColorIdentifier +
                           ColorFormat.GreenColorIdentifier +
                           ColorFormat.BlueColorIdentifier;

Ideally, the serialized XML should be: 理想情况下,序列化的XML应该是:

<ColorFormat Name="HexFmt" ColorBase="Hex">#&red;&green;&blue;</ColorFormat>

The actual serialization is: 实际的序列化为:

<ColorFormat Name="HexFmt" ColorBase="Hex">#&amp;red;&amp;green;&amp;blue;</ColorFormat>

I was wondering if there is a way of "serializing and deserializing" your own custom special XML character 我想知道是否存在一种“序列化和反序列化”您自己的自定义特殊XML字符的方法

You can use CData to wrap special characters. 您可以使用CData来包装特殊字符。
From MSDN CDATA Section 从MSDN CDATA部分

For example class below will be serialized witt color values wrapped with CData 例如下面的类将是序列化的witt颜色值,并用CData包装

[XmlType("ColorFormat")]
public class ColorFormat
{
    [XmlAttribute]
    public string Name { get; set; }

    [XmlAttribute]
    public string ColorBase { get; set; }

    [XmlIgnore]
    public string Format { get; set; }

    [XmlText]
    public XmlNode[] SerializableFormat
    {
        get
        {
            var doc = new XmlDocument();
            return new XmlNode[] { doc.CreateCDataSection(this.Format) };
        }
        set
        {
            this.Format = value[0].Value;
        }
    }
}

Using of ColorFormat class 使用ColorFormat

const string FORMAT = "&red;&green;&blue;";

var format = new ColorFormat
{
    Name = "HexFormat",
    ColorBase = "Hex",
    Format = FORMAT
};

var serializer = new XmlSerializer(typeof(ColorFormat));
using (var writer = new StringWriter())
{
    serializer.Serialize(writer, format);
    Console.WriteLine(writer.ToString());
}

Finally found it, gotta implement IXmlSerializable as such: 终于找到了,必须像这样实现IXmlSerializable:

public class ColorFormat : IXmlSerializable
{
    ...

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        Name = reader.GetAttribute(nameof(Name));
        ColorBase = CommonUtil.ParseStringToEnum<NumberBase>(reader.GetAttribute(nameof(ColorBase)));
        Format = reader.ReadInnerXml();
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteAttributeString(nameof(Name), Name);
        writer.WriteAttributeString(nameof(ColorBase), ColorBase.ToString());
        writer.WriteRaw(Format);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM