需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。

belinda_pjm 2010-10-15 01:12:40
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="BBS">
<Table ss:ExpandedColumnCount="11" ss:ExpandedRowCount="19" x:FullColumns="1"
x:FullRows="1" ss:StyleID="s113" ss:DefaultColumnWidth="93"
ss:DefaultRowHeight="14.25">
<Row ss:AutoFitHeight="0" ss:Height="22.5" ss:StyleID="s71">
<Cell ss:StyleID="s200">
<Data ss:Type="Number">1</Data>
</Cell>
<Cell ss:StyleID="s178">
<Data ss:Type="String">你好</Data>
</Cell>
<Cell ss:StyleID="s73">
<Data ss:Type="String">你好</Data>
</Cell>
<Cell ss:StyleID="s73">
<Data ss:Type="String">/</Data>
</Cell>
<Cell ss:MergeAcross="1" ss:StyleID="s239">
<Data ss:Type="String">永久</Data>
</Cell>
<Cell ss:StyleID="s199">
<Data ss:Type="String">/</Data>
</Cell>
<Cell ss:StyleID="s199">
<Data ss:Type="String">/</Data>
</Cell>
<Cell ss:StyleID="s136">
<Data ss:Type="DateTime">2009-09-18T00:00:00.000</Data>
</Cell>
<Cell ss:StyleID="s136">
<Data ss:Type="DateTime">2009-09-18T00:00:00.000</Data>
</Cell>
<Cell ss:StyleID="s136">
<Data ss:Type="DateTime">2009-11-05T00:00:00.000</Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0" ss:Height="22.5" ss:StyleID="s71">
<Cell ss:StyleID="s200">
<Data ss:Type="Number">2</Data>
</Cell>
<Cell ss:StyleID="s180">
<Data ss:Type="String">沙拉</Data>
</Cell>
<Cell ss:StyleID="s73">
<Data ss:Type="String">沙拉</Data>
</Cell>
<Cell ss:StyleID="s73">
<Data ss:Type="String">/</Data>
</Cell>
<Cell ss:StyleID="s181">
<Data ss:Type="DateTime">2009-09-02T00:00:00.000</Data>
</Cell>
<Cell ss:StyleID="s181">
<Data ss:Type="DateTime">2014-09-01T00:00:00.000</Data>
</Cell>
<Cell ss:StyleID="s199">
<Data ss:Type="String">/</Data>
</Cell>
<Cell ss:StyleID="s199">
<Data ss:Type="String">/</Data>
</Cell>
<Cell ss:StyleID="s199">
<Data ss:Type="String">/</Data>
</Cell>
<Cell ss:StyleID="s199">
<Data ss:Type="String">/</Data>
</Cell>
<Cell ss:StyleID="s199">
<Data ss:Type="String">/</Data>
</Cell>
</Row>
</Table>
</Worksheet>
<Worksheet ss:Name="1234">
<Table>
<Row>
<Cell>
<Data ss:Type="String">ssss</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
=========================================================================
以上是xml格式

我想取出 /Worksheet[@ss:Name="BBS"]/Table/Row/Cell/Data
节点的所有值

结果运行时报如下错误:
=========================================================================
需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Xml.XPath.XPathException: 需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。
=========================================================================

...全文
1187 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2010-10-15
  • 打赏
  • 举报
回复
应该是这样写

XmlDocument doc = new XmlDocument();
doc.Load("c:\\xx.xml");
XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(doc.NameTable);
xmlnsManager.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
xmlnsManager.AddNamespace("mxh", "urn:schemas-microsoft-com:office:spreadsheet");
XmlNodeList nodes = doc.SelectNodes("/mxh:Workbook/mxh:Worksheet[@ss:Name=\"BBS\"]/mxh:Table/mxh:Row/mxh:Cell/mxh:Data", xmlnsManager);
foreach (XmlNode n in nodes)
{
Console.WriteLine("<li>" + n.InnerText);
}
孟子E章 2010-10-15
  • 打赏
  • 举报
回复
因为你的数据本身带有名称空间的
就是这些
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40
belinda_pjm 2010-10-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 findcaiyzh 的回复:]

你应该没有使用XmlNamespaceManager

下面的代码在你的数据上已经测试过了

C# code

private void button2_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc……
[/Quote]
为什么前面节点名称也要带"ss:"这个命名控件呢?
宝_爸 2010-10-15
  • 打赏
  • 举报
回复
你应该没有使用XmlNamespaceManager

下面的代码在你的数据上已经测试过了


private void button2_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");

XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(doc.NameTable);
xmlnsManager.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");

XmlNodeList res = doc.SelectNodes("/ss:Workbook/ss:Worksheet[@ss:Name=\"BBS\"]/ss:Table/ss:Row/ss:Cell/ss:Data", xmlnsManager);
}

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧