XmlReader在序列化中的使用

和XmlDocument最大的不一樣——XmlReader逐行讀取。單獨不多使用,通常配合序列化(反序列化)使用,如下給出具體例子:ui

namespace ConsoleApplication1
{
    public class CData : IXmlSerializable
    {
        private string cDataValue = null;

        public string CDataValue
        {
            get { return cDataValue; }
            private set
            {
                cDataValue = value;
            }
        }
        public CData()
        {

        }
        public CData(string dataValue)
        {
            cDataValue = dataValue;
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            //當前讀到此處的時候,
            //就是CData整個字符串<Name><![CData....]></Name>一堆東西
            //所以Read先讀取Name,而後直接用Value獲取內部的XmlCData的真實數據
            if (reader.Read())
            {
                cDataValue = reader.Value;
            }
        }

        public void WriteXml(XmlWriter writer)
        {
            //把當前的數據轉換成CDataSection寫入
            writer.WriteCData(cDataValue);
        }

        #endregion
    }

    // 把A改爲Root
    [XmlRoot(ElementName = "Root")]
    public class A
    {
        public int Id { get; set; }
        //強制指定自定義的格式化方式
        [XmlElement("Name", typeof(CData))]
        public CData Name { get; set; }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sbu = new StringBuilder();
            
            //設置表頭(設置編碼格式和子Element的縮進符號)
            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Indent = true;
            setting.IndentChars = "\t";
            setting.NewLineChars = "\r\n";
            //必須用這種方式指定UTF-8輸出格式,不然前面會加BOM,
            //另外注意,其實如今編碼仍然是UTF-16,那是由於沒有用Stream類
            //而是用StringBuilder,StringBuilder默認覆蓋了Stream的UTF-8編碼
            //強制使用UTF-16致使(具體參考:http://blog.csdn.net/ding544935/article/details/7610958)
            setting.Encoding = new UTF8Encoding(false);

            using (var writer = XmlWriter.Create(sbu, setting))
            {
                XmlSerializer xz = new XmlSerializer(typeof(A));

                //去除命名空間
                XmlSerializerNamespaces emptyNameSpace = new XmlSerializerNamespaces();
                emptyNameSpace.Add(string.Empty, string.Empty);

                //序列化
                xz.Serialize(writer,
                    new A { Id = 1, Name = new CData("董瑋") }, emptyNameSpace);
            }
            Console.WriteLine(sbu.ToString());

            //如下是反序列化
            StringReader sr = new StringReader(sbu.ToString());
            XmlReaderSettings rset = new XmlReaderSettings();
            rset.IgnoreWhitespace = true;
            using (var reader = XmlReader.Create(sr, rset))
            {
                A a = null;
                XmlSerializer xz2 = new XmlSerializer(typeof(A));
                a = (A)xz2.Deserialize(reader);
                Console.WriteLine(a.Name.CDataValue);
            }
        }
    }
}
相關文章
相關標籤/搜索