記錄讀取xml檔案內容

2022-06-28 15:06:09 字數 3282 閱讀 7019

記錄下使用document類去讀取xml檔案內容

xml內容:

1

<?

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

?>

2<

schools

>

3<

school

id="1"

name

="school1"

>

4<

class

id="11"

name

="class11"

>

5<

student

id="111"

name

="student111"

/>

6<

student

id="112"

name

="student112"

/>

7<

student

id="113"

name

="student113"

/>

8class

>

9<

class

id="12"

name

="class12"

>

10<

student

id="121"

name

="student121"

/>

11<

student

id="122"

name

="student122"

/>

12class

>

13<

class

id="13"

name

="class13"

>

14<

student

id="131"

name

="student131"

/>

15<

student

id="132"

name

="student132"

/>

16class

>

17school

>

18<

school

id="2"

name

="school2"

>

19<

class

id="21"

name

="class21"

>

20<

student

id="211"

name

="student211"

/>

21class

>

22school

>

23schools

>

使用getelementsbytagname讀取檔案

定義學校類: 

1

public

class

school

1213

public

string getname()

1617

public

string getid()

2021

public listgetsclasslist()

24 }

定義班級類:

1

public

class

sclass

1516

public

string getid()

1920

21public

string getname()

2425

26public

school getschool()

2930

31public listgetstudents()

34 }

定義學生類:

1

public

class

student

1314

public

string getid()

1718

public

string getname()

2122

public

sclass getsclass()

25 }

定義讀取xml類:

1

public

class

schooldom catch

(exception e) 12}

1314

/**15

* parse school.xml,返回所有學校、班級、學生的資訊16*

17*

@return

18*/

19public listparseschoolxml() 49}

50}51}

52}53}

54}5556

return

schools;57}

58 }

定義tag常量

1

public

class

schooltag

測試方法

1

public

class

testdom 22}

23}2425 } catch

(exception e) 28}

2930

31public

static

void

main(string args)

34 }

輸出結果

school1---1

11--class11

111--student111

112--student112

113--student113

12--class12

121--student121

122--student122

13--class13

131--student131

132--student132

school2---2

21--class21

211--student211

如何讀取XML檔案內容

下面是通過讀取xml檔案中的內容並顯示在textbox的例子 以下是引用片段 using system using system.data using system.configuration using system.web using system.web.security using syst...

讀取XML 內容

file f new file system.getproperty user.dir file.separator src userfile.xml 定位檔案 reader new saxreader try catch documentexception e e.printstacktrace ...

記錄讀取XML檔案方式二

記錄 使用sax方式讀取,在速度上要優於dom 1 public class schoolsax extends defaulthandler 1617 18 19 文件接受文件開始的通知 此事件用於物件的初始化 20 21 override 22public void startdocument ...