为什么需要把xml根节点的属性(名空间)去掉后,才能正常读取? 本帖最后由 nndtdx 于 2011-11-24 16:27:10 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ok,我有一点进展了。msdn上对SelectSigleNode有句话是:如果 XPath 表达式需要命名空间解析,必须使用接受 XmlNamespaceManager 作为参数的 SelectSingleNode 重载。 XmlNamespaceManager 用于解析命名空间。看来是方法有问题我把方法调整了一下 static void Main(string[] args) { XmlDocument xmlDocument = new XmlDocument(); XmlTextReader reader=new XmlTextReader("data.xml"); xmlDocument.Load(reader); XmlElement root = xmlDocument.DocumentElement; XmlNamespaceManager namespaceManager=new XmlNamespaceManager(reader.NameTable); namespaceManager.AddNamespace("media", "http://search.yahoo.com/mrss/"); namespaceManager.AddNamespace("gr", "http://www.google.com/schemas/reader/atom/"); namespaceManager.AddNamespace("idx", "urn:atom-extension:indexing"); namespaceManager.AddNamespace("a", "http://www.w3.org/2005/Atom");//这个地方该怎么写呢?? try { XmlNode node = root.SelectSingleNode("book",namespaceManager); Console.WriteLine("找到title节点---->"+node.Value); } catch (Exception exception) { Console.Write(exception.Message); } Console.Read(); }还遗留注释地方的一个问题,正在解决中。 xmlns="http://www.w3.org/2005/Atom"如何处理这个没有前缀的名空间啊 我还是读不出来求助啊,整了半天了,那个应该是默认的名空间吧,msdn上说 用 namespaceManager.AddNamespace(String.Empty, "http://www.w3.org/2005/Atom");但是,还是读不出来啊,求助。 终于搞定了。读的时候也需要加名空间,root.SelectSingleNode("atom:book",namespaceManager)还有atom 是这个时候指定的 namespaceManager.AddNamespace("atom", "http://www.w3.org/2005/Atom");我又调整了xml,并且成功读取,代码如下xml<?xml version="1.0" ?><feed xmlns:media="http://search.yahoo.com/mrss/" xmlns:gr="http://www.google.com/schemas/reader/atom/" xmlns:idx="urn:atom-extension:indexing" xmlns="http://www.w3.org/2005/Atom" idx:index="no" gr:dir="ltr"> <book> <id>1</id> </book> </feed>C# static void Main(string[] args) { XmlDocument xmlDocument = new XmlDocument(); XmlTextReader reader=new XmlTextReader("data.xml"); xmlDocument.Load(reader); XmlElement root = xmlDocument.DocumentElement; XmlNamespaceManager namespaceManager = new XmlNamespaceManager(reader.NameTable); namespaceManager.AddNamespace("atom", "http://www.w3.org/2005/Atom"); namespaceManager.AddNamespace("media", "http://search.yahoo.com/mrss/"); namespaceManager.AddNamespace("gr", "http://www.google.com/schemas/reader/atom/"); namespaceManager.AddNamespace("idx", "urn:atom-extension:indexing"); try { XmlNode node = root.SelectSingleNode("atom:book",namespaceManager).SelectSingleNode("id",namespaceManager); Console.WriteLine("找到id节点---->"+node.Value); } catch (Exception exception) { Console.Write(exception.Message); } Console.Read(); }o(︶︿︶)o 唉,多亏了这个页面http://msdn.microsoft.com/ZH-CN/library/4bektfx9(v=VS.80)以前把这个xml读取想的太简单了,看来XPath选择的背后还有很多东西啊。。 【XmlNode node = root.SelectSingleNode("atom:book",namespaceManager).SelectSingleNode("id",namespaceManager);】贴错了,应该是XmlNode node = root.SelectSingleNode("atom:book",namespaceManager).SelectSingleNode("atom:id",namespaceManager); sql数据库备份与还原问题 C#网络电视 sql数据库添加问题 DataSet 压缩后DateTime数据的问题 急~~ !!!如何用treeview显示数据表里的数据 C#如何获取C++动态库中的数组 CompileAssemblyFromFileBatch 和 CompileAssemblyFromFile 的区别 请问这两种调用非托管dll的方式的区别是什么 ping的问题 有无福建的c#高手? 请教SQLite数据库读写BLOB字段。 winfrom 程序 xp下和win7旗舰版下面怎么有了两种结果?
msdn上对SelectSigleNode有句话是:如果 XPath 表达式需要命名空间解析,必须使用接受 XmlNamespaceManager 作为参数的 SelectSingleNode 重载。 XmlNamespaceManager 用于解析命名空间。看来是方法有问题
我把方法调整了一下
static void Main(string[] args)
{
XmlDocument xmlDocument = new XmlDocument(); XmlTextReader reader=new XmlTextReader("data.xml");
xmlDocument.Load(reader);
XmlElement root = xmlDocument.DocumentElement;
XmlNamespaceManager namespaceManager=new XmlNamespaceManager(reader.NameTable);
namespaceManager.AddNamespace("media", "http://search.yahoo.com/mrss/");
namespaceManager.AddNamespace("gr", "http://www.google.com/schemas/reader/atom/");
namespaceManager.AddNamespace("idx", "urn:atom-extension:indexing");
namespaceManager.AddNamespace("a", "http://www.w3.org/2005/Atom");//这个地方该怎么写呢??
try
{
XmlNode node = root.SelectSingleNode("book",namespaceManager);
Console.WriteLine("找到title节点---->"+node.Value);
}
catch (Exception exception)
{
Console.Write(exception.Message);
}
Console.Read(); }
还遗留注释地方的一个问题,正在解决中。
但是,还是读不出来啊,
求助。
读的时候也需要加名空间,root.SelectSingleNode("atom:book",namespaceManager)
还有atom 是这个时候指定的 namespaceManager.AddNamespace("atom", "http://www.w3.org/2005/Atom");
我又调整了xml,并且成功读取,代码如下xml<?xml version="1.0" ?>
<feed xmlns:media="http://search.yahoo.com/mrss/" xmlns:gr="http://www.google.com/schemas/reader/atom/" xmlns:idx="urn:atom-extension:indexing" xmlns="http://www.w3.org/2005/Atom" idx:index="no" gr:dir="ltr">
<book>
<id>1</id>
</book>
</feed>
C#
static void Main(string[] args)
{
XmlDocument xmlDocument = new XmlDocument();
XmlTextReader reader=new XmlTextReader("data.xml");
xmlDocument.Load(reader);
XmlElement root = xmlDocument.DocumentElement;
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(reader.NameTable); namespaceManager.AddNamespace("atom", "http://www.w3.org/2005/Atom");
namespaceManager.AddNamespace("media", "http://search.yahoo.com/mrss/");
namespaceManager.AddNamespace("gr", "http://www.google.com/schemas/reader/atom/");
namespaceManager.AddNamespace("idx", "urn:atom-extension:indexing");
try
{
XmlNode node = root.SelectSingleNode("atom:book",namespaceManager).SelectSingleNode("id",namespaceManager);
Console.WriteLine("找到id节点---->"+node.Value);
}
catch (Exception exception)
{
Console.Write(exception.Message);
}
Console.Read(); }
o(︶︿︶)o 唉,多亏了这个页面http://msdn.microsoft.com/ZH-CN/library/4bektfx9(v=VS.80)
以前把这个xml读取想的太简单了,看来XPath选择的背后还有很多东西啊。。
贴错了,应该是XmlNode node = root.SelectSingleNode("atom:book",namespaceManager).SelectSingleNode("atom:id",namespaceManager);