增:
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Web; using System.Xml; namespace AppService { public class XMLReader { public static void XMLMake(string inDesString) { try { string s = inDesString; string[] resultString = s.Split(‘|‘); foreach (string i in resultString) { } XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(dec); //创建一个根节点(一级) XmlElement root = doc.createElement_x("jokes"); doc.AppendChild(root); //创建节点(二级) XmlNode node = doc.createElement_x("tag"); //创建节点(三级) XmlElement element1 = doc.createElement_x("shenchang"); //element1.SetAttribute("Name", "Sam"); //element1.SetAttribute("ID", "665"); element1.InnerText = resultString[0]; node.AppendChild(element1); XmlElement element2 = doc.createElement_x("tizhong"); //element2.SetAttribute("Name", "Round"); //element2.SetAttribute("ID", "678"); element2.InnerText = resultString[1]; node.AppendChild(element2); XmlElement element3 = doc.createElement_x("bmi"); //element2.SetAttribute("Name", "Round"); //element2.SetAttribute("ID", "678"); element3.InnerText = resultString[2]; node.AppendChild(element3); root.AppendChild(node); string p = AppDomain.CurrentDomain.BaseDirectory + "Version1"; doc.Save(p + "\\ABC.xml"); Console.Write(doc.OuterXml); } catch { } } } }
改:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml; namespace AppService { public class XMLWrite { public static void XMLUpdate(String inDesString) { string s = inDesString; string[] resultString = s.Split(‘|‘); try { XmlDocument xmlDoc = new XmlDocument(); string p = AppDomain.CurrentDomain.BaseDirectory + "Version1"; xmlDoc.Load(p + "\\ABC.xml"); XmlNode xns = xmlDoc.SelectSingleNode("jokes"); XmlNodeList xnl = xns.ChildNodes; foreach (XmlNode xn in xnl) { XmlElement xe = (XmlElement)xn; XmlNodeList xnl2 = xe.ChildNodes; foreach (XmlNode xn2 in xnl2) { XmlElement xe2 = (XmlElement)xn2; if (xe2.Name == "shenchang") { xe2.InnerText = resultString[0]; } if (xe2.Name == "tizhong") { xe2.InnerText = resultString[1]; } if (xe2.Name == "bmi") { xe2.InnerText = resultString[2]; } } } xmlDoc.Save(p + "\\ABC.xml"); } catch { } } } }
时间: 2024-11-14 12:35:13