[XmlRoot("MachineData", Namespace = "")]
public class MachineMessage<T> : BaseEntity
{
#region == Fields =
private List<T> _content;
#endregion
#region == Properties ==
public T Content
{
get { return _content; }
set { _content = value; }
}
#endregion
}
[XmlType("product")]
public class Product : BaseEntity
{
#region == Fields ==
private string _name;
private string[] _parameters;
#endregion
#region == Properties ==
[XmlAttribute("customName")]
public string Name
{
get { return _name; }
set { _name = value; }
}
[XmlArray]
public string[] Parameters
{
get { return _parameters; }
set { _parameters = value; }
}
#endregion
}
[XmlType(TypeName="tool")]
public class Tool : BaseEntity
{
#region == Fields ==
private string _name;
private int _position;
#endregion
#region == Properties ==
[XmlAttribute("name")]
public string Name
{
get { return _name; }
set { _name = value; }
}
[XmlAttribute("position")]
public int Position
{
get { return _position; }
set { _position = value; }
}
#endregion
}
class Program
{
static void Main(string[] args)
{
MachineMessage<tool> withTool = new MachineMessage<tool>
{
Content =new Tool
{
Name = "new tool",
Position = 33
}
};
string withToolSer = withTool.Serialize();
MachineMessage<tool> withToolDes = BaseEntity.Deserialize<machinemessage><tool>>(withToolSer, Encoding.UTF8);
MachineMessage<product> withProduct = new MachineMessage<product>
{
Content = new Product
{
Name = "new Product",
Parameters = new string[]
{
"first",
"second",
"third"
}
}
};
string withProductSer = withProduct.Serialize();
MachineMessage<product> withProdDes = BaseEntity.Deserialize<machinemessage><product>>(withProductSer, Encoding.UTF8);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
Result:
="1.0"="utf-8"
<MachineData>
<Content name="new tool" position="33" />
</MachineData>
Goal:
="1.0"="utf-8"
<MachineData>
<tool name="new tool" position="33" />
</MachineData>