C# 序列化中的 CDATA块
时间:2010-09-16 来源:千叶红枫
问题描述:
首先我有一个Xml文件,文件中的一些节点内容是包含在CDATA块中的,在某些情况下我需要从这个Xml文件中序列化出这个对象,有时我又需要通过反序列化修改这个Xml文件中CDATA块的内容。
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 {
3 [XmlRootAttribute("xml")]
4 public class ScnConfig
5 {
6 public ScnElement configure;
7 [XmlArrayAttribute("items")]
8 public item[] items;
9 [XmlIgnore]
10 public static string ScnLinkTag = "<!--scnlinktag-->";
11
12 private static string 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 static void 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 static string 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 }
首先我有一个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> 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 {
3 [XmlRootAttribute("xml")]
4 public class ScnConfig
5 {
6 public ScnElement configure;
7 [XmlArrayAttribute("items")]
8 public item[] items;
9 [XmlIgnore]
10 public static string ScnLinkTag = "<!--scnlinktag-->";
11
12 private static string 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 static void 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 static string 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 通过名称获取相关值的方法。
相关阅读 更多 +