如何访问特定 XMLnode 的innerText
你好 我有以下 XML 片段:
<Customer AccountNumber="4">
<CustomerInfo>
<firstName>Andrew</firstName>
<lastName>Walker</lastName>
</CustomerInfo>
<Transactions>
<AccountBalance>595848584.4</AccountBalance>
</Transactions>
</Customer>
来自一个更大的 XML 文件。我想访问 C# 中的
。 目前我正在执行以下操作:
foreach (XmlNode customer in root)
{
i++;
if(_AccNumber== Convert.ToInt32(customer.Attributes[0].Value))
{
searchNode = CustNodes[(i - 1)];
string _firstName = searchNode.FirstChild.FirstChild.InnerText;
}
}
当我在调试器中查看 searchNode
时,我可以在结果视图元素 [0] 中看到它有一个 localName
CustomerInfo
如您所料。因此,在调试器中,名字具有以下路径:
searchNode -> [0].[0].[0].value
这是否有意义?
有更好的方法吗? (没有 LINQ)? 抱歉,如果有不清楚的地方……现在是凌晨 2:30!让我知道,我会更好地解释自己。 谢谢!!
Hi
I have the following XML fragment:
<Customer AccountNumber="4">
<CustomerInfo>
<firstName>Andrew</firstName>
<lastName>Walker</lastName>
</CustomerInfo>
<Transactions>
<AccountBalance>595848584.4</AccountBalance>
</Transactions>
</Customer>
from a bigger XML file. I want to access the <firsName>
in C#.
Currently I am doing the following:
foreach (XmlNode customer in root)
{
i++;
if(_AccNumber== Convert.ToInt32(customer.Attributes[0].Value))
{
searchNode = CustNodes[(i - 1)];
string _firstName = searchNode.FirstChild.FirstChild.InnerText;
}
}
When I view searchNode
in the debugger I can see in the results view element [0] which has a localName
CustomerInfo
as you expect. So in the debugger first name has the following path:
searchNode -> [0].[0].[0].value
if that makes sense?
Is there any better way of doing this? (without LINQ)?
Apologies, if something isn't clear....its 2:30AM! Let me know and I'll explain myself better.
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 xpath 很难理解,所以最好使用 GetElementsByTagName 函数
I think xpath is so much complex to understand , so better to use the GetElementsByTagName function
尝试 XPath.. 像这样的东西应该可以工作:
Try XPath.. something like this should work: