XML讀取、XML修改、XML刪除
第一種:單獨寫一個XML類
XMLHelper類
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace DZ.DAL
{
public class XMLHeleper
{
/// <summary>
/// 獲取當XMl中的節點
/// </summary>
/// <returns></returns>
public string GetXMLNodeName(string path, string nodeName)
{
XmlDocument xml = new XmlDocument();
xml.Load(path);
XmlNode node = xml.SelectSingleNode(nodeName);
return node.InnerText;
}
/// <summary>
/// 修改
/// </summary>
/// <param name="path"></param>
/// <param name="node"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool Update(string path, string node, string value)
{
try
{
XmlDocument xl = new XmlDocument();
xl.Load(path);
XmlNode nodes = xl.SelectSingleNode(node);
nodes.InnerText = value;
xl.Save(path);
return true;
}
catch (Exception)
{
return false;
}
}
}
}
呼叫:
public string FilePath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "\\Base.XML"; //獲取檔案路徑
XMLHeleper XML = new XMLHeleper();
XML查詢:
/// <summary>
/// 查詢XML檔案
/// </summary>
public void SelectDBDataBase()
{
string AddressIP = XML.GetXMLNodeName(FilePath, "//DataConfig//BAddress//DataAdd");
...........
............
.........
......
}
修改檔案:
public void UpDateXMLFile()
{
string ipaddress = "123456789";
XML.Update(FilePath, "//DataConfig//BAddress//DataAdd", ipaddress);
.......
.......
.......
}
XML檔案:
檔名:Base.XML
<DataConfig>
<BAddress>
<DataAdd>123</DataAdd>
<DataDK>123</DataDK>
<DataLogin>123</DataLogin>
<DataName>123</DataName>
<DataPwd>123</DataPwd>
</BAddress>
<WAddress>
<DataAdd>123</DataAdd>
<DataDK>123</DataDK>
<DataLogin>123</DataLogin>
<DataName>123</DataName>
<DataPwd>123</DataPwd>
</WAddress>
</DataConfig>
第二種:直接呼叫VS開發工具中的XML類
載入XML檔案:
/// <summary>
/// 載入XML檔案
/// </summary>
public void LoadXMLFile()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("Data.xml")); //載入XML文件
XmlNodeList NodeList = xmlDoc.SelectNodes("/zws/bz/zw"); //找到XML節點
if (NodeList == null)
{
return;
}
string zw = null;
for (int i = 0; i < NodeList.Count; i++)
{
zw += NodeList[i].ChildNodes[0].InnerText + ",";
}
txtLeader.Text = zw;
}
/// <summary>
/// 刪除XML檔案中的節點及值後在新增節點
/// </summary>
public void UpDateXMLFile()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("zw.xml")); //載入檔案
XmlNode rootElement = xmlDoc.SelectSingleNode("zws");
XmlNodeList xnl = rootElement.ChildNodes;//zws的所有子節點
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.Name == "bz")
{
xe.RemoveAll();
}
}
xmlDoc.Save(Server.MapPath("zw.xml"));
string txtContext = this.txtLeader.Text;
string[] str = txtContext.Split(',');
for (int i = 0; i < str.Length; i++)
{
string xml = str[i];
if (!string.IsNullOrEmpty(xml))
{
XmlNode xmldocselect = xmlDoc.SelectSingleNode("zws/bz"); //載入到的根節點
XmlElement xes = xmlDoc.CreateElement("zw"); //新增的子節點名稱
xes.InnerText = xml;
//xes.AppendChild(xes);
xmldocselect.AppendChild(xes);
xmlDoc.Save(Server.MapPath("zw.xml"));
}
}
}
XML檔案:
XML檔名:Data.XML
<zws>
<bm>2761290</bm>
<mima>123456</pw>
<isopen>0</isopen>
<bz>
<zw>1</zw>
<zw>2</zw>
<zw>3</zw>
<zw>4</zw>
</bz>
</zws>