Create a class. The attributes in the class come from an xml file. The attribute name is the same as that of the xml node. Reflection is used to assign values to each attribute in order not to be judged one by one.
Class myclass
{
// Set attributes
Public string profileid {get; set ;}
Public string url {get; set ;}
Public DateTime startdate {get; set ;}
Public bool bonus {get; set ;}
Public int max {get; set ;}
// This function is called internally, so it is set to private.
Private void setProperties ()
{
String propertyname = "";
XmlReader xr = XmlReader. Create (@ "a. xml ");
While (xr. Read ())
{
If (xr. IsStartElement ())
{
// Assign values to attributes
Propertyname = xr. LocalName. ToLower ();
Xr. Read ();
If (propertyname! = "")
{
// Check whether the property exists. If yes, the value is assigned. Otherwise, an error occurs.
If (this. GetType (). GetProperty (propertyname )! = Null)
{
// Convert. ChangeType is used for implementation.
// However, if the value of bonus in xml is 1, an error occurs. It seems that it cannot be converted to the bool type. Therefore, in xml, I forced to write false and true.
This. GetType (). GetProperty (propertyname). SetValue (this, Convert. ChangeType (xr. Value, this. GetType (). GetProperty (propertyname). PropertyType), null );
}
}
}
}
}
}