在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
由于所需要操作的XML文件结点格式不统一,故写了一个通用递归类,代码如下: 一、XML文件 <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <Cycles> <SY_DT5_GSM_Month>5</SY_DT5_GSM_Month> <SY_DT5_GSM_Day>50</SY_DT5_GSM_Day> </Cycles> </appSettings> <Transmissions> <GSM> <SY_DT5_GSM Company="" Model="" COM="COM3"> <YesterDay>2011-07-16</YesterDay> <LastMonth> </LastMonth> </SY_DT5_GSM> </GSM> <GRPS> </GRPS> <IP> </IP> </Transmissions> <connectionStrings> <Server>.</Server> <DataBase>Voltage</DataBase> <User>sa</User> <Pass> </Pass> </connectionStrings> </configuration> 二、递归类 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml; /// <summary> ///XmlRWrite 的摘要说明 /// </summary> public class XmlRWrite { private string _FilePath; private string _ParentNode; private string _ChildName; private string _AttributeName; public XmlRWrite() { } /// <summary> /// XML文件存储路径 /// </summary> public string FilePath { set { _FilePath = value; } } /// <summary> /// 父结点 /// </summary> public string ParentNode { set { _ParentNode = value; } } /// <summary> /// 子结点 /// </summary> public string ChildName { set { _ChildName = value; } } /// <summary> /// 结点属性 /// </summary> public string AttributeName { set { _AttributeName = value; } } /// <summary> /// 递归读取XML文件结点值 /// </summary> /// <param name="XNL">XmlNodeList列表</param> /// <returns>返回结点值</returns> private string ReadNodeValue(XmlNodeList XNL) { string Value = ""; for (int i = 0; i < XNL.Count; i++) { if (Value != "") break; if (XNL[i].Name == _ParentNode) { foreach (XmlNode Childnode in XNL[i]) { if (Childnode.Name == _ChildName) { Value = Childnode.InnerText; break; } } } else { XmlNodeList xnl = XNL[i].ChildNodes; if (xnl.Count > 0) Value = ReadNodeValue(xnl); } } return Value; } /// <summary> /// 递归读取XML文件结点属性值 /// </summary> /// <param name="XNL">XmlNodeList列表</param> /// <returns>返回结点值</returns> private string ReadAttributeValue(XmlNodeList XNL) { string Value = ""; for (int i = 0; i < XNL.Count; i++) { if (Value != "") break; if (XNL[i].Name == _ParentNode) { foreach (XmlNode Childnode in XNL[i]) { if (Childnode.Name == _ChildName) { Value = Childnode.Attributes[_AttributeName].Value; break; } } } else { XmlNodeList xnl = XNL[i].ChildNodes; if (xnl.Count > 0) Value = ReadAttributeValue(xnl); } } return Value; } /// <summary> /// 递归设置结点值 /// </summary> /// <param name="Value">结点值</param> /// <param name="XNL">XmlNodeList列表</param> private void WriteNodeValue(string Value, XmlNodeList XNL) { bool bl = false; for (int i = 0; i < XNL.Count; i++) { if (bl) break; if (XNL[i].Name == _ParentNode) { foreach (XmlNode Childnode in XNL[i]) { if (Childnode.Name == _ChildName) { Childnode.InnerText = Value; bl = true; break; } } } else { XmlNodeList xnl = XNL[i].ChildNodes; if (xnl.Count > 0) WriteNodeValue(Value, xnl); } } } /// <summary> /// 递归设置结点属性值 /// </summary> /// <param name="Value">属性值</param> /// <param name="XNL">XmlNodeList列表</param> private void WriteAttributeValue(string Value, XmlNodeList XNL) { bool bl = false; for (int i = 0; i < XNL.Count; i++) { if (bl) break; if (XNL[i].Name == _ParentNode) { foreach (XmlNode Childnode in XNL[i]) { if (Childnode.Name == _ChildName) { Childnode.Attributes[_AttributeName].Value = Value; bl = true; break; } } } else { XmlNodeList xnl = XNL[i].ChildNodes; if (xnl.Count > 0) WriteAttributeValue(Value, xnl); } } } private XmlNodeList XNodeList() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(_FilePath); XmlNodeList XNL = xmlDoc.SelectSingleNode("configuration").ChildNodes; return XNL; } /// <summary> /// 递归读取结点值 /// </summary> /// <returns>返回值</returns> public string GetNodeValue() { XmlNodeList XNL = XNodeList(); string Value = ReadNodeValue(XNL); return Value; } /// <summary> /// 递归读取属性值 /// </summary> /// <returns>返回值</returns> public string GetAttributeValue() { XmlNodeList XNL = XNodeList(); string Value = ReadAttributeValue(XNL); return Value; } /// <summary> /// 递归设置结点值 /// </summary> /// <param name="Value">结点值</param> public void SetNodeValue(string Value) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(_FilePath); XmlNodeList XNL = xmlDoc.SelectSingleNode("configuration").ChildNodes; WriteNodeValue(Value, XNL); xmlDoc.Save(_FilePath); } /// <summary> /// 递归设置结点属性值 /// </summary> /// <param name="Value">属性值</param> public void SetAttributeValue(string Value) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(_FilePath); XmlNodeList XNL = xmlDoc.SelectSingleNode("configuration").ChildNodes; WriteAttributeValue(Value, XNL); xmlDoc.Save(_FilePath); } } 三、使用方法 protected void Button1_Click(object sender, EventArgs e) { string path = "C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\WebSites\\WebSite2\\setup.xml"; XmlRWrite xmlRWrite = new XmlRWrite(); xmlRWrite.FilePath = path; //读取结点值 xmlRWrite.ParentNode = "SY_DT5_GSM"; xmlRWrite.ChildName = "YesterDay"; LblMsg.Text = xmlRWrite.GetNodeValue(); //读取属性值 xmlRWrite.ParentNode = "GSM"; xmlRWrite.ChildName = "SY_DT5_GSM"; xmlRWrite.AttributeName = "COM"; LblMsg.Text = xmlRWrite.GetAttributeValue(); //设置结点值 xmlRWrite.ParentNode = "SY_DT5_GSM"; xmlRWrite.ChildName = "YesterDay"; xmlRWrite.SetNodeValue("2011-07-16"); //设置属性值 xmlRWrite.ParentNode = "GSM"; xmlRWrite.ChildName = "SY_DT5_GSM"; xmlRWrite.AttributeName = "COM"; xmlRWrite.SetAttributeValue("COM3"); } |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论