电脑软硬件应用网
当前位置: 电脑软硬件应用网 > 设计学院 > 网络编程 > XML与XHTML教程 > 正文
[图文]根据XMl文件的ID读取对应的内容
根据XMl文件的ID读取对应的内容
2010-1-12 9:01:06  文/佚名   出处:电脑软硬件应用网   

项目中经常遇到一些读取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>


执行结果:

执行结果

  • 上一篇文章:

  • 下一篇文章:
  • 最新热点 最新推荐 相关文章
    XML的四种解析器原理及性能比较
    使用jquery的ajax解析xml的例子
    详解相互转换JSON-lib包的相关介绍
    教你使用quickwap的xml查询农历信息
    XML HTTP Request的属性和方法简介
    ASP生成XML数据文档的方法
    示范如何把XML读取数据放到内存
    使用DOM的技巧和诀窍总结
    兼容firefox与ie操作XML节点处理方法
    示例:asp读取xml文件的方法
    关于45IT | About 45IT | 联系方式 | 版权声明 | 网站导航 |

    Copyright © 2003-2011 45IT. All Rights Reserved 浙ICP备09049068号