1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<? xml version = "1.0" encoding = "utf-8" ?> < config > < rules > < rule name = "namea" > < params > < item name = "p" type = "get" pattern = "(ios|android|)" /> < item name = "cid" type = "get" pattern = "(ios|android|)" /> < item name = "type" type = "get" pattern = "(ios|android|)" /> < item name = "sort" type = "get" pattern = "(ios|android|)" /> < item name = "action" type = "post" pattern = "(ios|android|)" /> < item name = "userAgent" type = "header" pattern = "(ios|android|)" /> </ params > </ rule > < rule name = "nameb" > < params > < item name = "p" type = "get" pattern = "(ios|android|)" /> < item name = "cid" type = "get" pattern = "(ios|android|)" /> < item name = "type" type = "get" pattern = "(ios|android|)" /> < item name = "sort" type = "get" pattern = "(ios|android|)" /> < item name = "action" type = "post" pattern = "(ios|android|)" /> < item name = "userAgent" type = "header" pattern = "(ios|android|)" /> </ params > </ rule > < rule name = "namec" > < params > < item name = "p" type = "get" pattern = "(ios|android|)" /> < item name = "cid" type = "get" pattern = "(ios|android|)" /> < item name = "type" type = "get" pattern = "(ios|android|)" /> < item name = "sort" type = "get" pattern = "(ios|android|)" /> < item name = "action" type = "post" pattern = "(ios|android|)" /> < item name = "userAgent" type = "header" pattern = "(ios|android|)" /> </ params > </ rule > < rule name = "named" > < params > < item name = "p" type = "get" pattern = "(ios|android|)" /> < item name = "cid" type = "get" pattern = "(ios|android|)" /> < item name = "type" type = "get" pattern = "(ios|android|)" /> < item name = "sort" type = "get" pattern = "(ios|android|)" /> < item name = "action" type = "post" pattern = "(ios|android|)" /> < item name = "userAgent" type = "header" pattern = "(ios|android|)" /> </ params > </ rule > </ rules > </ config > |
问下,如何反序列化 参数 /config/rules/rule/params/item ?
不明白你的这个序列化何来的复杂之说。能具体描述吗?至于 小尧弟 说的什么遵循什么最小单元到不必在意。
我写了几个类,对应xml 的每个节点 最终发现无法序列化出 class rule 的 public List<paramsItem> @params{get;set;} ,一直是null
public class config { [XmlArray("rules")] public List<rule> rules{get;set;} } [XmlType(TypeName="item",Namespace="/config/rules/rule")] public class rule { [XmlElement("name")] public string name{get;set;} [XmlArray("params")] public List<paramsItem> @params{get;set;} } [XmlType(TypeName="item",Namespace="/config/rules/rule/params/item")] public class paramsItem { public string name{get;set;} public string type{get;set;} public string pattern{get;set;} }
@foxidea:
代码:
public class config { [XmlArray("rules")] public List<rule> rules{get;set;} }
对应的xml是:
<config> <rules> </rules> <rules> </rules> </config>
按照你的完整代码,会生成xml:
<config> <rules> <name>...</name> <params> <name>...</name> <type>...</type> <pattern>...</pattern> </params> <params> <name>...</name> <type>...</type> <pattern>...</pattern> </params> <params> <name>...</name> <type>...</type> <pattern>...</pattern> </params> </rules> <rules> <name>...</name> <params> <name>...</name> <type>...</type> <pattern>...</pattern> </params> <params> <name>...</name> <type>...</type> <pattern>...</pattern> </params> <params> <name>...</name> <type>...</type> <pattern>...</pattern> </params> </rules> <rules> <name>...</name> <params> <name>...</name> <type>...</type> <pattern>...</pattern> </params> <params> <name>...</name> <type>...</type> <pattern>...</pattern> </params> <params> <name>...</name> <type>...</type> <pattern>...</pattern> </params> </rules> </config>
以上代码的生成可能跟实际会有所出入,你可以先生成config对象,然后序列化为xml,比较生成的xml与要读取的xml之间的差异,然后去调整类的结构。
xml的属性需要使用XmlAttribute特性定义。
@foxidea: 另外,namespace是用于定义名称空间的,不是用来定义xml路径的。
@foxidea: 抱歉,我前面的猜测有意外,下面是我处理后的代码:
[XmlType(TypeName = "config")] public class XmlConfig { [XmlArray("rules")] public List<rule> rules { get; set; } } [XmlType(TypeName = "rule")] public class rule { [XmlAttribute] public string name { get; set; } [XmlArray("params")] public List<paramsItem> @params { get; set; } } [XmlType(TypeName = "item")] public class paramsItem { [XmlAttribute] public string name { get; set; } [XmlAttribute] public string type { get; set; } [XmlAttribute] public string pattern { get; set; } }
@小尧弟: 呵呵,理解就好了。楼主给的xml是内容为空的xml,<a />等同于<a></a>,当属性很多又是简单结构的时候,使用属性的可读性比使用子节点的可读性要强,而且,从性能来说,也有小小的提高,还少占用空间(xml字符串的总字节数少)。