如何使用C#从XML获取多个值 [英] How to get multiple Value from XML using C#

查看:81
本文介绍了如何使用C#从XML获取多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从XML文件中获取多个值。

我使用下面的代码。

I want to get multiple values from XML file.
I use below code.

var q = (from c in xDoc.Descendants("Login") where c.Element("UserName").Value == txtuname.Text.ToString() select (string)c.Element("UserName"));



但是使用上面的代码,我只得到一个节点值。


But using above code,i get only one node value.

推荐答案

如果您想要多个UserName,是否可以验证是否有多个分支包含您要搜索的用户名?它看起来似乎只匹配一个登录值...



如果要为每个结果选择多个值,则需要创建新的输出结果。 ..尝试类似:



var q =(
$ x $ b来自c的xDoc.Descendants(登录)

其中c.Element(UserName)。Value == txtuname.Text.ToString()

select new {(string)c.Element(UserName),(string)c .Element(OtherValue))};
If you want multiple "UserName"s, can you verify that there are more than one branch with the username you are searching for? It looks like it is looking to match exactly one login value...

If you want to select multiple values for each result, you need to create new output results... try something like:

var q = (
from c in xDoc.Descendants("Login")
where c.Element("UserName").Value == txtuname.Text.ToString()
select new { (string)c.Element("UserName"), (string)c.Element("OtherValue")) };


这篇关于如何使用C#从XML获取多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆