Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
XML
[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
   }


C#
[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
   }



C#
[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:

XML
<?xml version="1.0" encoding="utf-8"?>
<MachineData>
  <Content name="new tool" position="33" />
</MachineData>



Goal:

XML
<?xml version="1.0" encoding="utf-8"?>
<MachineData>
  <tool name="new tool" position="33" />
</MachineData>
Posted
Updated 13-Jul-15 9:47am
v3
Comments
Sergey Alexandrovich Kryukov 28-Jun-15 11:24am    
Not clear. This is a code dump, not a question. Without proper explanations, it makes no sense. Please use "Improve question" above.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900