的SelectSingleNode返回null - 即使有空间 [英] SelectSingleNode return null - even with namespace

查看:132
本文介绍了的SelectSingleNode返回null - 即使有空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问以类似的方式过,但我似乎无法得到这个工作。

I know this question has been asked in a similar fashion before, but I can't seem to get this working.

我有一些XML:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
    <Research xmlns="http://www.rixml.org/2005/3/RIXML" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" createDateTime="2011-03-29T15:41:48Z" language="eng" researchID="MusiJvs3008">
    <Product productID="MusiJvs3008">
    <StatusInfo currentStatusIndicator="Yes" statusDateTime="2011-03-29T15:41:48Z" statusType="Published" />
    <Source>
    <Organization type="SellSideFirm" primaryIndicator="Yes">
    <OrganizationID idType="Reuters">9999</OrganizationID> 

和我试图读取使用XPath值:

And I'm trying to read values using xpath:

XPathDocument xmldoc = new XPathDocument(xmlFile); 
XPathNavigator nav = xmldoc.CreateNavigator(); 
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
nsMgr.AddNamespace(string.Empty, "http://www.rixml.org/2005/3/RIXML"); 
XPathNavigator result = nav.SelectSingleNode("/Research", nsMgr); // <-- Returns null!

不过,即使是简单的选择根节点的返回null!我相信,我有什么错我的名字空间。是否有人可以帮忙吗?

But even a simple select of the root node returns null! I am sure I have something wrong with my namespace. Can someone please help?

理想我想简单的线条,让我选择从XML文件中的值,即

Ideally I want simple lines that will let me select values from the xml file, i.e.

String a = xmlDoc.SelectSingleNode(@"/Research/Product/Content/Title").Value;

顺便说一句,我没有(直接)的控制XML文件的内容。

BTW, I have no (direct) control over the XML file content.

推荐答案

我不相信你可以使用一个空的空间别名,并让它自动使用XPath的前pression。只要你使用真正的别名,它应该工作,虽然。这个测试是好的,例如:

I don't believe you can use an empty namespace alias and have it used automatically by the XPath expression. As soon as you use an actual alias, it should work though. This test is fine, for example:

using System;
using System.Xml;
using System.Xml.XPath;

class Test
{
    static void Main() 
    {
        string xmlFile = "test.xml";
        XPathDocument xmldoc = new XPathDocument(xmlFile); 
        XPathNavigator nav = xmldoc.CreateNavigator(); 
        XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
        nsMgr.AddNamespace("x", "http://www.rixml.org/2005/3/RIXML"); 
        XPathNavigator result = nav.SelectSingleNode("/x:Research", nsMgr);
        Console.WriteLine(result);
    }
}

你的有无的使用XPath和的XPathDocument ,顺便说一下?我倾向于认为的LINQ to XML是一种的的更愉快的API,特别是当它涉及到的命名空间。如果您使用的是.NET 3.5,你有没有特别的规定使用XPath,我建议你检查一下。

Do you have to use XPath and XPathDocument, by the way? I tend to find that LINQ to XML is a much more pleasant API, particularly when it comes to namespaces. If you're using .NET 3.5 and you have no particular requirement to use XPath, I'd suggest you check it out.

这篇关于的SelectSingleNode返回null - 即使有空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆