为什么XPath表达式不会在C#中返回任何结果,但它在C#之外?

时间:2022-03-15 21:00:46

I'm working on Open XML document format for excel sheets.

我正在为excel表格开发Open XML文档格式。

var nodeList = WorksheetXml.SelectNodes(@"//c[child::f]");

it should return all rows with formulas, but it doesn't return anything. The same xml on Oxygen Editor, when applied the same xpath expression, returns all of them.

它应该返回带有公式的所有行,但它不会返回任何内容。 Oxygen Editor上的相同xml,当应用相同的xpath表达式时,将返回所有这些xml。

I am copying WorksheetXml inner xml to assure the content is the same... do you know why C# is not working the expected way?

我正在复制WorksheetXml内部xml以确保内容是相同的...你知道为什么C#没有按预期的方式工作吗?


EDIT: Namespace issue

编辑:命名空间问题

I've put this:

我把这个:

var manager = new XmlNamespaceManager(WorksheetXml.NameTable);
manager.AddNamespace(string.Empty, @"http://schemas.openxmlformats.org/spreadsheetml/2006/main");
manager.AddNamespace("r", @"http://schemas.openxmlformats.org/officeDocument/2006/relationships");

var nodeList = WorksheetXml.SelectNodes(@"//c[child::f]", manager);

and it didn't work, I think it is not a namespace problem.

并且它不起作用,我认为它不是名称空间问题。

2 个解决方案

#1


Is it possible that Oxygen Editor is handling namespaces differently? My guess is that the c element is in a namespace, but you aren't specifying one.

Oxygen Editor是否可能以不同方式处理命名空间?我的猜测是c元素在命名空间中,但你没有指定一个。

See this very similar question for sample code - and if that's not the problem, please post more details.

有关示例代码,请参阅此非常相似的问题 - 如果不是问题,请发布更多详细信息。

EDIT: The code you've posted still doesn't use a namespace in the XPath. Try this:

编辑:您发布的代码仍然没有在XPath中使用命名空间。试试这个:

var manager = new XmlNamespaceManager(WorksheetXml.NameTable);
manager.AddNamespace("n", 
    "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
manager.AddNamespace("r",  
    "http://schemas.openxmlformats.org/officeDocument/2006/relationships");

var nodeList = WorksheetXml.SelectNodes("//n:c[child::n:f]", manager);

(I don't know which namespace the c and f elements are meant to be in - adjust appropriately.)

(我不知道c和f元素应该在哪个命名空间中 - 适当调整。)

(Note that there's no need to use a verbatim string literal when you have no backslashes and the string is on one line.)

(注意,当没有反斜杠且字符串在一行上时,不需要使用逐字字符串文字。)

#2


You did not specify OpenXML namespace for the nodes in your XPath expression. All stock .NET XPath APIs that I know of do not inherit any namespaces from document context - so if you do not explicitly specify it, and don't use any prefixes in XPath expression, it is treated as empty namespace.

您没有为XPath表达式中的节点指定OpenXML名称空间。我所知道的所有库存.NET XPath API都没有从文档上下文继承任何名称空间 - 因此,如果您没有显式指定它,并且不在XPath表达式中使用任何前缀,则将其视为空名称空间。

Assuming that this is a call to XmlNode.SelectNode, you need the 2-argument overload where the second argument is XmlNamespaceManager.

假设这是对XmlNode.SelectNode的调用,则需要2参数重载,其中第二个参数是XmlNamespaceManager。

#1


Is it possible that Oxygen Editor is handling namespaces differently? My guess is that the c element is in a namespace, but you aren't specifying one.

Oxygen Editor是否可能以不同方式处理命名空间?我的猜测是c元素在命名空间中,但你没有指定一个。

See this very similar question for sample code - and if that's not the problem, please post more details.

有关示例代码,请参阅此非常相似的问题 - 如果不是问题,请发布更多详细信息。

EDIT: The code you've posted still doesn't use a namespace in the XPath. Try this:

编辑:您发布的代码仍然没有在XPath中使用命名空间。试试这个:

var manager = new XmlNamespaceManager(WorksheetXml.NameTable);
manager.AddNamespace("n", 
    "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
manager.AddNamespace("r",  
    "http://schemas.openxmlformats.org/officeDocument/2006/relationships");

var nodeList = WorksheetXml.SelectNodes("//n:c[child::n:f]", manager);

(I don't know which namespace the c and f elements are meant to be in - adjust appropriately.)

(我不知道c和f元素应该在哪个命名空间中 - 适当调整。)

(Note that there's no need to use a verbatim string literal when you have no backslashes and the string is on one line.)

(注意,当没有反斜杠且字符串在一行上时,不需要使用逐字字符串文字。)

#2


You did not specify OpenXML namespace for the nodes in your XPath expression. All stock .NET XPath APIs that I know of do not inherit any namespaces from document context - so if you do not explicitly specify it, and don't use any prefixes in XPath expression, it is treated as empty namespace.

您没有为XPath表达式中的节点指定OpenXML名称空间。我所知道的所有库存.NET XPath API都没有从文档上下文继承任何名称空间 - 因此,如果您没有显式指定它,并且不在XPath表达式中使用任何前缀,则将其视为空名称空间。

Assuming that this is a call to XmlNode.SelectNode, you need the 2-argument overload where the second argument is XmlNamespaceManager.

假设这是对XmlNode.SelectNode的调用,则需要2参数重载,其中第二个参数是XmlNamespaceManager。