zoukankan      html  css  js  c++  java
  • Xml反序列化

    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>
  • 相关阅读:
    js 数据结构-栈与队列
    mysql (已解决)Access denied for user 'root'@'localhost' (using password: NO)
    mysql (已解决p)MYSQL5.7启动不了,本地计算机上的 MySQL57 服务启动后停止。
    mysql 慢查询日志,灾难日志恢复,错误日志
    php json的相关操作
    (转)LitJson 遍历key
    (转)用Eclipse进行C++开发时Bianry not found的问题解决
    (转) 在Eclipse中进行C/C++开发的配置方法(20140721最新版)
    (转)如何在eclipse的配置文件里指定jdk路径
    (转)mongodb分片
  • 原文地址:https://www.cnblogs.com/xingbinggong/p/4663486.html
Copyright © 2011-2022 走看看