xml 充当简易数据库

后台:

写入节点

public static void Update(string path, string node, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
doc.Save(path);
}
catch { }
}

 

Update(patch, "/config/string[@id='id']", "value",TextID.Text.Trim());//更新节点(写入数据)

读取节点

public static string Read(string path, string node, string attribute,string htm)
{
string value = "";
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
value = (attribute.Equals("") ? (string.IsNullOrEmpty(htm) ? xn.InnerText : xn.InnerXml) : xn.Attributes[attribute].Value);
}
catch { }
return value;
}

 

CommonForNate.XmlHelper.Read(patch, "/config/string[@id='id']", "value");//取节点数据

 

 

 

posted @ 2014-06-10 19:03  暗影_猎手  阅读(179)  评论(0编辑  收藏  举报