今天需要解析一个XML,这个XML和一般情况用.NET的序列化出来的格式不太一样。
我就又补习了一下。
分享一下学习成果吧。
示例代码下载:
http://download.csdn.net/detail/bdstjk/4028340
使用属性可以控制对象的 XML 序列化。
默认情况下,XML 元素名称由类或成员名称确定。在名为 Book
的简单类中,字段 ISBN
将生成 XML 元素标记 <ISBN>,如下面的示例所示。
5 |
// When an instance of the Book class is serialized, it might |
7 |
// <ISBN>1234567890</ISBN>. |
若要重新命名元素,可以更改这种默认行为。下面的代码演示属性 (Attribute) 如何通过设置 XmlElementAttribute 的 ElementName 属性 (Property) 实现此目的。
2 |
[XmlElement(ElementName = "TaxRate" )] |
3 |
public decimal ReturnTaxRate; |
XmlArrayAttribute 和 XmlArrayItemAttribute 属性旨在用于控制数组的序列化。使用这些属性可以控制元素名称、命名空间以及 XML 架构 (XSD) 数据类型(在万维网联合会 [www.w3.org] 文档“XML 架构第 2 部分:数据类型”中进行了定义)。此外,还可以指定数组所能包含的类型。
对于序列化数组时生成的封闭 XML 元素,其属性将由 XmlArrayAttribute 确定。例如,默认情况下,序列化下面的数组时,将会生成名为Employees
的 XML 元素。Employees
元素将包含在数组类型Employee
之后命名的一系列元素。
2 |
public Employee[] Employees; |
序列化实例可能如下所示。
通过应用 XmlArrayAttribute,可以按照以下方式更改 XML 元素的名称。
2 |
[XmlArray( "TeamMembers" )] |
3 |
public Employee[] Employees; |
生成的 XML 可能如下所示。
另一方面,XmlArrayItemAttribute 可以控制如何序列化数组中包含的项。请注意,该属性将应用于返回数组的字段。
2 |
[XmlArrayItem( "MemberName" )] |
3 |
public Employee[] Employees; |
生成的 XML 可能如下所示。
3 |
< MemberName >Haley</ MemberName > |
序列化派生类
XmlArrayItemAttribute 的另一种用法是,允许序列化派生类。例如,可将派生自 Employee
的另一个名为Manager
的类添加至上一示例中。如果没有应用XmlArrayItemAttribute,代码将在运行时失败,原因是无法识别派生类类型。若要解决这个问题,每次为每个可接受类型(基类和派生类)设置 Type 属性 (Property) 时,需要应用该属性 (Attribute) 两次。
02 |
[XmlArrayItem(Type = typeof (Employee)), |
03 |
XmlArrayItem(Type = typeof (Manager))] |
04 |
public Employee[] Employees; |
06 |
public class Employee{ |
09 |
public class Manager:Employee{ |
序列化实例可能如下所示。
06 |
< Employee xsi:type = "Manager" > |
将数组作为元素序列进行序列化
通过将 XmlElementAttribute 应用于返回数组的字段,还可以将该数组作为 XML 元素的平面序列进行序列化,如下所示。
3 |
public Employee[] Employees; |
序列化实例可能如下所示。
区别两种 XML 流的另一个方法是,使用 XML 架构定义工具,从编译好的代码生成 XML 架构 (XSD) 文档文件。没有将属性应用于字段时,架构会以下列方式描述元素。
|
|
<xs:element minOccurs="0" maxOccurs ="1" name="Employees" type="ArrayOfEmployee" />
|
将 XmlElementAttribute 应用于字段时,生成的架构会以下列方式描述元素。
|
|
<xs:element minOccurs="0" maxOccurs="unbounded" name="Employees" type="Employee" />
|
序列化 ArrayList
ArrayList 类可能包含各种不同对象的集合。因此,可以按照使用数组的类似方式使用 ArrayList。您可以创建返回单个ArrayList 的字段,而不用创建返回类型化对象的数组的字段。但是,与数组相同的是,必须将ArrayList 包含的对象的类型告知 XmlSerializer。为此,需要为该字段分配XmlElementAttribute 的多个实例,如下面的示例所示。
2 |
[XmlElement(Type = typeof (Employee)), |
3 |
XmlElement(Type = typeof (Manager))] |
使用 XmlRootAttribute 和 XmlTypeAttribute 控制类的序列化
能且只能应用于一个类的属性有下面两种:XmlRootAttribute 和 XmlTypeAttribute。这两种属性非常相似。XmlRootAttribute 只能应用于一个类:序列化时,该类表示 XML 文档的开始和结束元素,也就是根元素。另一方面,XmlTypeAttribute 可以应用于任何一个类,包括根类。
例如,在上面的示例中,Group
类就是根类,而其所有的公共字段和属性变成 XML 文档中的 XML 元素。因此,只能有一个根类。通过应用XmlRootAttribute,可以控制XmlSerializer 所生成的 XML 流。例如,可以更改元素名称和命名空间。
使用 XmlTypeAttribute 可以控制所生成 XML 的架构。需要通过 XML Web services 发布架构时,这项功能很有用。下面的示例将XmlTypeAttribute 和XmlRootAttribute 同时应用于同一个类。
1 |
[XmlRoot( "NewGroupName" )] |
2 |
[XmlType( "NewTypeName" )] |
4 |
public Employee[] Employees; |
如果对该类进行编译,并且使用 XML 架构定义工具生成其架构,可能会找到下面描述 Group
的 XML。
<xs:element name="NewGroupName" type="NewTypeName">
|
相比之下,如果是对该类的实例进行序列化,则只能在 XML 文档中找到 NewGroupName
。
<NewGroupName> . . .</NewGroupName>
|
最后来贴一个自己的XML解析实例
XML结构如下:
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
03 |
< Person IDCard = "610424199902230099" Name = "小田雨" MedicalID = "体检编号" Sex = "男" Age = "22" MedicalRecordDate = "2011-01-01" MedicalReportDate = "2011-01-01" |
04 |
MedicalCount = "体检次数" HospitalID = "001" HospitalName = "兴隆园医院" > |
11 |
< Conclusion ></ Conclusion > |
12 |
< Conclusion ></ Conclusion > |
13 |
< Conclusion ></ Conclusion > |
16 |
< Suggestion ></ Suggestion > |
17 |
< Suggestion ></ Suggestion > |
18 |
< Suggestion ></ Suggestion > |
20 |
< Health > 为空(预留)</ Health > |
24 |
< MedicalSub ID = "0001" Name = "化学检查" > |
25 |
< MedicalType ID = "0001001" Name = "血常规" MedicalDoc = "体检医师名字" MedicalDate = "2011-02-13" > |
27 |
< Item ID = "000100010001" Name = "白细胞" Unit = "G/L" Parameters = "3.7--10.0" > |
28 |
< Results >H==高,L=低,N=正常</ Results > |
31 |
< MedicalBodyPart > </ MedicalBodyPart > |
32 |
< MedicalImage > </ MedicalImage > |
33 |
< Conclusion ></ Conclusion > |
35 |
< Item ID = "000100010002" Name = "红细胞" Unit = "G/L" Parameters = "3.7--10.0" > |
36 |
< Results >H==高,L=低,N=正常</ Results > |
39 |
< MedicalBodyPart > </ MedicalBodyPart > |
40 |
< MedicalImage > </ MedicalImage > |
41 |
< Conclusion ></ Conclusion > |
45 |
< MedicalSub ID = "0002" Name = "物理检查" > |
46 |
< MedicalType ID = "0002001" Name = "B超" MedicalDoc = "体检医师名字" MedicalDate = "2011-02-13" > |
47 |
< Item ID = "000200010001" Name = "胸部B超" Unit = " " Parameters = "" > |
48 |
< Results >A=异常,N=正常</ Results > |
50 |
< Disease >病种,未见异常</ Disease > |
51 |
< MedicalBodyPart >检查部位:胸部</ MedicalBodyPart > |
52 |
< MedicalImage >影像所见</ MedicalImage > |
53 |
< Conclusion >检查结论</ Conclusion > |
55 |
< Item ID = "000200010002" Name = "腹部B超" Unit = " " Parameters = "" > |
56 |
< Results >A=异常,N=正常</ Results > |
58 |
< Disease >病种,未见异常</ Disease > |
59 |
< MedicalBodyPart >检查部位:腹部</ MedicalBodyPart > |
60 |
< MedicalImage >影像所见</ MedicalImage > |
61 |
< Conclusion >检查结论</ Conclusion > |
66 |
< MedicalSub ID = "0005" Name = "五官科" > |
67 |
< MedicalType ID = "0005001" Name = "眼科" MedicalDoc = "体检医师名字" MedicalDate = "2011-02-13" > |
68 |
< Item ID = "000500010001" Name = "视力/右" Unit = " " Parameters = "1.0-1.5" > |
69 |
< Results >A=异常,N=正常</ Results > |
71 |
< Disease >病种,未见异常</ Disease > |
72 |
< MedicalBodyPart >检查部位</ MedicalBodyPart > |
73 |
< MedicalImage >影像所见</ MedicalImage > |
74 |
< Conclusion >检查结论</ Conclusion > |
76 |
< Item ID = "000500010002" Name = "矫正视力/右" Unit = " " Parameters = "1.0-1.5" > |
77 |
< Results >A=异常,N=正常</ Results > |
79 |
< Disease >病种,未见异常</ Disease > |
80 |
< MedicalBodyPart >检查部位</ MedicalBodyPart > |
81 |
< MedicalImage >影像所见</ MedicalImage > |
82 |
< Conclusion >检查结论</ Conclusion > |
C#代码如下:
代码有点多
002 |
using System.Collections.Generic; |
006 |
using System.Xml.Serialization; |
014 |
static void Main( string [] args) |
018 |
r.Person = new Person(); |
019 |
r.Person.IDCard = "22" ; |
020 |
r.Person.Results = new List< string >(); |
021 |
r.Person.Results.Add( "1" ); |
022 |
r.Person.Results.Add( "1" ); |
023 |
r.Person.Results.Add( "1" ); |
024 |
r.Person.Suggestions = new List< string >(); |
025 |
r.Person.Suggestions.Add( "2" ); |
026 |
r.Person.Suggestions.Add( "2" ); |
027 |
r.Person.Suggestions.Add( "2" ); |
029 |
r.MedicalItems = new List<MedicalSub>(); |
030 |
MedicalSub ms = new MedicalSub(); |
033 |
ms.MedicalType = new MedicalType(); |
034 |
ms.MedicalType.ID = "wa" ; |
035 |
ms.MedicalType.Name = "s" ; |
036 |
ms.MedicalType.MedicalDoc= "qa" ; |
037 |
ms.MedicalType.MedicalDate = "2010-5-5" ; |
038 |
ms.MedicalType.Item = new List<Item>(); |
039 |
Item it = new Item(); |
043 |
ms.MedicalType.Item.Add(it); |
044 |
ms.MedicalType.Item.Add(it); |
045 |
r.MedicalItems.Add(ms); |
046 |
r.MedicalItems.Add(ms); |
048 |
Console.WriteLine( "序列化成功……" ); |
049 |
Console.WriteLine(XmlSerialize.SerializeXML<Root>(r)); |
060 |
public Person Person; |
062 |
public List<MedicalSub> MedicalItems; |
069 |
public string IDCard; |
075 |
public string MedicalID; |
084 |
public string MedicalRecordDate; |
087 |
public string MedicalReportDate; |
090 |
public string MedicalCount; |
093 |
public string HospitalID; |
096 |
public string HospitalName; |
098 |
[XmlArrayItem( "Result" )] |
099 |
public List< string > Results; |
101 |
[XmlArrayItem( "Conclusion" )] |
102 |
public List< string > Conclusions; |
104 |
[XmlArrayItem( "Suggestion" )] |
105 |
public List< string > Suggestions; |
107 |
public String Health; |
111 |
public class MedicalSub |
119 |
public MedicalType MedicalType; |
124 |
public class MedicalType |
133 |
public string MedicalDoc; |
136 |
public string MedicalDate; |
139 |
public List<Item> Item; |
154 |
public string Parameters; |
157 |
public string Results; |
161 |
public string Disease; |
163 |
public string MedicalBodyPart; |
165 |
public string MedicalImage; |
167 |
public string Conclusion; |
177 |
public class XmlSerialize |
182 |
/// <typeparam name="T"></typeparam> |
183 |
/// <param name="xmlObj"></param> |
184 |
/// <returns></returns> |
185 |
public static T DeserializeXML<T>( string xmlObj) |
187 |
XmlSerializer serializer = new XmlSerializer( typeof (T)); |
188 |
using (StringReader reader = new StringReader(xmlObj)) |
190 |
return (T)serializer.Deserialize(reader); |
197 |
/// <typeparam name="T"></typeparam> |
198 |
/// <param name="obj"></param> |
199 |
/// <returns></returns> |
200 |
public static string SerializeXML<T>(T obj) |
202 |
using (StringWriter writer = new StringWriter()) |
204 |
new XmlSerializer(obj.GetType()).Serialize((TextWriter)writer, obj); |
205 |
return writer.ToString(); |
来自:http://blog.csdn.net/bdstjk/article/details/7210742
- [.net 面向对象程序设计进阶] (11) 序列化(Serialization)(三) 通过接口 IXmlSerializable 实现XML序列化 及 通用XML类
[.net 面向对象程序设计进阶] (11) 序列化(Serialization)(三) 通过接口 IXmlSerializable 实现XML序列化 及 通用XML类 本节导读:本节主要介绍通过序列 ...
- 使用XML序列化器生成XML文件和利用pull解析XML文件
首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...
- 使用XMl序列化器生成xml文件
生成XML文件 创建几个虚拟的短信对象,存在list中 备份数据通常都是备份至sd卡 使用StringBuffer拼接字符串 把整个xml文件所有节点append到sb对象里 sb.append(&q ...
- Java中使用DOM4J来生成xml文件和解析xml文件
一.前言 现在有不少需求,是需要我们解析xml文件中的数据,然后导入到数据库中,当然解析xml文件也有好多种方法,小编觉得还是DOM4J用的最多最广泛也最好理解的吧.小编也是最近需求里遇到了,就来整理 ...
- xml语法、DTD约束xml、Schema约束xml、DOM解析xml
今日大纲 1.什么是xml.xml的作用 2.xml的语法 3.DTD约束xml 4.Schema约束xml 5.DOM解析xml 1.什么是xml.xml的作用 1.1.xml介绍 在前面学习的ht ...
- python 之模块之 xml.dom.minidom解析xml
# -*- coding: cp936 -*- #python 27 #xiaodeng #python 之模块之 xml.dom.minidom解析xml #http://www.cnblogs.c ...
- XML基础+Java解析XML +几种解析方式的性能比较
XML基础+Java解析XML 一:XML基础 XML是什么: 可扩展的标记语言 XML能干什么: 描述数据.存储数据.传输(交换)数据. XML与HTML区别: 目的不一样 XML 被设计用来描述数 ...
- Android 使用xml序列化器生成xml文件
在<Android 生成xml文件>一文中使用流的形式写入xml格式文件,但是存在一定的问题,那就是在短信内容中不能出现<>之类的括号,本文使用xml序列化器来解决 xml序列 ...
- Android之XML序列化和解析
XML文件是一种常用的文件格式,可以用来存储与传递数据 ,本文是XML文件序列化与解析的一个简单示例 写文件到本地,并用XML格式存储 /** * 写xml文件到本地 */ private void ...
随机推荐
- iOS 9应用开发教程之多行读写文本ios9文本视图
iOS 9应用开发教程之多行读写文本ios9文本视图 多行读写文本——ios9文本视图 文本视图也是输入控件,与文本框不同的是,文本视图可以让用户输入多行,如图2.23所示.在此图中字符串“说点什么吧 ...
- Kolla O版本部署
Kolla O版部署和之前的版本还是有些区别的,环境还是all-in-one 基本准备: 关闭Selina和firewalld [root@kolla ~]# cat /etc/redhat-rele ...
- BZOJ.3105.[CQOI2013]新Nim游戏(线性基 贪心 博弈论)
题目链接 如果后手想要胜利,那么在后手第一次取完石子后 可以使石子数异或和为0.那所有数异或和为0的线性基长啥样呢,不知道.. 往前想,后手可以取走某些石子使得剩下石子异或和为0,那不就是存在异或和为 ...
- 【HDU】1693:Eat the Trees【插头DP】
Eat the Trees Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths 构造
D. Bear and Two Paths 题目连接: http://www.codeforces.com/contest/673/problem/D Description Bearland has ...
- 51nod 1515 明辨是非 启发式合并
1515 明辨是非 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 Description 给n组操 ...
- poj 3630 Phone List 贪心
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23722 Accepted: 7289 Descr ...
- wikioi 1576 最长严格上升子序列
简单的最长严格上升子序列的题 dp[i]表示到a[i]这个数为最后的时候最大的长度是多少 然后就差不多了吧~ #include <cstdio> #include <cmath> ...
- [Visual Studio] 重置默认设置 还原默认设置
恢复默认设置的2种方法 如果VS出现问题或设置变乱,可以通过恢复默认设置使之回到安装成功时的状态,从而解决出现的问题.VS恢复默认设置的方法有2种,分别是:通过“导入和导出设置”实现和通过命令实现. ...
- MySQL数据库基准压力测试工具之MySQLSlap使用实例
一.Mysqlslap介绍 mysqlslap是MySQL5.1之后自带的benchmark基准测试工具,类似Apache Bench负载产生工具,生成schema,装载数据,执行benckmark和 ...