首先引入 System.Xml.
在程序中加入:
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.IO;
在定义串行化类时,遵守以下规则:
1.定义一个类,使用类名与XML的根结点名相同;
2. 定义属性时,使用"[XmlAttribute]";
3.定义子结点时, 使用"[XmlElementAttribute("结点名")]";
其中, 最重要的一点是 : 所有的变量名必须与结点名一致.
下面是了一个简单的例子:
XML:
<processchain name="theName">
<!-- Global Properties -->
<property name="prop1" value="value1"/>
<property name="prop2" value="${prop1}.value2"/>
<property name="prop3" value="value3"/>
<filters start="ONE" input="xmlinput"> <!-- start attribute specify which filter to start -->
<!-- Chain of filters -->
<filter name="xslt" id="ONE">
<property name="xmlinput" value="${xmlfile}" />
<property name="xslinput" value="C:/Development/Ix/testdata/XsltTest/DocumentServiceData.xsl" />
<!--if value="${data}" equals="XX" -->
<case break="no">
<or>
<condition value="${output}" eq="c:/test.xml"/>
<and>
<condition value="${output}" eq="fdeee"/>
<condition value="${output}" ne="555"/>
</and>
</or>
<route to="THREE"/>
</case>
<case break="yes">
<and>
<or>
<condition value="${output}" eq="c:/test.xml"/>
<condition value="${output}" eq="c:/test.xml"/>
</or>
<and>
<condition value="${output}" eq="c:/test.xml"/>
<condition value="${output}" ne="c:/ddfdfdf.xml"/>
</and>
</and>
<route to="THREE"/>
<route to="FOUR"/>
</case>
<caseelse>
<route to="THREE"/>
<route to="FOUR"/>
</caseelse>
</filter>
<filter name="outputmgr" id="THREE">
<property name="input" value="${output}" />
</filter>
</filters>
</processchain>
对应的反串行化类为:
using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.IO;
// [Serializable]
namespace XmlDeSerializeTest
{
/// <summary>
/// Asociated with the Xml, and to de-serialize the xml
///
/// </summary>
public class processchain
{
[XmlAttribute]
public string name;
[XmlElementAttribute("property")]
public Property[] property{get; set; }
[XmlElementAttribute("filters")]
public Filters[] filters{get; set;}
public class Property
{
[XmlAttribute]
public string name;
[XmlAttribute]
public string value;
}
public class Filters
{
[XmlAttribute]
public string start;
[XmlAttribute]
public string input;
[XmlElementAttribute("filter")]
public Filter[] filter{get; set;}
}
public class Filter
{
[XmlAttribute]
public string name;
[XmlAttribute]
public string id;
[XmlElementAttribute("property")]
public Property[] property{get; set;}
[XmlElementAttribute("case")]
public Case[] @case{get; set;}
public CaseElse caseelse;
}
public class Case
{
[XmlAttribute]
public string @break;
[XmlElementAttribute("or")]
public Or[] or;
[XmlElementAttribute("and")]
public And[] and;
[XmlElementAttribute("route")]
public Route[] route;
}
public class Or
{
[XmlElementAttribute("condition")]
public Condition[] condition{get; set;}
[XmlElementAttribute("or")]
public Or[] or;
[XmlElementAttribute("and")]
public And[] and;
}
public class And
{
[XmlElementAttribute("condition")]
public Condition[] condition{get; set;}
[XmlElementAttribute("or")]
public Or[] or;
[XmlElementAttribute("and")]
public And[] and;
}
public class Route
{
[XmlAttribute]
public string to;
}
public class CaseElse
{
[XmlElementAttribute("route")]
public Route[] route;
}
public class Condition
{
[XmlAttribute]
public string lvalue;
[XmlAttribute]
public string @operator;
[XmlAttribute]
public string rvalue;
}
}
}