C 實現複雜XML的序列化與反序列化

2022-07-16 07:06:11 字數 4850 閱讀 2125

default.xml檔案如下:

<?

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

>

c#示例**如下:

using

system;

using

system.collections.generic;

using

system.linq;

using

system.text;

using

system.xml.serialization;

namespace

多層級反序列

}[xmltype(typename = "

rule")]

public

class

rule

[xmlarray(

"params")]

public list@params

}[xmltype(typename = "

item")]

public

class

paramsitem

[xmlattribute]

public

string type

[xmlattribute]

public

string pattern

}class

program

//////

xml序列化某一型別到指定的檔案

//////

//////

//////

//////

public

static

void serializetoxml(string

filepath, t obj)

}catch

(exception ex)

}//////

從某一xml檔案反序列化到某一型別

///

///待反序列化的xml檔名稱

///反序列化出的

///public

static t deserializefromxml(string

filepath)

}catch

(exception ex)}}

}

xml序列化與反序列化

序列化是將物件狀態轉換為可保持或傳輸的格式的過程。與序列化相對的是反序列化,它將流轉換為物件。這兩個過程結合起來,可以輕鬆地儲存和傳輸資料。序列化的目的 1 以某種儲存形式使自定義物件持久化 2 將物件從乙個地方傳遞到另乙個地方。net framework 提供兩種序列化技術 二進位制序列化保持型別...

php json與xml序列化 反序列化

php提供了json encode和json decode函式對物件進行json格式序列化 反序列化操作 data array name byron age 24,male friends array casper frank vincent json json encode data 將陣列序列化...

XML序列化與反序列化(續)

前段時間寫了乙個關於xml序列化和反序列化的 最近突然發現乙個問題,就是當xml節點值為空時,序列化後的xml節點會是這種形式的。但是我們如果需要這種形式的該如何寫呢。先看看 吧 工具類和測試物件 public class xmltextwritertest xmltextwriter public...