您的位置:首页 > 其它

XML读取、XML修改、XML删除

2015-09-14 18:26 761 查看
第一种:单独写一个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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: