c# 中xml序列化时相同节点存入不同类型值
先上需要序列话的类定义:
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DescriptionType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2612.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.tempuri.org/OTA/2003/05")]
public partial class ParagraphType {
private object[] itemsField;
private ItemsChoiceType[] itemsElementNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Image", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("ListItem", typeof(ParagraphTypeListItem))]
[System.Xml.Serialization.XmlElementAttribute("Text", typeof(FormattedTextTextType))]
[System.Xml.Serialization.XmlElementAttribute("URL", typeof(string), DataType="anyURI")]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
} /// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemsChoiceType[] ItemsElementName {
get {
return this.itemsElementNameField;
}
set {
this.itemsElementNameField = value;
}
}
}
其中Items为object类型,可动态存放4种节点:Image ListItem Text URL,类型也是对应各自类型(如Image是string)。
需要注意的是XmlChoiceIdentifierAttribute方法用来通过枚举进一步检测成语,因此例子中的枚举类型是ItemsElementName,定义如下:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2612.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.tempuri.org/OTA/2003/05", IncludeInSchema=false)]
public enum ItemsChoiceType {
/// <remarks/>
Image,
/// <remarks/>
ListItem,
/// <remarks/>
Text,
/// <remarks/>
URL,
}
定义完毕,当序列化时,只需同时将Items和ItemsElementName 赋值如下:
Items = new List<object>()
{
new FormattedTextTextType() { Value = "备注一"},
"stringssssss",
}.ToArray(),
ItemsElementName = new ItemsChoiceType[]{ ItemsChoiceType.Text, ItemsChoiceType.Image}
此处同时使用了两个类型的数据,反序列化时只要认清顺序就能成功得到各自的结果。
最后需要注意的是,Items 与ItemsElementName 必须是一一对应关系,否则可能报序列化失败的错误。
c# 中xml序列化时相同节点存入不同类型值的更多相关文章
- .NET中XML序列化和反序列化常用类和用来控制XML序列化的属性总结(XmlSerializer,XmlTypeAttribute,XmlElementAttribute,XmlAttributeAttribute,XmlArrayAttribute...)
序列化和反序列化是指什么? 序列化(seriallization): 将对象转化为便于传输的数据格式, 常见的序列化格式:二进制格式,字节数组,json字符串,xml字符串.反序列化(deserial ...
- .NET中XML序列化的总结
[题外话] 以前虽然常用.NET中的序列化,但是常用的BinaryFormatter,也就是二进制文件的序列化,却鲜用XML的序列化.对于XML序列化,.NET中同样提供了一个非常方便的工具XmlSe ...
- .NET(C#):XML序列化时派生类的处理
原文 www.cnblogs.com/mgen/archive/2011/12/03/2275014.html 目录 1. 针对基类的XmlSerializer序列化派生类 2. 类内成员是派生类的序 ...
- 解决nodejs中json序列化时Date类型默认为UTC格式
在nodejs中,json序列化时Date类型时,默认转为UTC格式. 如下图 上面只是一个例子,下面我用一个更具体化的例子来展示一个这个情况,我们在开发WEB项目中,经常用到Express组件, 我 ...
- 解决nodejs中json序列化时Date类型为UTC格式
在nodejs中,json序列化时Date类型时,默认转为UTC格式. 如下图 zhupengfei@DESKTOP-HJASOE3 MINGW64 /d/MyProject/exp2 $ node ...
- .net的XML对象序列化VS WCF中xml序列化问题
整理一下 .net 对象序列化注意事项: 1. 字段:必须是 public类型 2.属性:只读或者只写的属性不被序列化,只有 可读可写并且赋值的才可以 序列化: Someclass obj = new ...
- 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
摘 自: http://blog.csdn.net/fxhflower/article/details/7276820 可使用以下代码: //Create our own namespaces for ...
- python中json序列化时汉字变成编码的解决方式
我们在使用json模块时,如果被序列化对象中不包含汉字,当然没有任何问题,但是有汉字会被编译成unicode码: import json dic = {","sex":& ...
- c#中的序列化
1.对象的序列化 NET支持对象序列化有以下几种方式:二进制序列化:对象序列化之后是二进制形式的,通过BinaryFormatter类来实现的,这个类位于System.Runtime.Serializ ...
随机推荐
- Mysql优化-分区
分区简介 分区是根据一定的规则,数据库把一个表分解成多个更小的.更容易管理的部分.就访问数据库应用而言,逻辑上就只有一个表或者一个索引,但实际上这个表可能有N个物理分区对象组成,每个分区都是一个独立的 ...
- js用正则判断身份证号码
在用户注册或修改信息时会用到正则表达式判断身份证号,直接调用该函数即可 //判断身份证号码 function idCardFn(idCard){ }(||)?\d{}([-]|[])([-]|[]\d ...
- Can't finish GitHub sharing process Successfully created project 'springcloud-parent' on GitHub,
解决Can't finish GitHub sharing process Successfully created project '' on GitHub, but initial push fa ...
- NX二次开发-创建圆弧(圆心-半径)UF_CURVE_create_arc
NX9+VS2012 #include <uf.h> #include <uf_curve.h> #include <uf_ui.h> #include <u ...
- NX二次开发-UFUN点收集器UF_UI_select_point_collection
#include <uf.h> #include <uf_ui.h> UF_initialize(); //点收集器 char sMessage[] = "点收集器& ...
- Python的变长参数
Python的变长参数 def foo1(*args): for arg in args: print arg def foo2(**kargs): for key in kargs: print k ...
- Greenplum(PostgreSql)使用 with recursive 实现树形结构递归查询并插入新表
本代码目的是替代Oracle的connect by语句,并实现后者的path和idleaf功能. 正文开始: 假设表org,字段有 id(编号),name(名称),pid(上级编号), 最上级的记录p ...
- DLL注入技术之劫持进程创建注入
劫持进程创建注入原理是利用Windows系统中CreateProcess()这个API创建一个进程,并将第6个参数设为CREATE_SUSPENDED,进而创建一个挂起状态的进程,利用这个进程状态进行 ...
- mysql中的Date日期格式的问题:只有日期没有时间及格式化时间
只有日期没有时间,把xml中的date改为timestamp 格式化最简单的方法:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", time ...
- HDU 3607 线段树+DP+离散化
题意:从左往右跳箱子,每个箱子有金币数量,只能从矮处向高处跳,求最大可获得金币数,数据规模1<=n<=1e5. 显然是一个dp的问题,不难得出dp[ i ] = max(dp[j] )+v ...