xml操作SelectNodes报错:表达式的计算结果必须为节点集

hh0702 2011-04-27 04:48:58
在执行 ReadResource("ConfigItemEdit", "yy-en.xml") 报错:
表达式的计算结果必须为节点集。

请帮看看错在哪里,谢谢!!!




public static Hashtable ReadResource(string frmName, string lang)
{
Hashtable result = new Hashtable();
XmlReader reader = null;
reader = new XmlTextReader(lang );

XmlDocument doc = new XmlDocument();
doc.Load(reader);

XmlNode root = doc.DocumentElement;
XmlNodeList nodelist = root.SelectNodes( "[Name=" + frmName + "]/Controls/Control");
//最后一句报错:表达式的计算结果必须为节点集。


foreach (XmlNode node in nodelist)
{
try
{
XmlNode node1 = node.SelectSingleNode("@name");
XmlNode node2 = node.SelectSingleNode("@text");
if (node1 != null)
{
result.Add(node1.InnerText.ToLower(), node2.InnerText);
}
}
catch (FormatException fe)
{
Console.WriteLine(fe.ToString());
}
}
reader.Close();
return result;
}






文件:yy-en.xml



<?xml version="1.0" encoding="utf-8" ?>
<Resource>
<Form>
<Name>ConfigItemEdit</Name>
<Controls>
<Control name="txtTtitle" text="Ttitle~" />
<Control name="txtTips" text="tipsssssssssssss:" />
<Control name="chkEnabledStatet" text="kq" />
</Controls>
</Form>

<Form>
<Name>FrmPasswordChange</Name>
<Controls>
<Control name="txtTtitle" text="Ttiasdfasdfa234234234234sdfasdftle~" />
<Control name="txtTips" text="tipsssssssssasdfasd34234234ssss:" />
<Control name="chkEnabledStatet" text="k23423423423423q" />
</Controls>
</Form>
</Resource>
...全文
1680 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hh0702 2011-04-28
  • 打赏
  • 举报
回复
感谢 huangwenquan123 !

用你的方法搞定,用xml做多语言还是蛮方便~
huangwenquan123 2011-04-27
  • 打赏
  • 举报
回复
    public static Hashtable ReadResource(string fldname,string path)
{
Hashtable hs = new Hashtable();
XmlDocument xml = new XmlDocument();
xml.Load(path);
XmlNodeList node = xml.SelectSingleNode("//Name[text()='" + fldname + "']").NextSibling.SelectNodes("Control");
foreach (XmlNode n in node)
{
hs.Add(n.Attributes["name"].Value, n.Attributes["text"].Value);
}
return hs;
}
//调用
string path = Server.MapPath("XMLFile3.xml");
Hashtable hs = ReadResource("FrmPasswordChange", path);
foreach (DictionaryEntry d in hs)
{
Response.Write(d.Key + "===" + d.Value + "<br/>");
}
/*
txtTtitle===Ttiasdfasdfa234234234234sdfasdftle~
chkEnabledStatet===k23423423423423q
txtTips===tipsssssssssasdfasd34234234ssss:
*/
hh0702 2011-04-27
  • 打赏
  • 举报
回复
还是没解决

Icedmilk 能详细说说么
se7en 2011-04-27
  • 打赏
  • 举报
回复
肯定是xml文件对应的节点啊 。应该不能是整个xml文件吧
Icedmilk 2011-04-27
  • 打赏
  • 举报
回复
"/Resource/Form[Name = \"ConfigItemEdit\"]/Controls/Control"

XPath 这样写试试
hh0702 2011-04-27
  • 打赏
  • 举报
回复
自己顶一下,在线等待~

111,093

社区成员

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

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

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