Xml De-Serialize使用

               首先引入 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;
           
        }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值