public class WriteXML
{
public static void SetXMLValue(string AppKey, string AppValue)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
XmlNode xNode;
XmlElement xElem1;
xNode = xDoc.SelectSingleNode("//userSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
if (xElem1 != null)
xElem1.InnerXml = AppValue;
xDoc.Save(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
}
catch
{
}
}
public static void SetXMLValue(string AppKey, int AppValue)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
XmlNode xNode;
XmlElement xElem1;
xNode = xDoc.SelectSingleNode("//userSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
if (xElem1 != null)
xElem1.InnerXml = AppValue.ToString();
xDoc.Save(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
}
catch
{
}
}
public static void SetXMLValue(string AppKey, bool AppValue)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
XmlNode xNode;
XmlElement xElem1;
xNode = xDoc.SelectSingleNode("//userSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
if (xElem1 != null)
xElem1.InnerXml = AppValue.ToString();
xDoc.Save(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
}
catch
{
}
}
public static void SetXMLValue(string AppKey, DateTime AppValue)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
XmlNode xNode;
XmlElement xElem1;
xNode = xDoc.SelectSingleNode("//userSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
if (xElem1 != null)
xElem1.InnerXml = AppValue.ToString();
xDoc.Save(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
}
catch
{
}
}
public static void CreateXMLDocument(string MyLoveMisc, string miscName)
{
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;
XmlElement MiscList;
XmlText xmltext;
if (System.IO.File.Exists(Application.StartupPath + "//" + MyLoveMisc + ".xml"))
{
xmldoc = new XmlDocument();
xmldoc.Load(Application.StartupPath + "//" + MyLoveMisc + ".xml");
MiscList = xmldoc.CreateElement("MiscList");
MiscList = xmldoc.CreateElement("", "MiscList", "");
xmltext = xmldoc.CreateTextNode(miscName);
MiscList.AppendChild(xmltext);
xmldoc.ChildNodes.Item(1).AppendChild(MiscList);
}
else
{
xmldoc = new XmlDocument();
//加入XML的声明段落
xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmldoc.AppendChild(xmlnode);
//加入一个根元素
xmlelem = xmldoc.CreateElement("", "MyLoveMisc", "");
// xmltext = xmldoc.CreateTextNode("heyu5220");
// xmlelem.AppendChild(xmltext);
xmldoc.AppendChild(xmlelem);
//加入另外一个元素
MiscList = xmldoc.CreateElement("MiscList");
MiscList = xmldoc.CreateElement("", "MiscList", "");
xmltext = xmldoc.CreateTextNode(miscName);
MiscList.AppendChild(xmltext);
xmldoc.ChildNodes.Item(1).AppendChild(MiscList);
}
//保存创建好的XML文档
try
{
xmldoc.Save(Application.StartupPath + "//" + MyLoveMisc + ".xml");
}
catch (Exception e)
{
//显示错误信息
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
public static void DeleteXMLNode(string MyLoveMisc, string miscName)
{
if (System.IO.File.Exists(Application.StartupPath + "//" + MyLoveMisc + ".xml"))
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + "//" + MyLoveMisc + ".xml");
XmlNodeReader reader = new XmlNodeReader(doc["MyLoveMisc"]);
XmlNodeList xnl = doc["MyLoveMisc"].ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.InnerText == miscName)
{
doc["MyLoveMisc"].RemoveChild(xn);
doc.Save(Application.StartupPath + "//" + MyLoveMisc + ".xml");
break;
}
}
}
else
{
}
}
}