获取xml单个值的方法 。XML asp.net
<subject><subject_ID>1</subject_ID><subject_Name>1</subject_Name></subject>怎么获取当subje...
<subject>
<subject_ID>1</subject_ID>
<subject_Name>1</subject_Name>
</subject>
怎么获取当subject_ID 为1 时 的subject_Name
请给代码谢谢。 展开
<subject_ID>1</subject_ID>
<subject_Name>1</subject_Name>
</subject>
怎么获取当subject_ID 为1 时 的subject_Name
请给代码谢谢。 展开
1个回答
展开全部
XDocument doc = XDocument.Load(“test.xml");
var test = doc.Descendants("subject").Where(x => x.Elements("subject_ID").Any(f => (string)f == "2")).FirstOrDefault().Elements("subject_Name").FirstOrDefault().Value;
var test = doc.Descendants("subject").Where(x => x.Elements("subject_ID").Any(f => (string)f == "2")).FirstOrDefault().Elements("subject_Name").FirstOrDefault().Value;
更多追问追答
追问
你好,你能给个注释吗 谢谢。
追答
恩,当然可以
XDocument doc = XDocument.Load(“test.xml"); //读取xml文件
var test = doc.Descendants("subject") //选取节点subject
.Where(x => x.Elements("subject_ID").Any(f => (string)f == "2")) //查找subject_Id = 2的subject节点,你的情况改成1
.FirstOrDefault() //选取符合集合第一个
.Elements("subject_Name") //取的subject节点下的subject_name节点
.FirstOrDefault().Value; //取值
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询