首页 技术 正文
技术 2022年11月17日
0 收藏 349 点赞 4,224 浏览 2979 个字

近段对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 Pengjuusing 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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898