C# 加载 XML 文件并读取它以查找节点

发布于 2024-12-10 11:43:56 字数 877 浏览 0 评论 0原文

我正在尝试创建一个读取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独木成林 2024-12-17 11:43:56

尝试,

 string searchpath = "//CustomerInformation[name='JohnDoe']";

或者使用 Linq-XML。

var result = (from ele in XDocument.Load(@"c:\CustomerDatabase.xml").Descendants("CustomerInformation")
                    where ((string)ele.Element("name")) == "JohnDoe"
                    select ele).FirstOrDefault();
if(result!=null)
   {
    //
   }

Try,

 string searchpath = "//CustomerInformation[name='JohnDoe']";

Or use Linq-XML.

var result = (from ele in XDocument.Load(@"c:\CustomerDatabase.xml").Descendants("CustomerInformation")
                    where ((string)ele.Element("name")) == "JohnDoe"
                    select ele).FirstOrDefault();
if(result!=null)
   {
    //
   }
甜尕妞 2024-12-17 11:43:56

你的 XPath 对我来说看起来很奇怪。我建议看这里:http://www.w3schools.com/xpath/xpath_syntax.asp

也许这会更好:

string searchpath = "//CustomerInformation[@CustomerName='" + name + "']"; 

正如其他人所说,你能提供内部异常以及哪一行抛出错误吗?

Your XPath looks odd to me. I would suggest looking here: http://www.w3schools.com/xpath/xpath_syntax.asp

Perhaps this would work better:

string searchpath = "//CustomerInformation[@CustomerName='" + name + "']"; 

As others have said, can you provide the inner exception and which line throws the error?

夜空下最亮的亮点 2024-12-17 11:43:56
string searchpath = "//CustomerInformation[./name='" + name + "']";
string searchpath = "//CustomerInformation[./name='" + name + "']";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文