<?xml version="1.0" encoding="utf-8" ?><xml> <exception> <modeule id="test1"> <tipMessage id="m001">连接数据库失败</tipMessage> <tipMessage id="m002">删除失败</tipMessage> </modeule> <modeule id="test2"> <tipMessage id="m001">连接数据库失败</tipMessage> <tipMessage id="m002">删除失败</tipMessage> </modeule> </exception> <message> <module id="test3"> <tipMessage id="m001">连接数据库失败</tipMessage> <tipMessage id="m002">删除失败</tipMessage> </module> </message></xml>以上为一个XML文件,我想根据我传入的参数“test1”和“m001”读出节点 <tipMessage id="m001">连接数据库失败</tipMessage>的值,高手请指点
2 回答
叮当猫咪
TA贡献1776条经验 获得超12个赞
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace XmlTest { class Program { static void Main(string[] args) { var doc = XDocument.Load("data.xml"); var value = (from node in doc.Descendants("modeule") where node.Attribute("id").Value == "test1" from tip in node.Elements("tipMessage") where tip.Attribute("id").Value == "m001" select tip.Value).FirstOrDefault(); Console.WriteLine(value); Console.ReadLine(); } } }
- 2 回答
- 0 关注
- 963 浏览
添加回答
举报
0/150
提交
取消