C# 序列化中的 CDATA块
来源:互联网 发布:dbc数据库是什么 编辑:程序博客网 时间:2024/05/22 12:49
问题描述:
首先我有一个Xml文件,文件中的一些节点内容是包含在CDATA块中的,在某些情况下我需要从这个Xml文件中序列化出这个对象,有时我又需要通过反序列化修改这个Xml文件中CDATA块的内容。
Xml的部分内容如下:
1 <?xml version="1.0" encoding="utf-8"?>
2 <xml>
3 <configure>
4 <basenode>items</basenode>
5 <node>item</node>
6 <field>name,value</field>
7 </configure>
8 <items>
9 <item>
10 <name><![CDATA[admin-table]]></name>
11 <value><![CDATA[table_admin]]></value>
12 </item>
13 <item>
14 <name><![CDATA[admin-pre]]></name>
15 <value><![CDATA[a_]]></value>
16 </item>
17 </items>
18 </xml>
2 <xml>
3 <configure>
4 <basenode>items</basenode>
5 <node>item</node>
6 <field>name,value</field>
7 </configure>
8 <items>
9 <item>
10 <name><![CDATA[admin-table]]></name>
11 <value><![CDATA[table_admin]]></value>
12 </item>
13 <item>
14 <name><![CDATA[admin-pre]]></name>
15 <value><![CDATA[a_]]></value>
16 </item>
17 </items>
18 </xml>
1 <?xml version="1.0" encoding="utf-8"?>
2 <xml>
3 <configure>
4 <basenode>items</basenode>
5 <node>item</node>
6 <field>name,value</field>
7 </configure>
8 <items>
9 <item>
10 <name><![CDATA[admin-table]]></name>
11 <value><![CDATA[table_admin]]></value>
12 </item>
13 <item>
14 <name><![CDATA[admin-pre]]></name>
15 <value><![CDATA[a_]]></value>
16 </item>
17 </items>
18 </xml>
2 <xml>
3 <configure>
4 <basenode>items</basenode>
5 <node>item</node>
6 <field>name,value</field>
7 </configure>
8 <items>
9 <item>
10 <name><![CDATA[admin-table]]></name>
11 <value><![CDATA[table_admin]]></value>
12 </item>
13 <item>
14 <name><![CDATA[admin-pre]]></name>
15 <value><![CDATA[a_]]></value>
16 </item>
17 </items>
18 </xml>
这个配置文件的对象类:
1 namespace ScnCommon
2 {
3 [XmlRootAttribute("xml")]
4 public class ScnConfig
5 {
6 public ScnElement configure;
7 [XmlArrayAttribute("items")]
8 public item[] items;
9 [XmlIgnore]
10 public staticstring ScnLinkTag= "<!--scnlinktag-->";
11
12 private staticstring GetFilePath(string argFileName)
13 {
14 string path = HttpContext.Current.Request.PhysicalApplicationPath+ "common\\"+argFileName;
15 return path;
16 }
17
18 public static ScnConfig GetConfig(string argFileName)
19 {
20 string key ="scn-"+ argFileName;
21 ScnConfig config= (ScnConfig)HttpContext.Current.Cache[key];
22 if (config ==null)
23 {
24 XmlSerializer seria= new XmlSerializer(typeof(ScnConfig));
25 try
26 {
27 string file = GetFilePath(argFileName);
28 FileStream fs= new FileStream(file, FileMode.Open);
29 config= (ScnConfig)seria.Deserialize(fs);
30 fs.Close();
31 HttpContext.Current.Cache.Insert(key, config,new CacheDependency(file));
32 }
33 catch (System.IO.FileNotFoundException)
34 {
35 config= new ScnConfig();
36 }
37 }
38 return config;
39 }
40
41 public staticvoid SaveConfig(ScnConfig config,string argFileName)
42 {
43 string file = GetFilePath(argFileName);
44 XmlSerializer seria= new XmlSerializer(typeof(ScnConfig));
45 FileStream fs= new FileStream(file, FileMode.Create);
46 seria.Serialize(fs, config);
47 fs.Close();
48 }
49
50 public staticstring GetValue(string argName,string argFileName)
51 {
52 string value = "";
53 ScnConfig config= GetConfig(argFileName);
54 if (config.items != null)
55 {
56 for (int i= 0; i< config.items.Length; i++)
57 {
58 if (config.items[i].name.Value== argName)
59 {
60 value= config.items[i].value.Value;
61 }
62 }
63 }
64 return value;
65 }
66 }
67
68 public class ScnElement
69 {
70 public string basenode;
71 public string node;
72 public string field;
73 }
74
75 public class item
76 {
77 private XmlCDataSection _name;
78 public XmlCDataSection name
79 {
80 get {
81 return this._name;
82 }
83 set {
84 XmlDataDocument doc= new XmlDataDocument();
85 XmlCDataSection cd= doc.CreateCDataSection(value.Value);
86 this._name = cd;
87 }
88 }
89
90 private XmlCDataSection _value;
91 public XmlCDataSection value
92 {
93 get {
94 return this._value;
95 }
96 set {
97 XmlDataDocument doc= new XmlDataDocument();
98 XmlCDataSection cd= doc.CreateCDataSection(value.Value);
99 this._value = cd;
100 }
101 }
102 }
103 }
2 {
3 [XmlRootAttribute("xml")]
4 public class ScnConfig
5 {
6 public ScnElement configure;
7 [XmlArrayAttribute("items")]
8 public item[] items;
9 [XmlIgnore]
10 public staticstring ScnLinkTag= "<!--scnlinktag-->";
11
12 private staticstring GetFilePath(string argFileName)
13 {
14 string path = HttpContext.Current.Request.PhysicalApplicationPath+ "common\\"+argFileName;
15 return path;
16 }
17
18 public static ScnConfig GetConfig(string argFileName)
19 {
20 string key ="scn-"+ argFileName;
21 ScnConfig config= (ScnConfig)HttpContext.Current.Cache[key];
22 if (config ==null)
23 {
24 XmlSerializer seria= new XmlSerializer(typeof(ScnConfig));
25 try
26 {
27 string file = GetFilePath(argFileName);
28 FileStream fs= new FileStream(file, FileMode.Open);
29 config= (ScnConfig)seria.Deserialize(fs);
30 fs.Close();
31 HttpContext.Current.Cache.Insert(key, config,new CacheDependency(file));
32 }
33 catch (System.IO.FileNotFoundException)
34 {
35 config= new ScnConfig();
36 }
37 }
38 return config;
39 }
40
41 public staticvoid SaveConfig(ScnConfig config,string argFileName)
42 {
43 string file = GetFilePath(argFileName);
44 XmlSerializer seria= new XmlSerializer(typeof(ScnConfig));
45 FileStream fs= new FileStream(file, FileMode.Create);
46 seria.Serialize(fs, config);
47 fs.Close();
48 }
49
50 public staticstring GetValue(string argName,string argFileName)
51 {
52 string value = "";
53 ScnConfig config= GetConfig(argFileName);
54 if (config.items != null)
55 {
56 for (int i= 0; i< config.items.Length; i++)
57 {
58 if (config.items[i].name.Value== argName)
59 {
60 value= config.items[i].value.Value;
61 }
62 }
63 }
64 return value;
65 }
66 }
67
68 public class ScnElement
69 {
70 public string basenode;
71 public string node;
72 public string field;
73 }
74
75 public class item
76 {
77 private XmlCDataSection _name;
78 public XmlCDataSection name
79 {
80 get {
81 return this._name;
82 }
83 set {
84 XmlDataDocument doc= new XmlDataDocument();
85 XmlCDataSection cd= doc.CreateCDataSection(value.Value);
86 this._name = cd;
87 }
88 }
89
90 private XmlCDataSection _value;
91 public XmlCDataSection value
92 {
93 get {
94 return this._value;
95 }
96 set {
97 XmlDataDocument doc= new XmlDataDocument();
98 XmlCDataSection cd= doc.CreateCDataSection(value.Value);
99 this._value = cd;
100 }
101 }
102 }
103 }
相关说明:
1.定义Config类,一些节点。ScnElement、item 类节点的相关描述,这里需要注意的是item 类中所使用的 XmlDataDocument 、XmlCDataSection。这个就是保证在反序列化时相关内容能写入CDATA块中的关键。
2.GetFilePath 获取Xml文件的路径。GetConfig 通过Xml序列化出这个Config对象。SaveConfig 对Config对象的某些属性修改后反序列化成新的Xml文件。GetValue 通过名称获取相关值的方法。
- C# 序列化中的 CDATA块
- c# Serialization: 序列化内容至CData
- Xml CDATA 序列化
- xml 序列化时如何将某个字符串成员写成CDATA块
- c#中的序列化
- C#中的序列化
- C#中的序列化
- C#中的序列化
- XmlSerializer 序列化与反序列化CDATA 以及实现
- C#中的"序列化" Serializable 理解
- C#中的对象序列化研究
- [Serializable]C#中的对象序列化
- C#中的"序列化" Serializable 理解
- C#中的"序列化" Serializable 理解
- c#中的数据对象序列化
- C#中的序列化和反序列化小例
- c# 与 Unity3d 中的序列化
- C#中的"序列化" Serializable 理解
- Linux信号量机制及编程
- java与c/c++之间的数据交互-----jni点滴【转】
- mongoDB——前序(NoSQL)
- 如何合理布局网络广告位
- 复制原文件夹下的所有文件到指定文件夹
- C# 序列化中的 CDATA块
- Hadoop中HDFS目录字符长度和目录级数限制
- Antrus – 我搞的一个Android下的MVC开源框架
- SQL函数说明大全
- Mysql备份还原数据库之mysqldump实例及参数详细说明
- 数学之路(3)-信息隐藏(2)
- Linked 、 Google 、 Facebook oAuth登录
- 不得不引起注意的引用类型
- poj 2513解题报告