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. java-cmd-命令行编译和运行java文件

    一.使用的工具 1.javac 2.java 二.命令 项目目录只这样的 D:/project/src/com/example/Child.java D:/project/src/com/exampl ...

  2. Apache+Tomcat+mod_jk负载均衡

    一.需要的软件 1.jdk1.5以上 2.Tomcat6以上 3.Apache2.2以上 ,地址http://apache.dataguru.cn//httpd/binaries/win32/ ,名称 ...

  3. gd-jpeg: JPEG library reports unrecoverable error 解决办法

    Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecov ...

  4. 修复跨站攻击 php

    在公用文件中加入如下代码 <?php /*云体检通用漏洞防护补丁v1.1 更新时间:2013-05-25 功能说明:防护XSS,SQL,代码执行,文件包含等多种高危漏洞 */ $url_arr= ...

  5. 【leetcode❤python】191. Number of 1 Bits

    #-*- coding: UTF-8 -*- class Solution(object):    def hammingWeight(self, n):        if n<=0:retu ...

  6. Threads in Spring

    使用Spring时经常会问,我们定义的Bean应该是Singleton还是Prototype?多个客户端同时调用Dao层,需要考虑线程安全吗?通过阅读官方文档和Spring的源代码,这类问题的答案是: ...

  7. require或include相对路径多层嵌套引发的问题

    require或include相对路径多层嵌套引发的问题   php中require/include 包含相对路径的解决办法 在PHP中require,include一个文件时,大都是用相对路径,是个 ...

  8. Ubuntu 14.04中文输入法的安装

    Ubuntu默认自带的中文输入法是IBUS框架的ibus-pinyin,IBUS-Bopomofo等.对于习惯于搜狗,紫光华宇,谷歌拼音的我们可能有点使用不习惯.下面就是安装常用的IBUS中文输入法. ...

  9. 通知(NSNotificationCenter)

    // 监听加载更多的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadMoreDeals ...

  10. Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs

    B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...