C 中XML的讀取

2021-09-08 09:46:49 字數 3013 閱讀 7719

本文主要介紹在c#中有關xml的讀取,寫入操作。

1.xml的內容如下:  

<?

xml version="1.0" encoding="utf-8"

?>

<

root

>

<

title

>

<

settings

id = "0"

name

= "顯示文字"

>歡迎您!智慧型服務,互動體驗......

settings

>

<

settings

id = "1"

name

= "字型"

>微軟雅黑

settings

>

<

settings

id = "2"

name

= "顏色"

>yellow

settings

>

<

settings

id = "3"

name

= "字型尺寸"

>48

settings

>

title

>

<

menu

>

<

submu

id="0"

name

="部門分布"

/>

<

submu

id="1"

name

/>

<

submu

id="2"

name

="部門宣傳"

/>

<

submu

id="3"

name

="會議安排"

/>

menu

>

<

mu1>

<

submu

id = "0"

name

= "icentroview產品"

>

<

video

id = "0"

>videos/icv.mp4

video

>

submu

>

<

submu

id = "1"

name

= "員工社群"

>

<

video

id = "0"

>videos/ygsqxcp.mp4

video

>

submu

>

<

submu

id = "2"

name

= "三維展示"

>

<

video

id = "0"

>videos/ibaosight.mp4

video

>

submu

>

<

submu

id = "1"

name

= "好生活宣傳"

2.獲得xml文件

private

static

config\\config.xml";

private

xmldocument xmldoc;

private

xmlnode root;

public

static

string

title;

public

xmlhandler()

private

void

loadconfig()

catch

(exception e)

}

3.通過屬性名稱讀取xml節點中的內容

public

titlemodel gettitle()

else

if (element.getattribute("

name

") == "

字型尺寸")

else

if (element.getattribute("

name

") == "顏色"

)

else

if (element.getattribute("

name

") == "字型"

) }}

return

title;

}catch

(exception e)

}

4.通過屬性讀取xml中節點的屬性值

public listgetmenustring()

}return

list;

}catch

(exception e)

}

5.通過節點獲取xml節點中的內容

public

string

getmainbackground()

return

url;

}catch

(exception e)

}

以上就完成,通過節點屬性、屬性值對xml的讀取。

c 中對xml的讀取

節點型別 xmldeclaration 屬性 version 1.0屬性 encoding utf 8 節點型別 whitespace 標記空白內容 節點型別 whitespace 標記空白內容 不是想要的結果啊!是為何?源 如下 讀取 如下 private void button1 click o...

C 讀取 讀取XML

讀取xml到listbox combobox 1,知識需求 1 訪問xml檔案的兩個基本模型 一,dom模型 使用dom的好處在於它允許編輯和更新xml文件,可以隨機訪問文件中的資料,可以使用xpath查詢,但是,dom的缺點在於它需要一次性的載入整個文件到記憶體中,對於大型的文件,這會造成資源問題...

C 中關於XML的讀取和生成

1.簡單的根據key的值來獲取xml的value 根據key值獲取value值 key值 value值 public static string readconfig string configkey return configvalue 2.生成xml檔案 private void btnclos...