读写xml文件需要引用using System.Xml;命名空间。
下面给几个函数例子分别读写xml文件。
创建XML文件:
1 //------------【函数:创建xml文件】------------ 2 3 //filePath为Excel文件路径名 4 //nodeName根节点名称 5 //------------------------------------------------------------------------ 6 public static bool CreateNewXML(string filePath,string nodeName) 7 { 8 bool result = true; 9 try 10 { 11 //判断文件夹是否存在 12 string mystr1 = NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly)); 13 if (mystr1 != "error"&& nodeName!="") 14 { 15 XmlDocument xmlDoc = new XmlDocument(); 16 XmlNode header = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); 17 xmlDoc.AppendChild(header); 18 19 XmlElement xm = xmlDoc.CreateElement(nodeName); 20 21 xmlDoc.AppendChild(xm); 22 23 xmlDoc.Save(filePath); 24 } 25 else 26 { 27 result = false; 28 } 29 30 } 31 catch 32 { 33 result = false; 34 } 35 36 return result; 37 }
向XML文件中添加有个节点:
1 //------------【函数:创建新的节点node】------------ 2 3 //filePath为Excel文件路径名 4 //nodeName节点名称 5 //nodeID节点属性名称 6 //nodeIDValue节点属性值 7 //nodeText节点内容 8 //------------------------------------------------------------------------ 9 public static bool AppendNewNode(string filePath,string nodeName,string nodeID,string nodeIDValue,string nodeText) 10 { 11 bool result = true; 12 try 13 { 14 //判断文件夹是否存在 15 string mystr1 = NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly)); 16 if (mystr1 != "error" && nodeName != "") 17 { 18 XmlDocument xmlDoc = new XmlDocument();//新建XML文件 19 XmlElement xm = null; 20 if (!File.Exists(filePath)) 21 { 22 XmlNode header = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); 23 xmlDoc.AppendChild(header); 24 xm = xmlDoc.CreateElement(nodeName); 25 xm.SetAttribute(nodeID, nodeIDValue); 26 xm.InnerText = nodeText; 27 xmlDoc.AppendChild(xm); 28 } 29 else 30 { 31 xmlDoc.Load(filePath);//加载XML文件 32 XmlElement root = xmlDoc.DocumentElement; 33 xm = xmlDoc.CreateElement(nodeName); 34 xm.SetAttribute(nodeID, nodeIDValue); 35 xm.InnerText = nodeText; 36 root.AppendChild(xm); 37 } 38 39 xmlDoc.Save(filePath); 40 41 } 42 else 43 { 44 result = false; 45 } 46 } 47 catch 48 { 49 result = false; 50 } 51 52 return result; 53 }
为XML文件指定节点添加一个子节点:
1 //------------【函数:为节点node创建新的子节点node】------------ 2 3 //filePath为Excel文件路径名 4 //nodeName节点名称 5 //nodeID节点属性名称 6 //nodeIDValue节点属性值 7 //nodeText节点内容 8 //FatherNode父节点 9 //------------------------------------------------------------------------ 10 public static bool AppendNodeToNode(string filePath, string nodeName, string nodeID, string nodeIDValue, string nodeText,string FatherNode) 11 { 12 bool result = true; 13 try 14 { 15 //判断文件是否存在 16 if (File.Exists(filePath) && nodeName != ""&&FatherNode !="") 17 { 18 XmlDocument xmlDoc = new XmlDocument();//新建XML文件 19 xmlDoc.Load(filePath);//加载XML文件 20 21 XmlElement xm = xmlDoc.CreateElement(nodeName); 22 xm.SetAttribute(nodeID, nodeIDValue); 23 xm.InnerText = nodeText; 24 25 XmlNode root = xmlDoc.GetElementsByTagName(FatherNode)[0]; 26 root.AppendChild(xm); 27 28 xmlDoc.Save(filePath); 29 30 } 31 else 32 { 33 result = false; 34 } 35 } 36 catch 37 { 38 result = false; 39 } 40 41 return result; 42 }
读取XML文件中指定节点的值:
1 //------------【函数:获取节点node中的值】------------ 2 3 //filePath为Excel文件路径名 4 //nodeName节点名称 5 //------------------------------------------------------------------------ 6 public static string ReadNodeFromXML(string filePath,string nodeName) 7 { 8 string result = "error"; 9 try 10 { 11 //判断文件是否存在 12 if (File.Exists(filePath) && nodeName != "") 13 { 14 XmlDocument xmlDoc = new XmlDocument();//新建XML文件 15 xmlDoc.Load(filePath);//加载XML文件 16 17 XmlNode xm = xmlDoc.GetElementsByTagName(nodeName)[0]; 18 result = xm.InnerText; 19 20 } 21 else 22 { 23 result = "error"; 24 } 25 } 26 catch 27 { 28 result = "error"; 29 } 30 return result; 31 }