WD1X.COM - 问答一下,轻松解决,电脑应用解决专家
主板显卡CPU内存显示器
硬盘维修显卡维修显示器维修
注册表系统命令DOS命令Win8
存储光存储鼠标键盘
内存维修打印机维修
WinXPWin7Win11Linux
硬件综合机箱电源散热器手机数码
主板维修CPU维修键盘鼠标维修
Word教程Excel教程PowerPointWPS
网络工具系统工具图像工具
数据库javascript服务器
PHP教程CSS教程XML教程

根据XMl文件的ID读取对应的内容

更新时间:2010-01-12 09:01 作者:佚名点击:

项目中经常遇到一些读取xml文件的方法,特别是在进行错误处理的情况下,要根据ErrId来获取Error内容,网上这一方面的东西比较零散,今天花了点时间整理了一下,写了一个小例子,拿来和大家分享一下。

首先建立一个共同的类,代码如下:

view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace XmlRead
{
public class Common
{
public static bool GetMessageByKey(string xmlPath, string AppKey, out string AppValue)
{
bool isSuccess = true;
AppValue = "";
if (!IsXmlFlieExist(xmlPath))
{
return false;
}
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlPath);
XmlNode xNode;
XmlElement xElem1;

xNode = xDoc.SelectSingleNode("//appSettings");

xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null)
{
AppValue = xElem1.GetAttribute("value");
}
else
{
}
}
catch (Exception ex)
{
ex.ToString();
isSuccess = false;
}
return isSuccess;
}


public static bool IsXmlFlieExist(string xmlPath)
{
try
{
if (File.Exists(xmlPath))
{
return true;
}
else
{
return false;
}

}
catch
{
return false;
}
}

}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace XmlRead
{
public class Common
{
public static bool GetMessageByKey(string xmlPath, string AppKey, out string AppValue)
{
bool isSuccess = true;
AppValue = "";
if (!IsXmlFlieExist(xmlPath))
{
return false;
}
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlPath);
XmlNode xNode;
XmlElement xElem1;

xNode = xDoc.SelectSingleNode("//appSettings");

xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null)
{
AppValue = xElem1.GetAttribute("value");
}
else
{
}
}
catch (Exception ex)
{
ex.ToString();
isSuccess = false;
}
return isSuccess;
}


public static bool IsXmlFlieExist(string xmlPath)
{
try
{
if (File.Exists(xmlPath))
{
return true;
}
else
{
return false;
}

}
catch
{
return false;
}
}

}
}


主程序是下面这样的,比较简单:

view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XmlRead
{
class Program
{
static void Main(string[] args)
{
string msg;
const string xmlPath = "D:\\VS-workspace\\XmlRead\\XmlRead\\MyConfig.xml";
Common .GetMessageByKey(xmlPath, "Errer001", out msg);
Console.WriteLine(msg);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XmlRead
{
class Program
{
static void Main(string[] args)
{
string msg;
const string xmlPath = "D:\\VS-workspace\\XmlRead\\XmlRead\\MyConfig.xml";
Common .GetMessageByKey(xmlPath, "Errer001", out msg);
Console.WriteLine(msg);
Console.ReadKey();
}
}
}



参考的xml文件如下:



view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<System.Config>
<appSettings>
<add key="ConnectString" value="D085D536F765EEB74123E527CEC0F564" />
<add key="Message2" value="D085D536F765EEB74123E527CEC0F564" />
<add key="Message3" value="D085D536F765EEB74123E527CEC0F564" />

<add key="Errer001" value="Host is already using!" />
<add key="Errer002" value="Please input halfsize number!" />
</appSettings>
</System.Config>
<?xml version="1.0" encoding="utf-8"?>
<System.Config>
<appSettings>
<add key="ConnectString" value="D085D536F765EEB74123E527CEC0F564" />
<add key="Message2" value="D085D536F765EEB74123E527CEC0F564" />

<add key="Message3" value="D085D536F765EEB74123E527CEC0F564" />

<add key="Errer001" value="Host is already using!" />
<add key="Errer002" value="Please input halfsize number!" />
</appSettings>
</System.Config>


执行结果:

执行结果

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
你可能感兴趣的内容