[From Network]XmlSerializer,CollectionBase 序列化

 

CS code:

using System;

using System.Collections.Generic;

using System.IO;

using System.Xml;

using System.Xml.Serialization;

namespace RefecCTest

{

public class Program

    {

        static void Main(string[] args)

        {

            const string XML_URL = @"D:/tc/xmlserial.xml";

          

            TestDataSet tds = new TestDataSet();

            TestData td = new TestData();

            td.Title = "hello,one!";

            tds.Add(td);

            TestData td1 = new TestData();

            td1.Title = "hello,two!";

            tds.Add(td1);

            XmlSerializer xs = new XmlSerializer(typeof(TestDataSet));

            xs.Serialize(Console.Out, tds);

            XmlReader xtReader = new XmlTextReader(XML_URL);

            TestDataSet tds1 = (TestDataSet)xs.Deserialize(xtReader);

            foreach (TestData tdt in tds1)

            {

                Console.WriteLine(tdt.Title);

            }

        }

        public class TestData

        {

            private string _title = "";

            public String Title

            {

                get { return _title; }

                set { _title = value; }

            }

           // public string tm = null;

        }

        [XmlRootAttribute("yourRootName")]

        public class TestDataSet : System.Collections.CollectionBase

        {

            public void Add(TestData item)

            {

                List.Add(item);

            }

            public TestData this[int index]

            {

                get

                {

                    if (index >= Count || index < 0)

                    {

                        return null;

                    }

                    return (TestData)List[index];

                }

            }

            //doing

        }

    }

}

xmlserial.xml:

<?xml version="1.0" encoding="gb2312"?>
<yourRootName>
<TestData>
    <Title>hello,one!</Title>
</TestData>
<TestData>
    <Title>hello,two!</Title>
</TestData>
</yourRootName>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值