日期:2014-05-20  浏览次数:20391 次

如何读取XML 简单----急
我的XML如下
<?xml   version= "1.0 "   encoding= "UTF-8 "   ?>
<document>
<ChannelID> 3205 </ChannelID>
<CreateDate> 2007-1-31 </CreateDate>
<ChannelName> HOME </ChannelName>
<Channel> 3206 </Channel>
<Channel> 3207 </Channel>
<Channel> 3208 </Channel>

</document>

Channel有多少个不知道
我想把里面所有的Channel用,分开写入一个字符串中,如2306,3207,3208,
应该怎么写

------解决方案--------------------
string strXML = @ " <?xml version= " "1.0 " " encoding= " "UTF-8 " " ?> <document> <ChannelID> 3205 </ChannelID> <CreateDate> 2007-1-31 </CreateDate> <ChannelName> HOME </ChannelName> <Channel> 3206 </Channel> <Channel> 3207 </Channel> <Channel> 3208 </Channel> </document> "; XmlDocument dom = new XmlDocument(); dom.LoadXml(strXML); XmlNodeList nl = dom.SelectNodes( "//Channel "); string str = " "; for(int i=0;i <nl.Count;i++) { str += ", "+ nl[i].InnerText; } if(str != " ") { str = str.Substring(1); } Response.Write(str);
------解决方案--------------------
http://hi.baidu.com/wlx%5fsm/blog/item/8f308b0a68cfeb3eb1351dad.html
------解决方案--------------------
XmlDocument dom = new XmlDocument();
dom.Load(文件路径);
XmlNodeList nodel = dom.SelectNodes( "//Channel ");
foreach (XmlNode node in nodel)
{
//node.InnerText是你要的.你自己组织一下吧
}
------解决方案--------------------
如果是单表形式的xml文档 可直接读到DataSet中
DataSet ds = new DataSet();
ds.ReadXml(@ "c:\1.xml ");
GridView1.DataSource = ds;
GridView1.DataBind();
-----------------
1.xml内容:
<Info>
<stu>
<id> 1 </id>
<name> feiyu </name>
</stu>
<stu>
<id> 2 </id>
<name> zhangsan </name>
</stu> <stu>
<id> 3 </id>
<name> lisi </name>
</stu>
</Info>