首页 新闻 会员 周边 捐助

XmlReader读取XML文件的问题

0
[已解决问题] 解决于 2008-08-06 13:58
<P>我创建了一个xml文件,然后用XmlReader进行读取。但出现了一些问题,调试不出来。大侠帮帮忙。具体情况如下</P> <P>xml格式:</P> <DIV class=e><SPAN class=b><STRONG><FONT color=#ff0000>&nbsp;</FONT></STRONG></SPAN> <FONT color=#0000ff><SPAN class=m>&lt;?</SPAN><SPAN class=pi>xml version="1.0" encoding="gb2312" </SPAN><SPAN class=m>?&gt;</SPAN></FONT> </DIV> <DIV class=e> <DIV class=c style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><A class=b onfocus=h() onclick="return false" href="file:///K:/Project/NET/SEC/SEC/bin/Debug/Data/Favorites.xml#"><STRONG><FONT color=#ff0000>-</FONT></STRONG></A> <SPAN class=m><FONT color=#0000ff>&lt;</FONT></SPAN><SPAN class=t><FONT color=#990000>Favorites</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN></DIV> <DIV> <DIV class=e> <DIV class=c style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><A class=b onfocus=h() onclick="return false" href="file:///K:/Project/NET/SEC/SEC/bin/Debug/Data/Favorites.xml#"><STRONG><FONT color=#ff0000>-</FONT></STRONG></A> <SPAN class=m><FONT color=#0000ff>&lt;</FONT></SPAN><FONT color=#990000><SPAN class=t>Folder</SPAN><SPAN class=t> Name</SPAN></FONT><SPAN class=m><FONT color=#0000ff>="</FONT></SPAN><B>搜索</B><FONT color=#0000ff><SPAN class=m>"</SPAN><SPAN class=m>&gt;</SPAN></FONT></DIV> <DIV> <DIV class=e> <DIV style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><SPAN class=b><STRONG><FONT color=#ff0000>&nbsp;</FONT></STRONG></SPAN> <SPAN class=m><FONT color=#0000ff>&lt;</FONT></SPAN><SPAN class=t><FONT color=#990000>Title</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN><SPAN class=tx><STRONG>百度</STRONG></SPAN><SPAN class=m><FONT color=#0000ff>&lt;/</FONT></SPAN><SPAN class=t><FONT color=#990000>Title</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN> </DIV></DIV> <DIV class=e> <DIV style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><SPAN class=b><STRONG><FONT color=#ff0000>&nbsp;</FONT></STRONG></SPAN> <SPAN class=m><FONT color=#0000ff>&lt;</FONT></SPAN><SPAN class=t><FONT color=#990000>Url</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN><SPAN class=tx><STRONG>http://www.baidu.com</STRONG></SPAN><SPAN class=m><FONT color=#0000ff>&lt;/</FONT></SPAN><SPAN class=t><FONT color=#990000>Url</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN> </DIV></DIV> <DIV><SPAN class=b><STRONG><FONT color=#ff0000>&nbsp;</FONT></STRONG></SPAN> <SPAN class=m><FONT color=#0000ff>&lt;/</FONT></SPAN><SPAN class=t><FONT color=#990000>Folder</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN></DIV></DIV></DIV> <DIV class=e> <DIV class=c style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><A class=b onfocus=h() onclick="return false" href="file:///K:/Project/NET/SEC/SEC/bin/Debug/Data/Favorites.xml#"><STRONG><FONT color=#ff0000>-</FONT></STRONG></A> <SPAN class=m><FONT color=#0000ff>&lt;</FONT></SPAN><FONT color=#990000><SPAN class=t>Folder</SPAN><SPAN class=t> Name</SPAN></FONT><SPAN class=m><FONT color=#0000ff>="</FONT></SPAN><B>娱乐</B><FONT color=#0000ff><SPAN class=m>"</SPAN><SPAN class=m>&gt;</SPAN></FONT></DIV> <DIV> <DIV class=e> <DIV style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><SPAN class=b><STRONG><FONT color=#ff0000>&nbsp;</FONT></STRONG></SPAN> <SPAN class=m><FONT color=#0000ff>&lt;</FONT></SPAN><SPAN class=t><FONT color=#990000>Title</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN><SPAN class=tx><STRONG>搜狐</STRONG></SPAN><SPAN class=m><FONT color=#0000ff>&lt;/</FONT></SPAN><SPAN class=t><FONT color=#990000>Title</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN> </DIV></DIV> <DIV class=e> <DIV style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><SPAN class=b><STRONG><FONT color=#ff0000>&nbsp;</FONT></STRONG></SPAN> <SPAN class=m><FONT color=#0000ff>&lt;</FONT></SPAN><SPAN class=t><FONT color=#990000>Url</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN><SPAN class=tx><STRONG>http://www.sohu.com</STRONG></SPAN><SPAN class=m><FONT color=#0000ff>&lt;/</FONT></SPAN><SPAN class=t><FONT color=#990000>Url</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN> </DIV></DIV> <DIV><SPAN class=b><STRONG><FONT color=#ff0000>&nbsp;</FONT></STRONG></SPAN> <SPAN class=m><FONT color=#0000ff>&lt;/</FONT></SPAN><SPAN class=t><FONT color=#990000>Folder</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN></DIV></DIV></DIV> <DIV><SPAN class=b><STRONG><FONT color=#ff0000>&nbsp;</FONT></STRONG></SPAN> <SPAN class=m><FONT color=#0000ff>&lt;/</FONT></SPAN><SPAN class=t><FONT color=#990000>Favorites</FONT></SPAN><SPAN class=m><FONT color=#0000ff>&gt;</FONT></SPAN></DIV> <DIV><SPAN class=m><FONT color=#0000ff>然后我读取所有Folder中的属性Name的值,将这些值保存至string[]当中。但我现在只能取到第一个Name和对应的值。</FONT></SPAN></DIV> <DIV><SPAN class=m><FONT color=#0000ff>我的代码是这样的:</FONT></SPAN></DIV><SPAN class=m> <DIV><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XmlReaderSettings settings = new XmlReaderSettings();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; settings.ConformanceLevel = ConformanceLevel.Fragment;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; settings.IgnoreWhitespace = true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; settings.IgnoreComments = true;</DIV> <DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.reader = XmlReader.Create(file, settings);</DIV> <DIV><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string folders = "";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.reader.ReadToFollowing("Folder");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (this.reader.HasAttributes)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (reader.MoveToNextAttribute())<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; folders += this.reader.Name + ":" + this.reader.Value + ";";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</DIV> <DIV>&nbsp;</DIV> <DIV>各位。告诉我怎么实现啊。谢谢了!</SPAN></DIV></DIV></DIV>
少之的主页 少之 | 菜鸟二级 | 园豆:205
提问于:2008-08-06 12:46
< >
分享
最佳答案
0
循环一下就可以了啊 :) XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; settings.IgnoreWhitespace = true; settings.IgnoreComments = true; this.reader = XmlReader.Create(file, settings); string folders = ""; while (this.reader.ReadToFollowing("Folder")) { if (this.reader.HasAttributes) { while (reader.MoveToNextAttribute()) { folders += this.reader.Name + ":" + this.reader.Value + ";"; } } }
Colin Han | 老鸟四级 |园豆:3041 | 2008-08-06 13:04
其他回答(1)
0
对呀!!支持!!!
伽马科技.攻城师 | 园豆:1303 (小虾三级) | 2008-08-06 13:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册