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 == )
{
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反序列化的更多相关文章
- 让Visual Studio 2013为你自动生成XML反序列化的类
Visual Sutdio 2013增加了许多新功能,其中很多都直接提高了对代码编辑的便利性.如: 1. 在代码编辑界面的右侧滚动条上显示不同颜色的标签,让开发人员可以对所编辑文档的修改.查找.定位情 ...
- 自动生成XML反序列化的类
原文地址:http://www.cnblogs.com/jaxu/p/3632077.html Visual Sutdio 2013增加了许多新功能,其中很多都直接提高了对代码编辑的便利性.如: ...
- .NET(C#):觉察XML反序列化中的未知节点
原文 www.cnblogs.com/mgen/archive/2011/12/12/2284554.html 众所周知XML是可以扩展的,XML的元素可以靠名称识别而不是只按照未知识别.在 XML反 ...
- XML反序列化遇到数字型节点值为空导致反序列化异常
实体类: [XmlRoot("stream")] public class _30320DuisiFukuanQueryResponseModel : ResponseModelB ...
- XML反序列化出错,XML 文档(2, 2)中有错误
XML转换为实体类的错误处理方案 一.错误描述: XML反序列化出错,XML 文档(2, 2)中有错误 二.解决方案: 在实体类的字段要加上XmlElement属性 三.具体实现: 1.XML文档 & ...
- Xml反序列化记录
1.概述 公司项目遇到一个需要对接webservice的,webservice大部分用的都是xml来传输的,这里记录一下xml反序列化遇到的问题 2.xml工具类 xml序列化: public sta ...
- 用XmlSerializer进行xml反序列化的时候,程序报错: 不应有 <xml xmlns=''>
原因 一,类型错误: 比如xml本来是UserInfo类型 用XmlSerializer进行反序列化传入的类型是MemberInfo这就会报错 二,xml根节点和对象的类名不一致,而又没有对类加入[X ...
- ASP.NET下使用xml反序列化、缓存实现个性化配置文件的实时生效
因为一些配置属性比较多,存在多组属性,因此结合xml解析.缓存技术,实现配置文化的自动解析.存入缓存.缓存依赖实时更新配置内容. 配置文件反序列化存入缓存的核心方法: public Class.Set ...
- C# XML反序列化与序列化举例:XmlSerializer(转)
using System; using System.IO; using System.Xml.Serialization; namespace XStream { /// <summary&g ...
随机推荐
- datagrid后台分页js.js
$(function () { gridbind(); bindData(); }); //表格绑定function gridbind() { $('#dg').datagrid({ title: ' ...
- 为什么需要main函数,及其参数的用法
首先,需要明确main函数是什么? 答:main函数是C语言约定的入口函数 C99标准里面是这样描述的: Program startup The function called at program ...
- oracle一列中的数据有多个手机号码用逗号隔开,我如何分别取出来?
ID NUMBER1 137xxxx,138xxxx取出来成ID NUMBER1 137xxxx1 138xxxx create table test (id int, phone varchar2( ...
- 【leetcode❤python】21. Merge Two Sorted Lists
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- Nodejs之socket广播
nodejs发送udp广播还是蛮简单的,我们先写个服务器用于接收广播数据,代码如下: var dgram = require("dgram"); var server = dgra ...
- js 定位到某个锚点
js 定位到某个锚点 html页面内可以设置锚点,锚点定义 <a name="firstAnchor">&nsbp;</a> 锚点使用 <a ...
- Adding Value To Combo List at Runtime in Oracle Forms
You want to add a value in Combo List item in Oracle Forms, by typing it in combo list box text area ...
- FPM的远程利用
看了lijiejie的博客,和乌云的PHPFastCGI的这篇文章,感觉在实际的业务中经常能遇到,所以在此记录下来: 原文:http://www.lijiejie.com/fastcgi-read-f ...
- 08.安装Oracle 10g和SQLServer2008(仅作学习使用VirtualBox虚拟机来安装节省电脑资源)
1.虚拟机和宿主机共享文件夹. 2.右ctrl+F切换VirtualBox全屏 3.安装Oracle 10g 4.输入密码:root------------>下一步 5.勾选网络配置" ...
- 学习python得到方向与主体
Python的主体内容大致可以分为以下几个部分: 面向过程.包括基本的表达式,if语句,循环,函数等.如果你有任何一个语言的基础,特别是C语言的基础,这一部分就是分分钟了解下Python规定的事.如果 ...