首页 新闻 搜索 专区 学院

C#读取XML文件节点值

0
悬赏园豆:10 [已解决问题] 解决于 2009-09-26 01:42

<?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>的值,高手请指点

wbz009的主页 wbz009 | 初学一级 | 园豆:0
提问于:2009-09-24 15:07
< >
分享
最佳答案
0

引用XML,然后添加下面代码

    public partial class testxml : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string a = GetStatus("test1", "m001");
            Response.Write(a);
        }

        private string GetStatus(string modeule, string tipMessage)
        {
            string Result = string.Empty;
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(Server.MapPath("xml/test.xml"));
            XmlNode xmlNode = xmlDoc.SelectSingleNode("xml"); //取得xml下面的节点
            foreach (XmlNode exMe in xmlNode)
            {
                if (exMe.SelectSingleNode("modeule") != null) //exception
                {
                    XmlNode modeuleNode = exMe.SelectSingleNode("modeule");
                    if (modeuleNode.Attributes.GetNamedItem("id").InnerText.ToUpper() == modeule.ToUpper())
                    {
                        foreach (XmlNode tipMessageNode in modeuleNode)
                        {
                            if (tipMessageNode.Attributes.GetNamedItem("id").InnerText.ToUpper() == tipMessage.ToUpper())
                            {
                                Result = tipMessageNode.InnerText;
                                break;
                            }
                        }
                    }
                }
                else  //message
                {
                    XmlNode modeuleNode = exMe.SelectSingleNode("module");
                    if (modeuleNode.Attributes.GetNamedItem("id").InnerText.ToUpper() == modeule.ToUpper())
                    {
                        foreach (XmlNode tipMessageNode in modeuleNode)
                        {
                            if (tipMessageNode.Attributes.GetNamedItem("id").InnerText.ToUpper() == tipMessage.ToUpper())
                            {
                                Result = tipMessageNode.InnerText;
                                break;
                            }
                        }
                    }
                }
            }
            return Result;
        }
    }

收获园豆:8
风浪 | 老鸟四级 |园豆:2986 | 2009-09-24 15:38
其他回答(2)
0
Code
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
NeilChen | 园豆:244 (菜鸟二级) | 2009-09-24 15:22
Btw there is a spelling error in the word "modeule", should be "module".
支持(0) 反对(0) NeilChen | 园豆:244 (菜鸟二级) | 2009-09-24 15:23
0

用GetAttribute(“id”)的值来判断,如果其值是test1,不就得到你要的节点,在看子节点是否符合。

洋-洋 | 园豆:160 (初学一级) | 2009-09-24 15:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册