下面是类的设置,包含enum类型IterationTypes。序列化和反序列化可直接将xml中的对应字符串反序列化为enum对象。
namespace MoscaCore.Models
{
public enum IterationTypes
{
[XmlEnum(Name = "IOIteration")]
IOIteration = 0,
[XmlEnum(Name = "NodeIteration")]
NodeIteration = 1,
[XmlEnum(Name = "FieldIteration")]
FieldIteration = 2,
}
public class Iteration
{
private IterationTypes iterationType = IterationTypes.IOIteration;//默认的迭代方法
[XmlAttribute("IterationType")]
public IterationTypes IterationType
{
get { return iterationType; }
set { iterationType = value; }
}
private int maxIteration = 20;
[XmlAttribute("MaxIteration")]
public int MaxIteration
{
get { return maxIteration; }
set { maxIteration = value; }
}
private double sigma = 100;
[XmlAttribute("Sigma")]
public double Sigma
{
get { return sigma; }
set { sigma = value; }
}
}
}
在编辑xml的时候可以直接输入enum对应的string了,如下:
<iteration iterationtype="NodeIteration" maxiteration="20" sigma="100"></iteration>
对应的xsd限定写法如下:
<xs:element name="Iteration"> <xs:complextype> <xs:attribute name="MaxIteration" type="xs:int"> <xs:attribute name="Sigma" type="xs:double"> <xs:attribute name="IterationType"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:enumeration value="IOIteration"> <xs:enumeration value="NodeIteration"> <xs:enumeration value="FieldIteration"> </xs:enumeration></xs:enumeration></xs:enumeration></xs:restriction> </xs:simpletype> </xs:attribute> </xs:attribute></xs:attribute></xs:complextype> </xs:element>