不谋万世者,不足谋一时;不谋全局者,不足谋一域。君子敏于事而慎于言 新浪微薄:土司阿哈

用C#读xml文件

上一篇 / 下一篇  2008-12-01 17:46:02 / 个人分类:软件开发相关

1208328177
  • 自动化测试:
  • 测试文档:

:]4a }Q;V0//读一个Xml文件,用XmlTextReader来读
H Rh:p$E(`U0public ArrayList readXml(string fileName) 51Testing软件测试网O3w Vg H4MN*`c
{ 51Testing软件测试网Eu%`W&Qqk0d
ArrayList readResult=new ArrayList();
9G`XG r?0readResult.Clear();

)BWT v/x)x/Jn0p6^0 51Testing软件测试网HK qQ#K I C*P

XmlTextReader tr=new XmlTextReader(fileName); 51Testing软件测试网)h:M&H]L
while(!tr.EOF) 51Testing软件测试网;H Q%M j~c
{
,@InO` F.y:rmC$h {0if(tr.MoveToContent()==XmlNodeType.Element) 51Testing软件测试网+`N-r;Y5t-i3e
{ 51Testing软件测试网/H4e{b/{`;_
try 51Testing软件测试网P8_,Z k0_s-u
{
8Z e"`LG7x~0readResult.Add(tr.ReadElementString());
@#x7l|M ^8R0}
3p\{4y]]D-FB0catch(XmlException ex){}
"^%j.VWOL`w0|0}
4Wr}3I P0t l0r0else
z+NA7{$s%Cw0{tr.Read();}
&e^r M,de_ v%{ \iC0} 51Testing软件测试网b(D0G(} N
return readResult;
0@vPP#S3_*S0}

^&]vd%z2a+\0

&IX+g*I nB051Testing软件测试网 VP?9M"C(h1D
/* 51Testing软件测试网k/JL$KQ`f'^
* 读一个Xml的XmlDocument,用递归调用方法来显示所读Xml的所有内容
Hv ] R7u'B0* 递归调用不适合文件很大的Xml文件读取,这里只是展示一下XmlDocument的内部结构
U%VxEW4?'}0* 正常情况可以用XmlDocument的GetElementsByTagName()方法来查询 51Testing软件测试网 n2\0lJ6H[
* */ 51Testing软件测试网"oJe4d0JCs
public ArrayList readXml(XmlDocument doc){
n/pa6fL.h }'w0ArrayList readResult=new ArrayList();
"Q"d a/OYz0readResult.Clear(); 51Testing软件测试网mXyL&B"PY*K Z

4d8pL:f.vF0XmlNodeList nodeLst=doc.ChildNodes;
$k+w`C+b'N0XmlNodeListRead(readResult,nodeLst); 51Testing软件测试网n&o5Z'K)~ lL`w*Q

@-OJ bcq1n v0return readResult; 51Testing软件测试网,l2n,wM-W
} 51Testing软件测试网@8eQ9kgt

51Testing软件测试网%N0C9a*F&~PEua

//递归调用 51Testing软件测试网9u!e+O5} Y"N
public void XmlNodeListRead(ArrayList readResult,XmlNodeList nodeLst){
B`A*|.M$G0|0for (int i=0;i <nodeLst.Count;i++)
e!A n/V_)I0{
+J^*o2m-v6r h0XmlNode node=nodeLst[i]; 51Testing软件测试网w[c9vi$LY9jGP
if(!node.HasChildNodes)
KT_Z7EO0{
q2iZ }-o4c u$M$qBR0if(node.NodeType!=XmlNodeType.Comment & node.NodeType!=XmlNodeType.XmlDeclaration)
"Ql"lP[0{ 51Testing软件测试网ck(nb`
try 51Testing软件测试网0E-NZE]w8S6q
{readResult.Add(node.InnerText);} 51Testing软件测试网MB;s@EVsW
catch(XmlException ex){}
|'RVQ'Pq;V0} 51Testing软件测试网4nRNS~#b3fI
} 51Testing软件测试网z*Y,KT gx
else
6iY%X oF6] I_{G0{ 51Testing软件测试网1rd9QF*o_
XmlNodeListRead(readResult,node.ChildNodes);
|*['n*R(NSn!E.G0} 51Testing软件测试网s$mw[`s$^?8J
}
+z }l~7@;vete0}

`RG'bO*[[1_j0 51Testing软件测试网`5]+`g.M-n

private void button1_Click(object sender, System.EventArgs e)
_%t~Q$Y,V2fC0{
m%{F5I0W(vq*{0string fileName="books.xml"; 51Testing软件测试网4q;ez&\\jDR Y
ArrayList myRead=readXml(fileName); 51Testing软件测试网)f-vV%o,F[Q
this.listBox1.Items.Clear(); 51Testing软件测试网GHLy[4p#`Q
for(int i=0;i <myRead.Count;i++){this.listBox1.Items.Add(myRead[i].ToString());} 51Testing软件测试网@{-f/]]9TC;?N
}

6~A{3M ~PAz0 51Testing软件测试网,@%dAv V!g@

private void button3_Click(object sender, System.EventArgs e) 51Testing软件测试网8k3w K&X vt'w
{
Yg ~0j)V7cdY0doc.Load("booksEdit.xml");

-pP V"u-M8Qo0 51Testing软件测试网pG.@#Ux,h n j&f~H)n

//显示
y]2Ic XX9^%e0ArrayList myRead=readXml(doc);
V2Fdl\:x;E O0this.listBox1.Items.Clear();
M&^zn/}L6PS0for(int i=0;i <myRead.Count;i++){this.listBox1.Items.Add(myRead[i].ToString());}
']:d+f?@!s0}

hl(C b3s(Jd*_0

TAG: 软件开发相关

 

评分:0

我来说两句

Open Toolbar