代码改变世界

Xml反序列化

2015-07-21 09:47  Carl Xing  阅读(321)  评论(0编辑  收藏  举报

XML的反序列化可在类的属性上标记特性来隐射反序列化。例如这种形式

public class PaymentAccount
    {
        [XmlAttribute("name")]
        public string Name
        { get; set; }

        [XmlAttribute("environment")]
        public string Environment
        { get; set; }

        [XmlElement("webServiceUrl")]
        public string WebServiceUrl
        {
            get;
            set;
        }

        [XmlElement("websiteUrl")]
        public string WebUrl
        {
            get;
            set;
        }

        [XmlArray("paymentTypes")]
        [XmlArrayItem("paymentType", typeof(PaymentType))]
        public List<PaymentType> PaymentTypes { get; set; }
    }

也可以实现IXmlSerializable来实现自定义的序列化和反序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

namespace MvcTest
{
    [XmlRoot("siteMap", Namespace = nameSpace)]
    public class SiteMapConfig:IXmlSerializable
    {
        private const string nameSpace = "urn:schemas-test-com:sitemap";

        public static SiteMapConfig Instance
        {
            get 
            {
                SiteMapConfig cg = null;
                string path = HttpContext.Current.Server.MapPath("~/config/sitemap.config");
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    XmlSerializer xs = new XmlSerializer(typeof(SiteMapConfig));
                    object obj=xs.Deserialize(fs);
                    cg = (SiteMapConfig)obj;
                }
                return cg;
            }
        }

        public SiteMapNode ParentNode { get; set; }

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

        public void ReadXml(XmlReader reader)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            XmlNamespaceManager xn = new XmlNamespaceManager(doc.NameTable);
            xn.AddNamespace("sm", nameSpace);
            XmlNode pNode = doc.SelectSingleNode("/sm:siteMap/sm:siteMapNode",xn);
            ParentNode = new SiteMapNode() { 
                Children=new List<SiteMapNode>(),
                Description = pNode.Attributes["description"].Value,
                Title=pNode.Attributes["title"].Value,
                Url = pNode.Attributes["url"].Value
            };
            XmlNodeList list = pNode.ChildNodes;
            ReadNodes(ParentNode, list);
        }

        private void ReadNodes(SiteMapNode pNode, XmlNodeList nList)
        {
            if (nList==null || nList.Count == 0)
            {
                return;
            }
            pNode.Children = new List<SiteMapNode>();
            foreach (XmlNode node in nList)
            {
                SiteMapNode sNode=new SiteMapNode() {
                    Parent=pNode,
                    Description = node.Attributes["description"].Value,
                    Title = node.Attributes["title"].Value,
                    Url = node.Attributes["url"].Value
                };
                pNode.Children.Add(sNode);
                ReadNodes(sNode, node.ChildNodes);
            }
        }

        public void WriteXml(XmlWriter writer)
        {
            
        }
    }

    public class SiteMapNode
    {
        public SiteMapNode Parent { get; set; }

        public string Url { get; set; }

        public string Title { get; set; }

        public string Description { get; set; }

        public List<SiteMapNode> Children { get; set; }
    }
}

XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="urn:schemas-test-com:sitemap" >
  <siteMapNode url="" title="p1"  description="">
    <siteMapNode url="" title="c1"  description="" />
    <siteMapNode url="" title="c2"  description="" />
  </siteMapNode>
</siteMap>