下面的Product类中的几个属性应该如何使用呢?1、[Serializable()] ?(该类可序列化?)
2、[XmlRoot("product")]、 ?(该类可以转换为一个Xml的节点?)
3、[XmlAttribute("name")] ?(该属性可以由xml的一个属性加载到Product类?)
---------------------------------------------------------------------------------------
[Serializable()]
[XmlRoot("product")]
public class Product
{
private string productId;
private double productPrice;
private string productName; /// <summary>
/// Creates a new instance of ExceptionPolicyData.
/// </summary>
public Product()
{
} public Product(string id, string name, double price)
{
this.ProductID = id;
this.ProductName = name;
this.ProductPrice = price;
} /// <summary>
/// Gets or sets the Policy name.
/// </summary>
[XmlAttribute("name")]
public string ProductName
{
get { return this.productName; }
set { this.productName = value; }
} [XmlAttribute("id")]
public string ProductID
{
get { return this.productId; }
set { this.productId = value; }
} [XmlAttribute("price")]
public double ProductPrice
{
get { return this.productPrice; }
set { this.productPrice = value; }
} public override string ToString()
{
string res = "Product: ID=" + this.productId +
", Name=" + this.productName +
", Price=" + Convert.ToString(this.productPrice);
return res;
}
}
参阅Attribute特性的相关章节可以深入了解。
在序列化Product类时,序列化程序会读取这些特性并进行序列化……