导航

C#操作XML

Posted on 2011-03-29 00:27  beeone  阅读(240)  评论(0编辑  收藏  举报

#region 操作XML
private static XmlDocument xmldoc = new XmlDocument();
private static JM_A ja = new JM_A();
/// <summary>
/// 查询XML
/// </summary>
/// <param name="key">查找标记</param>
/// <returns>XML值</returns>
public static String xml_select(String key)
{
try
{
xmldoc.Load(
"config.xml");
XmlNode root
= xmldoc.SelectSingleNode("info");
XmlNodeList Li
= root.ChildNodes;
String value
= "";
foreach (XmlNode item in Li)
{
XmlElement eL
= (XmlElement)item;
if (key == eL.Name)
{
value
= eL.InnerText;
break;
}
}
return ja.AESDecrypt(value);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return "";
throw new Exception("XML操作出错");
}
}
/// <summary>
/// 修改XML
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void xml_update(String key, String value)
{
try
{
xmldoc.Load(
"config.xml");
XmlNode root
= xmldoc.SelectSingleNode("info");
XmlNodeList Li
= root.ChildNodes;
foreach (XmlNode item in Li)
{
XmlElement eL
= (XmlElement)item;
if (key == eL.Name)
{
Console.WriteLine(
"111111");
eL.InnerText
= ja.AESEncrypt(value);
xmldoc.Save(
"config.xml");
break;
}
}
}
catch
{
throw new Exception("XML操作出错");
}
}
#endregion
}