如何使用自定义的IXmlSerializable作为XmlAttribute? [英] How to use a custom IXmlSerializable as an XmlAttribute?

查看:224
本文介绍了如何使用自定义的IXmlSerializable作为XmlAttribute?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施的IXmlSerializable 下面编码一个RGB颜色值作为一个字符串类型:

I implement IXmlSerializable for the type below which encodes a RGB color value as a single string:

public class SerializableColor : IXmlSerializable
{
    public int R { get; set; }
    public int G { get; set; }
    public int B { get; set; }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        var data = reader.ReadString();
        reader.ReadEndElement();
        var split = data.Split(' ');
        R = int.Parse(split[0]);
        G = int.Parse(split[1]);
        B = int.Parse(split[2]);
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteString(R + " " + G + " " + B);
    }
}



由于它是一个字符串,我想存放作为一个属性,以节省空间。但只要我添加了 [XmlAttribute] 来我的财产,我得到以下异常:

Since it's a single string, I wanted to store it as an attribute to save space. But as soon as I add the [XmlAttribute] to my property, I get the following exception:

{无法序列类型SerializableColor成员'颜色'。XmlAttribute / XMLTEXT不能用来编码类型执行IXmlSerializable的。}

{"Cannot serialize member 'Color' of type SerializableColor. XmlAttribute/XmlText cannot be used to encode types implementing IXmlSerializable."}

有没有一种方法,使之作为一个属性的工作呢?

Is there a way to make it work as an attribute too?

推荐答案

该错误意味着正是它说。当IXmlSerializable的实现,因为预计的IXmlSerializable XML序列化到完全自定义,你不能使用这些XML序列化的属性。如果你想使类序列化使用XmlSerializer的属性,你可以做到这一点。

The error means exactly what it says. You cannot use these XML serialization attributes when IXmlSerializable is implemented because IXmlSerializable expects the XML serialization to be completely customized. You can do this if you want to make the class serializable with XmlSerializer using the attributes.

[XmlRoot("SerializableColor")]
public class SerializableColor
{
    [XmlAttribute("R")]
    public int R { get; set; }
    [XmlAttribute("G")]
    public int R { get; set; }
    [XmlAttribute("B")]
    public int B { get; set; }    
}



另外,对于您的实现XmlSerializable的:

Also, for your implementation of XmlSerializable:

    public void ReadXml(XmlReader reader)
    {
        string data = null;

        reader.MoveToAttribute("Color");
        if (reader.ReadAttributeValue())
        {
            data = reader.Value;
        }
        reader.ReadEndElement();

        var split = data.Split(' ');
        R = int.Parse(split[0]);
        G = int.Parse(split[1]);
        B = int.Parse(split[2]);
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteAttributeString("Color", R + " " + G + " " + B);
    }

如果,在另一方面,你正在寻找能够做是一个有颜色,是可逆的短字符串represenation,都一看的 ColorTranslator类。特别是看到FromHtml和ToHtml方法。

If, on the other hand, all you are looking to be able to do is have a short string represenation of a color that is reversible, have a look at the ColorTranslator Class. In particular, see the FromHtml and ToHtml methods.

这篇关于如何使用自定义的IXmlSerializable作为XmlAttribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆