本文中主要列举了C#中对XML文件的基本读写方法,仅供参考。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; namespace PCS { struct stcXmlInfo { public string strName; public string strMin; public string strMax; public string strInit; } class CDealXmlWL { public static CDealXmlWL dealWL; public CDealXmlWL() { dealWL = this; } string _strXmlPath; //【XML】读操作; public void ReadXml(string strXmlPath) { _strXmlPath = strXmlPath; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(_strXmlPath); XmlNodeList nodeChilds = xmlDoc.SelectSingleNode("WLSettings").ChildNodes; CDealXmlWL.dealWL.PSV = LoadXmlWLOne(nodeChilds[0]); CDealXmlWL.dealWL.PSA = LoadXmlWLOne(nodeChilds[1]); } //【XML】写操作; public void WriteXml() { XmlDocument xmlDoc = new XmlDocument(); //创建XML的声明; XmlDeclaration xmlDec; xmlDec = xmlDoc.CreateXmlDeclaration("1.0","gb2312",null); xmlDoc.AppendChild(xmlDec); //创建XML的一个节点; XmlElement eleRoot = xmlDoc.CreateElement("","WLSettings",""); xmlDoc.AppendChild(eleRoot); //选定刚创建的根节点; XmlNode nodeRoot = xmlDoc.SelectSingleNode("WLSettings"); //创建子节点; dealOneXmlElement(xmlDoc,nodeRoot,PSV.strName,PSV.strMin,PSV.strMax,PSV.strInit); dealOneXmlElement(xmlDoc,PSA.strName,PSA.strMin,PSA.strMax,PSA.strInit); //保存新生成的XML文件; FileStream fileStream = new FileStream(_strXmlPath,FileMode.Create,FileAccess.Write); xmlDoc.Save(fileStream); fileStream.Dispose(); } //拷贝一个【XML】中的某一节点到另一个【XML】文件中; private void copyNode() { //【源】从要拷贝的源文件【xmlDocOut】中确定要拷贝的节点【xmlNode】; XmlDocument xmlDocOut = new XmlDocument(); xmlDocOut.Load("D:\\GWei\\练习\\C#\\xmlTest\\xmlTest\\gw2.xml"); XmlNode xmlNew = xmlDocOut.SelectSingleNode("/LogTool"); //【目标】从【xmlDocOut】中拷贝某节点内容到【xmlDoc】的指定节点中; XmlDocument xmlDoc = new XmlDocument(); //创建XML的声明; XmlDeclaration xmlDec; xmlDec = xmlDoc.CreateXmlDeclaration("1.0",""); xmlDoc.AppendChild(eleRoot); //选定刚创建的根节点; XmlNode nodeRoot = xmlDoc.SelectSingleNode("WLSettings"); //插入新节点; XmlNode xmlNode = xmlDoc.ImportNode(xmlNew,true); nodeRoot.AppendChild(xmlNode); } private void dealOneXmlElement(XmlDocument xmlDoc,XmlNode nodeRoot,string strName,string strMin,string strMax,string strInit) { //创建子节点; XmlElement xmlEle = xmlDoc.CreateElement("WLSetting"); xmlEle.SetAttribute("Name",strName); xmlEle.SetAttribute("Min",strMin); xmlEle.SetAttribute("Max",strMax); xmlEle.SetAttribute("Init",strInit); nodeRoot.AppendChild(xmlEle); } private stcXmlInfo LoadXmlWLOne(XmlNode xmlNodeIn) { stcXmlInfo stcTmp = new stcXmlInfo(); stcTmp.strName = xmlNodeIn.Attributes[0].InnerText; stcTmp.strMin = xmlNodeIn.Attributes[1].InnerText; stcTmp.strMax = xmlNodeIn.Attributes[2].InnerText; stcTmp.strInit = xmlNodeIn.Attributes[3].InnerText; return stcTmp; } stcXmlInfo psv = new stcXmlInfo(); public stcXmlInfo PSV //安全电压; { set { psv = value; } get { return psv; } } stcXmlInfo psa = new stcXmlInfo(); public stcXmlInfo PSA //安全电流; { set { psa = value; } get { return psa; } } } }
<?xml version="1.0" encoding="gb2312"?> <WLSettings> <WLSetting Name="PS Volt Setting" Min="20" Max="60" Init="23" /> <WLSetting Name="PS ma Setting" Min="100" Max="180" Init="150" /> </WLSettings>原文链接:https://www.f2er.com/xml/298565.html