0

I am creating an object class from an XML file and then would like to assign one field which is not int the XML file, namely: Color.

My serialization is as follows:

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

As you see, in the last line from the code I am creating a color field and then in the other function will assign some variable color for that.

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

But I have always null for the XmlFilePack. Does anyone know where is the error?

Update: Here is the XML file:

<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>
Saber
  • 133
  • 1
  • 9

2 Answers2

1

The System.Drawing.Color class doesn't play well with serialization. What I've found is that it's often best to wrap classes like this as described, for example, here.

Basically converting the Color to the equivalent HTML value for serialization

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

but wrapped in a class.

Community
  • 1
  • 1
fredrik
  • 6,483
  • 3
  • 35
  • 45
0

Thanks a lot for your answers and comments. I found out the following answer will work, too:

I defined Color as a string

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

and then wrote this code

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

It works very nice and easy somehow to manage.

Saber
  • 133
  • 1
  • 9