C# 加载 XML 文件并读取它以查找节点
我正在尝试创建一个读取 xml 文件并搜索节点的 Web 服务。但是每次我收到一个错误,上面写着 return base.Channel.MymethodName(username);
XmlDocument doc = new XmlDocument();
doc.Load("C:\\CustomerDatabase.xml");
XmlNode root = doc.DocumentElement;
string searchpath = "//CustomerInformation[CustomerName'" + name + "']";
XmlNode userNode = root.SelectSingleNode(searchpath);
如果我屏蔽这部分代码,其他一切都会正常,所以我想我没有这样做正确。我已经阅读了这里的所有帖子,但在这些问题上仍然没有运气。有什么建议或帮助吗?
更新:
我的 xml 文件看起来像这样,
<CustomerInfo>
<CustomerInformation>
<name>JohnDoe</name>
</CustomerInformation>
</CustomerInfo>
我开始评论我的代码的每一行,我认为这是一个问题,
XmlNode userNode = root.SelectSingleNode(searchpath);
有什么想法可以修复
第二次编辑: 我正在尝试打开该文件,以便查看输入的名称是否在 XML 文件中。因此,几乎一个人输入 JohnDoe(如果存在),它会发送一个字符串,表示该人已经存在。
I am attempting to create a web service that reads an xml file and search for a node. But every time I get an error back which says return base.Channel.MymethodName(username);
XmlDocument doc = new XmlDocument();
doc.Load("C:\\CustomerDatabase.xml");
XmlNode root = doc.DocumentElement;
string searchpath = "//CustomerInformation[CustomerName'" + name + "']";
XmlNode userNode = root.SelectSingleNode(searchpath);
If I block out this part of the code everything else works, so I am thinking I am not doing this correct. I have read through all the posts I can on here, and still having no luck on the issues. Any suggestions or help?
Update:
My xml file looks like this
<CustomerInfo>
<CustomerInformation>
<name>JohnDoe</name>
</CustomerInformation>
</CustomerInfo>
I started commented each line of my code and i think this is one is problem
XmlNode userNode = root.SelectSingleNode(searchpath);
any ideas how I can fix that
2nd Edit:
I am attempting to open the file so that I can see if the name that is inputted is in the XML file. so pretty much a person types in JohnDoe if it exist it send a string saying person already exists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试,
或者使用 Linq-XML。
Try,
Or use Linq-XML.
你的 XPath 对我来说看起来很奇怪。我建议看这里:http://www.w3schools.com/xpath/xpath_syntax.asp
也许这会更好:
正如其他人所说,你能提供内部异常以及哪一行抛出错误吗?
Your XPath looks odd to me. I would suggest looking here: http://www.w3schools.com/xpath/xpath_syntax.asp
Perhaps this would work better:
As others have said, can you provide the inner exception and which line throws the error?