asp.net webserver 中 XmlElement 的使用

1.建立一個ToosTemp.asmx文件

2.其中定義一個方法,取得產品的分類:

        [WebMethod(EnableSession = true, Description = "取得商品分類")]
        public GetProdClasssResponse GetProdClass(string merchantUid, string ShopCode)
        {
            GetProdClassBll bllGetProdClass = new GetProdClassBll();
            return bllGetProdClass.GetProdClass(merchantUid, ShopCode);
        }

3.建立輸出GetProdClasssResponse實體:

    [Serializable]

    [XmlRoot("GetProdClasssResponse")]  

    public class GetProdClasssResponse

    {

        private GetProdClass getProdClass;



        public GetProdClass GetProdClass {

            set { getProdClass = value; }

            get { return getProdClass; }

        }

    }

4.建立其他實體,使用到的:

View Code
    public class GetProdClass

    {

        private bool _ResultType;



        public bool ResultType

        {

            get { return _ResultType; }

            set { _ResultType = value; }

        }



        private string _ErrorCode;



        public string ErrorCode

        {

            get { return _ErrorCode; }

            set { _ErrorCode = value; }

        }

        private string _ErrorMsg;



        public string ErrorMsg

        {

            get { return _ErrorMsg; }

            set { _ErrorMsg = value; }

        }

        private List<ProProdClass> _ProdClass;



        [XmlElement(ElementName = "ProdClass")]

        public List<ProProdClass> ProdClass

        {

            set

            { _ProdClass = value; }

            get {

                return _ProdClass;

            }

        }

    }



    public class ProProdClass

    {

        private string _ID;



        public string ID

        {

            get { return _ID; }

            set { _ID = value; }

        }

        private string _ClassName;



        public string ClassName

        {

            get { return _ClassName; }

            set { _ClassName = value; }

        }

        private string _Parent;



        public string Parent

        {

            get { return _Parent; }

            set { _Parent = value; }

        }

        private int _Kind;



        public int Kind

        {

            get { return _Kind; }

            set { _Kind = value; }

        }



    }

5.如果沒有  [XmlElement(ElementName = "ProdClass")] 這句:

生成的xml如下:

View Code
<?xml version="1.0" encoding="utf-8"?>

<GetProdClasssResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">

  <GetProdClass>

    <ResultType>true</ResultType>

    <ProdClass>

      <ProProdClass>

        <ID>8dc84093-55ef-4bb7-b49e-27a5f285fb03</ID>

        <ClassName>點心</ClassName>

        <Parent>0</Parent>

        <Kind>1</Kind>

      </ProProdClass>

      <ProProdClass>

        <ID>d63f2b60-c869-48d6-8af5-ecdc03ec1177</ID>

        <ClassName>飲料</ClassName>

        <Parent>0</Parent>

        <Kind>1</Kind>

      </ProProdClass>

      <ProProdClass>

        <ID>bb007659-c359-404b-ba86-f4bc73305b42</ID>

        <ClassName>漢堡</ClassName>

        <Parent>0</Parent>

        <Kind>1</Kind>

      </ProProdClass>

      <ProProdClass>

        <ID>56505c54-e81b-46d6-a1ad-257fd43356c5</ID>

        <ClassName>冷飲</ClassName>

        <Parent>d63f2b60-c869-48d6-8af5-ecdc03ec1177</Parent>

        <Kind>2</Kind>

      </ProProdClass>

      <ProProdClass>

        <ID>ba8acf1c-5af7-464f-bd15-f4b65296961f</ID>

        <ClassName>湯類</ClassName>

        <Parent>d63f2b60-c869-48d6-8af5-ecdc03ec1177</Parent>

        <Kind>2</Kind>

      </ProProdClass>

      <ProProdClass>

        <ID>c8143a27-51b9-4421-99a7-3e6a3b682d28</ID>

        <ClassName>甜點</ClassName>

        <Parent>8dc84093-55ef-4bb7-b49e-27a5f285fb03</Parent>

        <Kind>2</Kind>

      </ProProdClass>

      <ProProdClass>

        <ID>39a9d3b2-7df0-4f47-828b-43da6c15fcf7</ID>

        <ClassName>沙拉</ClassName>

        <Parent>8dc84093-55ef-4bb7-b49e-27a5f285fb03</Parent>

        <Kind>2</Kind>

      </ProProdClass>

      <ProProdClass>

        <ID>647f4aea-74c0-4883-8972-45f181401574</ID>

        <ClassName>熱飲</ClassName>

        <Parent>d63f2b60-c869-48d6-8af5-ecdc03ec1177</Parent>

        <Kind>2</Kind>

      </ProProdClass>

      <ProProdClass>

        <ID>87414a0a-62ed-4b9d-b6f2-463b9860647f</ID>

        <ClassName>米漢堡</ClassName>

        <Parent>bb007659-c359-404b-ba86-f4bc73305b42</Parent>

        <Kind>2</Kind>

      </ProProdClass>

      <ProProdClass>

        <ID>41682ed6-746e-426e-b689-528a4a9751ef</ID>

        <ClassName>熱狗堡</ClassName>

        <Parent>bb007659-c359-404b-ba86-f4bc73305b42</Parent>

        <Kind>2</Kind>

      </ProProdClass>

      <ProProdClass>

        <ID>0193b67f-97de-46c4-807b-c86d4572dfa9</ID>

        <ClassName>油炸</ClassName>

        <Parent>8dc84093-55ef-4bb7-b49e-27a5f285fb03</Parent>

        <Kind>2</Kind>

      </ProProdClass>

      <ProProdClass>

        <ID>0fb3b45c-bba4-4289-aa24-decc79128aec</ID>

        <ClassName>漢堡</ClassName>

        <Parent>bb007659-c359-404b-ba86-f4bc73305b42</Parent>

        <Kind>2</Kind>

      </ProProdClass>

    </ProdClass>

  </GetProdClass>

</GetProdClasssResponse>

6.如果加上后,生成的如下:

View Code
<?xml version="1.0" encoding="utf-8"?>

<GetProdClasssResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">

  <GetProdClass>

    <ResultType>true</ResultType>

    <ProdClass>

      <ID>8dc84093-55ef-4bb7-b49e-27a5f285fb03</ID>

      <ClassName>點心</ClassName>

      <Parent>0</Parent>

      <Kind>1</Kind>

    </ProdClass>

    <ProdClass>

      <ID>d63f2b60-c869-48d6-8af5-ecdc03ec1177</ID>

      <ClassName>飲料</ClassName>

      <Parent>0</Parent>

      <Kind>1</Kind>

    </ProdClass>

    <ProdClass>

      <ID>bb007659-c359-404b-ba86-f4bc73305b42</ID>

      <ClassName>漢堡</ClassName>

      <Parent>0</Parent>

      <Kind>1</Kind>

    </ProdClass>

    <ProdClass>

      <ID>56505c54-e81b-46d6-a1ad-257fd43356c5</ID>

      <ClassName>冷飲</ClassName>

      <Parent>d63f2b60-c869-48d6-8af5-ecdc03ec1177</Parent>

      <Kind>2</Kind>

    </ProdClass>

    <ProdClass>

      <ID>ba8acf1c-5af7-464f-bd15-f4b65296961f</ID>

      <ClassName>湯類</ClassName>

      <Parent>d63f2b60-c869-48d6-8af5-ecdc03ec1177</Parent>

      <Kind>2</Kind>

    </ProdClass>

    <ProdClass>

      <ID>c8143a27-51b9-4421-99a7-3e6a3b682d28</ID>

      <ClassName>甜點</ClassName>

      <Parent>8dc84093-55ef-4bb7-b49e-27a5f285fb03</Parent>

      <Kind>2</Kind>

    </ProdClass>

    <ProdClass>

      <ID>39a9d3b2-7df0-4f47-828b-43da6c15fcf7</ID>

      <ClassName>沙拉</ClassName>

      <Parent>8dc84093-55ef-4bb7-b49e-27a5f285fb03</Parent>

      <Kind>2</Kind>

    </ProdClass>

    <ProdClass>

      <ID>647f4aea-74c0-4883-8972-45f181401574</ID>

      <ClassName>熱飲</ClassName>

      <Parent>d63f2b60-c869-48d6-8af5-ecdc03ec1177</Parent>

      <Kind>2</Kind>

    </ProdClass>

    <ProdClass>

      <ID>87414a0a-62ed-4b9d-b6f2-463b9860647f</ID>

      <ClassName>米漢堡</ClassName>

      <Parent>bb007659-c359-404b-ba86-f4bc73305b42</Parent>

      <Kind>2</Kind>

    </ProdClass>

    <ProdClass>

      <ID>41682ed6-746e-426e-b689-528a4a9751ef</ID>

      <ClassName>熱狗堡</ClassName>

      <Parent>bb007659-c359-404b-ba86-f4bc73305b42</Parent>

      <Kind>2</Kind>

    </ProdClass>

    <ProdClass>

      <ID>0193b67f-97de-46c4-807b-c86d4572dfa9</ID>

      <ClassName>油炸</ClassName>

      <Parent>8dc84093-55ef-4bb7-b49e-27a5f285fb03</Parent>

      <Kind>2</Kind>

    </ProdClass>

    <ProdClass>

      <ID>0fb3b45c-bba4-4289-aa24-decc79128aec</ID>

      <ClassName>漢堡</ClassName>

      <Parent>bb007659-c359-404b-ba86-f4bc73305b42</Parent>

      <Kind>2</Kind>

    </ProdClass>

  </GetProdClass>

</GetProdClasssResponse>

7.對比如下:

如果有這個語句,生成的xml是少一個實體的。可以把list定義的實體給去掉。

8. [XmlElement(ElementName = "ProdClass1")] 可以定義輸出的實體名稱

        private List<ProProdClass> _ProdClass;

        [XmlElement(ElementName = "ProdClass1")]
        public List<ProProdClass> ProdClass
        {
            set
            { _ProdClass = value; }
            get {
                return _ProdClass;
            }
        }

這樣的語句,會輸出一個一個 ProdClass1實體。

 

 

你可能感兴趣的:(webserver)