近段对XML 序列化进行处理,用XmlSerializer这个挺好用的。

但是对于派生类对象的XML的生成总会报错。因为同一个节点名称,不能反射为不同的对象。这个在网上找了好久,都说要利用反射来处理。

现在用XML的类似C++ 函数前置声明。 XmlInclude 来实现。

声明:新手新学,难免纰漏!

直接代码。

环境: Vs2008 C#

1. 建了一个新C# 控制台工程。

主要要添加引用

using System.Xml;
using System.Xml.Serialization;

2. 声明和定义XML的生成对象格式。

自己写了个FormatDefine.cs类,用来实现定义对象的格式。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Zhang Pengju using System.Xml;
using System.Xml.Serialization;
//
namespace SerializeTest
{
public class StyleNote
{
[XmlText]
public string StyleNoteNode { get; set; }
} [XmlInclude(typeof(PointStyle))]
[XmlInclude(typeof(LineStyle))]

public class Style
{
[XmlAttribute("id")]
public int id { get; set; }
[XmlAttribute("type")]
public string TypeNode { get; set; }
//
public string StyleType { get; set; }
}
// 线的STYLE
public class LineStyle : Style
{
[XmlElement("Line")]
public string LineNode { get; set; }
[XmlElement("Count")]
public int CountNode { get; set; } }
// 点的STYLE
public class PointStyle : Style
{
[XmlElement("Point")]
public string PointNode { get; set; }
}
//
public class Styles
{
[XmlElement(ElementName = "Style")]
public List<Style> StyleListNode { get; set; }
public Styles()
{
StyleListNode = new List<Style>();
} } // 根节点
[XmlRoot("root")]
public class TestRoot
{
[XmlElement("Title")]
public StyleNote rStyleNote;
[XmlElement("Styles")]
public Styles StylesSNode { get; set; }
}
}

3. 调用和使用。

在Program.cs中测试。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Xml;
using System.Xml.Serialization;
using System.IO; namespace SerializeTest
{
class Program
{
static void Main(string[] args)
{
TestRoot oTestRoot = new TestRoot();
// StyleNote
StyleNote oStyleNote = new StyleNote();
oStyleNote.StyleNoteNode = "this is just a test";
// 线
LineStyle oLineStyle = new LineStyle();
oLineStyle.id = 1;
oLineStyle.StyleType = "StyleTypeLine";
oLineStyle.TypeNode = "TypeNodeLine";
oLineStyle.LineNode = "线节点";
oLineStyle.CountNode = 10;
// 点
PointStyle oPointStyle = new PointStyle();
oPointStyle.id = 2;
oPointStyle.StyleType = "StyleTypePoint";
oPointStyle.TypeNode = "TypeNodePoint";
oPointStyle.PointNode = "点节点"; //
Styles oStyles = new Styles();
oStyles.StyleListNode.Add(oPointStyle);
oStyles.StyleListNode.Add(oLineStyle); // 添加到root中
oTestRoot.rStyleNote = oStyleNote;
oTestRoot.StylesSNode = oStyles; // 保存文件名称
string oFileName = "C:\\my_Test.xml"; System.Xml.Serialization.XmlSerializer serializer = null;
System.IO.StreamWriter writer = null;
try
{
serializer = new System.Xml.Serialization.XmlSerializer(oTestRoot.GetType());
FileStream fs = new FileStream(oFileName, FileMode.Create);
serializer.Serialize(fs,oTestRoot);
}
catch (System.Exception ex)
{
}
finally
{
if (writer != null)
{
writer.Close();
}
}
}
}
}

这样运行,基本搞定。

生成XML如下:

<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title>this is just a test</Title>
<Styles>
<Style xsi:type="PointStyle" id="1" type="TypeNodePoint">
<StyleType>StyleTypePoint</StyleType>
<Point>点节点</Point>
</Style>
<Style xsi:type="LineStyle" id="1" type="TypeNodeLine">
<StyleType>StyleTypeLine</StyleType>
<Line>线节点</Line>
<Count>10</Count>
</Style>
</Styles>
</root>

至此,没有了。

欢迎留言,讨论!求进步!

留源码下载地址:http://download.csdn.net/detail/cartzhang/5591659

C# 派生类的XmlSerializer序列化XML的更多相关文章

  1. .NET(C#):XML序列化时派生类的处理

    原文 www.cnblogs.com/mgen/archive/2011/12/03/2275014.html 目录 1. 针对基类的XmlSerializer序列化派生类 2. 类内成员是派生类的序 ...

  2. c# XML和实体类之间相互转换(序列化和反序列化)[砖]

    link: http://blog.okbase.net/haobao/archive/62.html by: 好饱 我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlU ...

  3. C# XML和实体类之间相互转换(序列化和反序列化)

    我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. using System; using System.Collections.Ge ...

  4. XML和实体类之间相互转换(序列化和反序列化)

    我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  5. C#中将xml文件反序列化为实例时采用基类还是派生类的问题

    基类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...

  6. 六、用DataContractSerialize类序列化XML

    一.层次结构 基类:XmlObjectSerializer 派生类: DataContractSerializer NetDataContractSerializer DataContractJson ...

  7. .NET调用外部接口将得到的List数据,并使用XmlSerializer序列化List对象成XML格式

    BidOpeningData.BidSupervisionSoapClient client = new BidOpeningData.BidSupervisionSoapClient(); Dict ...

  8. 基类,派生类,内存分配情况 .xml

    pre{ line-height:1; color:#1e1e1e; background-color:#d2d2d2; font-size:16px;}.sysFunc{color:#627cf6; ...

  9. C#语言中的XmlSerializer类的XmlSerializer.Serialize(Stream,Object)方法举例详解

    在对象和 XML 文档之间进行序列化和反序列化操作. XmlSerializer 使您能够控制如何将对象编码为 XML. 命名空间:   System.Xml.Serialization程序集:  S ...

随机推荐

  1. Linux ALSA声卡驱动之八:ASoC架构中的Platform

    1.  Platform驱动在ASoC中的作用 前面几章内容已经说过,ASoC被分为Machine,Platform和Codec三大部件,Platform驱动的主要作用是完成音频数据的管理,最终通过C ...

  2. createrepo

    [root@iio enp]# createrepo -g /enp/comps.xml .Spawning worker 0 with 1362 pkgsWorkers FinishedSaving ...

  3. 特征变化--->特征向量中部分特征到类别索引的转换(VectorIndexer)

    VectorIndexer: 倘若所有特征都已经被组织在一个向量中,又想对其中某些单个分量进行处理时,Spark ML提供了VectorIndexer类来解决向量数据集中的类别性特征转换. 通过为其提 ...

  4. 一种高兼容性的JavaBean序列化方案

    在对JavaBean做序列化时,我们可能在某些场景希望前后兼容性好一些.比如所有的javaBean都序列化后保存在数据库,用的时候需要反序列化创建.随着业务的发展,数据模型可能会进行变更,那么原来的数 ...

  5. 【转】vue.js三种安装方式

    Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手 ...

  6. Linux 下 Solr的搭建与使用(建议jdk1.8以上)

    官方表示solr5之后的版本不再提供对第三方容器的支持(不提供war包了). “旧式”solr.xml格式不再支持,核心必须使用core.properties文件定义. 使用第三方容器的需要自己手动修 ...

  7. 【BZOJ3328】PYXFIB(数学)

    什么都不会的数学蒻菜瑟瑟发抖--Orz橙子(和兔子) 题目: BZOJ3328 分析: 橙子给我安利的数学题--(然后我就看着他因为矩阵乘法多模了一次卡了一天常数qwq表示同情) 先考虑一个子问题:求 ...

  8. 题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)

    Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...

  9. Android内存管理(13)常见产生内存泄漏的原因

    1.集合类泄漏 集合类如果仅仅有添加元素的方法,而没有相应的删除机制,导致内存被占用.如果这个集合类是全局性的变量 (比如类中的静态属性,全局性的 map 等即有静态引用或 final 一直指向它), ...

  10. Flume OG 与 Flume NG 的对比

    Flume OG 与 Flume NG 的对比 1.Flume OG Flume OG:Flume original generation 即Flume 0.9.x版本,它由agent.collect ...