xml 中的冒号 读取问题的解决
更新时间:2008年03月09日 20:10:59 作者:
c#读取xml时因为冒号问题的解决方法
参考文章:http://chs.gotdotnet.com/quickstart/howto/doc/Xml/XmlNameSpace.aspx
偶的源码:
public void bindXml(string xmlUrl)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load( System.AppDomain.CurrentDomain.BaseDirectory+"kkjj.xml");
//doc.Load(xmlUrl);
doc.SelectNodes("/rss/channel");
System.Xml.XmlNode root = doc.DocumentElement;
System.Xml.XmlNode xn = root.SelectSingleNode("/rss/channel");
outStr += "相册名称:" + xn.SelectSingleNode("title").InnerText;//相册名称
outStr += "<br>相册url:" + xn.SelectSingleNode("link").InnerText;//相册url
outStr += "<br>相册图片:<img src='" + xn.SelectSingleNode("image/url").InnerText + "' />";//相册首页图片
//outStr += "<br><img src='" + xn.SelectNodes("item")[0].SelectSingleNode("enclosure").Value + "'>";
System.Xml.XmlNodeList xnl = xn.SelectNodes("item");
outStr += "<br>相册图片数:" + xnl.Count.ToString() + "张";
System.Xml.XmlNamespaceManager nsmanager = new System.Xml.XmlNamespaceManager(doc.NameTable);
nsmanager.AddNamespace("media", "http://search.yahoo.com/mrss/");
// 创建包含 XPATH 表达式的字符串以进行计算。
string exprString = String.Format("// {0}:group", "media");
System.Xml.XmlNodeList nodelist = doc.SelectNodes(exprString, nsmanager);
outStr += "<br>media:title:"+nodelist[0].SelectSingleNode("media:title",nsmanager).Attributes["type"].InnerText;
// for(int i=0;i<xnl.Count;i++)
// {
// outStr += "<br> <img src='"+xnl[i].SelectSingleNode("enclosure").Attributes["url"].InnerText+"' />";
// }
}
当时看到了。。gg的xml里面有冒号,就卡卡解决了。。
付上xml片段
- <media:group>
<media:title type="plain">02.JPG</media:title>
<media:description type="plain" />
<media:keywords />
<media:thumbnail url="http://lh3.google.com/pengpengsay/R3C96dqJBUI/AAAAAAAAAEo/j3csPO0ig7o/s72/02.JPG" height="48" width="72" />
<media:thumbnail url="http://lh3.google.com/pengpengsay/R3C96dqJBUI/AAAAAAAAAEo/j3csPO0ig7o/s144/02.JPG" height="96" width="144" />
<media:thumbnail url="http://lh3.google.com/pengpengsay/R3C96dqJBUI/AAAAAAAAAEo/j3csPO0ig7o/s288/02.JPG" height="192" width="288" />
<media:content url="http://lh3.google.com/pengpengsay/R3C96dqJBUI/AAAAAAAAAEo/j3csPO0ig7o/02.JPG" height="400" width="600" type="image/jpeg" medium="image" />
<media:credit>成刚</media:credit>
</media:group>
xml地址:http://picasaweb.google.com/pengpengsay/lNFxFK
偶的源码:
public void bindXml(string xmlUrl)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load( System.AppDomain.CurrentDomain.BaseDirectory+"kkjj.xml");
//doc.Load(xmlUrl);
doc.SelectNodes("/rss/channel");
System.Xml.XmlNode root = doc.DocumentElement;
System.Xml.XmlNode xn = root.SelectSingleNode("/rss/channel");
outStr += "相册名称:" + xn.SelectSingleNode("title").InnerText;//相册名称
outStr += "<br>相册url:" + xn.SelectSingleNode("link").InnerText;//相册url
outStr += "<br>相册图片:<img src='" + xn.SelectSingleNode("image/url").InnerText + "' />";//相册首页图片
//outStr += "<br><img src='" + xn.SelectNodes("item")[0].SelectSingleNode("enclosure").Value + "'>";
System.Xml.XmlNodeList xnl = xn.SelectNodes("item");
outStr += "<br>相册图片数:" + xnl.Count.ToString() + "张";
System.Xml.XmlNamespaceManager nsmanager = new System.Xml.XmlNamespaceManager(doc.NameTable);
nsmanager.AddNamespace("media", "http://search.yahoo.com/mrss/");
// 创建包含 XPATH 表达式的字符串以进行计算。
string exprString = String.Format("// {0}:group", "media");
System.Xml.XmlNodeList nodelist = doc.SelectNodes(exprString, nsmanager);
outStr += "<br>media:title:"+nodelist[0].SelectSingleNode("media:title",nsmanager).Attributes["type"].InnerText;
// for(int i=0;i<xnl.Count;i++)
// {
// outStr += "<br> <img src='"+xnl[i].SelectSingleNode("enclosure").Attributes["url"].InnerText+"' />";
// }
}
当时看到了。。gg的xml里面有冒号,就卡卡解决了。。
付上xml片段
- <media:group>
<media:title type="plain">02.JPG</media:title>
<media:description type="plain" />
<media:keywords />
<media:thumbnail url="http://lh3.google.com/pengpengsay/R3C96dqJBUI/AAAAAAAAAEo/j3csPO0ig7o/s72/02.JPG" height="48" width="72" />
<media:thumbnail url="http://lh3.google.com/pengpengsay/R3C96dqJBUI/AAAAAAAAAEo/j3csPO0ig7o/s144/02.JPG" height="96" width="144" />
<media:thumbnail url="http://lh3.google.com/pengpengsay/R3C96dqJBUI/AAAAAAAAAEo/j3csPO0ig7o/s288/02.JPG" height="192" width="288" />
<media:content url="http://lh3.google.com/pengpengsay/R3C96dqJBUI/AAAAAAAAAEo/j3csPO0ig7o/02.JPG" height="400" width="600" type="image/jpeg" medium="image" />
<media:credit>成刚</media:credit>
</media:group>
xml地址:http://picasaweb.google.com/pengpengsay/lNFxFK
您可能感兴趣的文章:
- C# 对XML操作入门实例
- C#中XmlTextWriter读写xml文件详细介绍
- c#读取xml文件到datagridview实例
- c#使用linq技术创建xml文件的小例子
- 简介C#读取XML的两种方式
- C#的XML两种代码注释实例说明
- C#怎样才能将XML文件导入SQL Server
- C# 写入XML文档三种方法详细介绍
- C#序列化成XML注意细节
- C# 操作XML文档 使用XmlDocument类方法
- C# LINQ to XML应用介绍
- C# 对XML基本操作代码总结
- 操作XML文档遇到的XMLNS问题及解决方法 (C# 和 PHP)
- C# DataSet的内容写成XML时如何格式化字段数据
- 用C#把文件转换为XML的代码
- C#读取XML中元素和属性值的实现代码
最新评论