对象类字段中的系统颜色

时间:2016-06-09 09:16:11

标签: c# .net xml serialization

我正在从XML文件创建一个对象类,然后想要分配一个不在XML文件中的字段,即:Color

我的序列化如下:

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class pack
{
    public steps steps { get; set; }           
}

    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class steps
    {
        readonly ChildCollection<step> Steps;           

        [System.Xml.Serialization.XmlElementAttribute("step")]
        public Collection<step> StepList { get { return Steps; } } 
    }
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class step
{
    [System.Xml.Serialization.XmlElementAttribute("step")]
    public Collection<step> StepList { get { return Steps; } }
    [System.Xml.Serialization.XmlAttributeAttribute("name")]
    public string Name { get; set; }
    [System.Xml.Serialization.XmlAttributeAttribute("id")]
    public string Id { get; set; }
    [System.Xml.Serialization.XmlAttributeAttribute("Color")]
    public System.Drawing.Color Color { get; set; }
}

如您所见,在代码的最后一行中,我创建了一个颜色字段,然后在另一个函数中为其指定了一些变量颜色。

pack XmlFilePack = new pack();
using (FileStream stream = File.OpenRead(@"Steps_1.xml"))
{
    try
    {
        XmlSerializer serializer = new XmlSerializer(typeof(pack));
        XmlFilePack = (pack)serializer.Deserialize(stream);
    }
    finally
    {
        //close file
        stream.Close();
    }
}

但我null总是XmlFilePack。有谁知道错误在哪里?

更新:这是XML文件:

<pack >
    <steps>
        <step id ="12" name="S1" >
        </step>
        <step id ="1" name="S1" >
            <step id ="2" name="S11" >
                <step id ="3" name="S111" >
                    <step id ="5" name="S1121" >
                    </step>
                </step>
            </step>
            <step id ="6" name="S12" >              
                <step id ="4" name="S112" >
                    <step id ="14" name="S112" >
                    </step>
                </step>     
            </step>
        </step>
    </steps>
</pack>

2 个答案:

答案 0 :(得分:1)

System.Drawing.Color课程在序列化方面表现不佳。我所发现的是,通常最好像所描述的那样包装这样的类,例如here

基本上将Color转换为序列化的等效HTML值

String HtmlColor = System.Drawing.ColorTranslator.ToHtml(MyColorInstance);

但是在课堂上。

答案 1 :(得分:0)

非常感谢您的回答和评论。我发现以下答案也适用:

我将Color定义为string

[System.Xml.Serialization.XmlAttributeAttribute("Color")]
public string Color { get; set; }

然后写了这段代码

System.Drawing.Color colstep1 = System.Drawing.ColorTranslator.FromHtml(xNode.Color);
var col1 = System.Drawing.ColorTranslator.FromHtml(xNode.Color);
childTreeNode.ForeColor = col1;

它以非常好的方式管理。