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 == )
{
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>

Xml反序列化的更多相关文章

  1. 让Visual Studio 2013为你自动生成XML反序列化的类

    Visual Sutdio 2013增加了许多新功能,其中很多都直接提高了对代码编辑的便利性.如: 1. 在代码编辑界面的右侧滚动条上显示不同颜色的标签,让开发人员可以对所编辑文档的修改.查找.定位情 ...

  2. 自动生成XML反序列化的类

    原文地址:http://www.cnblogs.com/jaxu/p/3632077.html   Visual Sutdio 2013增加了许多新功能,其中很多都直接提高了对代码编辑的便利性.如: ...

  3. .NET(C#):觉察XML反序列化中的未知节点

    原文 www.cnblogs.com/mgen/archive/2011/12/12/2284554.html 众所周知XML是可以扩展的,XML的元素可以靠名称识别而不是只按照未知识别.在 XML反 ...

  4. XML反序列化遇到数字型节点值为空导致反序列化异常

    实体类: [XmlRoot("stream")] public class _30320DuisiFukuanQueryResponseModel : ResponseModelB ...

  5. XML反序列化出错,XML 文档(2, 2)中有错误

    XML转换为实体类的错误处理方案 一.错误描述: XML反序列化出错,XML 文档(2, 2)中有错误 二.解决方案: 在实体类的字段要加上XmlElement属性 三.具体实现: 1.XML文档 & ...

  6. Xml反序列化记录

    1.概述 公司项目遇到一个需要对接webservice的,webservice大部分用的都是xml来传输的,这里记录一下xml反序列化遇到的问题 2.xml工具类 xml序列化: public sta ...

  7. 用XmlSerializer进行xml反序列化的时候,程序报错: 不应有 <xml xmlns=''>

    原因 一,类型错误: 比如xml本来是UserInfo类型 用XmlSerializer进行反序列化传入的类型是MemberInfo这就会报错 二,xml根节点和对象的类名不一致,而又没有对类加入[X ...

  8. ASP.NET下使用xml反序列化、缓存实现个性化配置文件的实时生效

    因为一些配置属性比较多,存在多组属性,因此结合xml解析.缓存技术,实现配置文化的自动解析.存入缓存.缓存依赖实时更新配置内容. 配置文件反序列化存入缓存的核心方法: public Class.Set ...

  9. C# XML反序列化与序列化举例:XmlSerializer(转)

    using System; using System.IO; using System.Xml.Serialization; namespace XStream { /// <summary&g ...

随机推荐

  1. fffffffffff

    create proc partPage114 @n int,--每页数量 @page int, --页码从0开始 @Mainkey int as declare @sql nvarchar(1000 ...

  2. CentOS 7 网络配置方法

    [root@zookeeper network-scripts]#  vim /etc/sysconfig/network-scripts/ifcfg-enp0s3 输入以下文本: TYPE=Ethe ...

  3. Outlook 无法更新全球通讯簿,错误 0×80190194

    当 Outlook 客户端尝试更新全球通讯簿,实际上是下载脱机通讯簿(Officeline Address Book,简称 OAB)时,可能会收到 0×80190194 的错误.错误代码 0×8019 ...

  4. oracle数据库导入导出dmp文件oracle命令

    在控制台下导入imp scott/密码@orcl file=文件路径 full=Y 导出 整个数据库TEST 用户名system  密码1234 exp system/1234@TEST file=文 ...

  5. 【leetcode❤python】 257. Binary Tree Paths

    深度优先搜索 # Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         se ...

  6. 如何优雅的处理Nodejs中的异步回调

    前言 Nodejs最大的亮点就在于事件驱动, 非阻塞I/O 模型,这使得Nodejs具有很强的并发处理能力,非常适合编写网络应用.在Nodejs中大部分的I/O操作几乎都是异步的,也就是我们处理I/O ...

  7. C#正则表达式获取组名,按照组名输出匹配内容

    最近写了个正则表达式匹配的工具,可以按照组名输出匹配内容,还是挺方便的,代码留存一下,以后用的话,直接copy了. Regex regex = new Regex(this.textBoxRegex. ...

  8. Java开发中经典的小实例-(while(参数){})

    import java.util.Scanner;public class Test_while {    public static void main(String[] args) {       ...

  9. java web开发必备知识

    从各种招聘网站的要求上筛选出了一些java开发的一些基本的要求,对照自身看看有哪些缺陷. java基础 既然是java web开发,java SE肯定要学好了. 多线程,IO,集合等,对队列,缓存,消 ...

  10. poj 2954 Triangle(Pick定理)

    链接:http://poj.org/problem?id=2954 Triangle Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...