/// <summary> /// Sets the application configuration setting. /// </summary> /// <param name="setting">Setting to be changed/added.</param> /// <param name="val">Value to change/add.</param> public static void SetValue(string setting, string val) { bool changed = false; System.Reflection.Assembly asm = System.Reflection.Assembly.GetEntryAssembly(); System.IO.FileInfo fi = new System.IO.FileInfo(asm.Location + ".config"); System.Xml.XmlDataDocument doc = new System.Xml.XmlDataDocument(); try { //Load the XML application configuration file doc.Load(fi.FullName); //Loops through the nodes to find the target node to change foreach (System.Xml.XmlNode node in doc["configuration"]["appSettings"]) { if (node.Name == "add") { //Set the key and value attributes if (node.Attributes.GetNamedItem("key").Value == setting) { node.Attributes.GetNamedItem("value").Value = val; //Flag the change as complete changed = true; } } } //If not changed yet then we assume it's a new key to be added if (!changed) { //create the new node and append it to the collection System.Xml.XmlNode node = doc["configuration"]["appSettings"]; System.Xml.XmlElement elem = doc.CreateElement("add"); System.Xml.XmlAttribute attrKey = doc.CreateAttribute("key"); System.Xml.XmlAttribute attrVal = doc.CreateAttribute("value"); elem.Attributes.SetNamedItem(attrKey).Value = setting; elem.Attributes.SetNamedItem(attrVal).Value = val; node.AppendChild(elem); } //Save the XML configuration file. doc.Save(fi.FullName); } catch { //There was an error loading or reading the config file...throw an error. throw new Exception("Unable to set the value. Check that the configuration file exists and contains the appSettings element."); } }
private XmlDataDocument buildXmlDoc(XmlDataDocument xmldata) { XmlDataDocument xmldoc = new XmlDataDocument(); //建立数据xml结构 //xmldoc.LoadXml("<XML><etpTemplate_" + this.workitem.TempId + "_xmlland><" + this.unitItem.UnitName + ">" + ((System.Xml.XmlDocument)(xmldata)).DocumentElement.InnerXml + "</" + this.unitItem.UnitName + "></etpTemplate_" + this.workitem.TempId + "_xmlland></XML>"); xmldoc.LoadXml("<XML><etpTemplate_" + this.workitem.TempId + "_xmlland>" + ((System.Xml.XmlDocument)(xmldata)).DocumentElement.InnerXml + "</etpTemplate_" + this.workitem.TempId + "_xmlland></XML>"); XmlAttribute xmlAtt = xmldoc.DocumentElement.Attributes.Append(xmldoc.CreateAttribute("id")); xmlAtt.Value = this.workitem.TempId; xmlAtt = xmldoc.DocumentElement.Attributes.Append(xmldoc.CreateAttribute("itemname")); xmlAtt.Value = this.workitem.ItemName; xmlAtt = xmldoc.DocumentElement.Attributes.Append(xmldoc.CreateAttribute("typexml")); xmlAtt.Value = "Data"; return xmldoc; }
/// <summary> /// Sets the application configuration setting. /// </summary> /// <param name="setting">Setting to be changed/added.</param> /// <param name="val">Value to change/add.</param> public static void SetValue(string setting, string val) { bool changed=false; System.Reflection.Assembly asm=System.Reflection.Assembly.GetEntryAssembly(); System.IO.FileInfo fi=new System.IO.FileInfo(asm.Location+".config"); System.Xml.XmlDataDocument doc= new System.Xml.XmlDataDocument(); try { //Load the XML application configuration file doc.Load(fi.FullName); //Loops through the nodes to find the target node to change foreach (System.Xml.XmlNode node in doc["configuration"]["appSettings"]) { if (node.Name=="add") { //Set the key and value attributes if (node.Attributes.GetNamedItem("key").Value==setting) { node.Attributes.GetNamedItem("value").Value=val; //Flag the change as complete changed=true; } } } //If not changed yet then we assume it's a new key to be added if (!changed) { //create the new node and append it to the collection System.Xml.XmlNode node=doc["configuration"]["appSettings"]; System.Xml.XmlElement elem=doc.CreateElement("add"); System.Xml.XmlAttribute attrKey=doc.CreateAttribute("key"); System.Xml.XmlAttribute attrVal=doc.CreateAttribute("value"); elem.Attributes.SetNamedItem(attrKey).Value=setting; elem.Attributes.SetNamedItem(attrVal).Value=val; node.AppendChild(elem); } //Save the XML configuration file. doc.Save(fi.FullName); } catch { //There was an error loading or reading the config file...throw an error. throw new Exception("Unable to set the value. Check that the configuration file exists and contains the appSettings element."); } }
private void save() { XmlDataDocument xml_doc = new XmlDataDocument (); XmlNode root = xml_doc.CreateElement ("vocab"); foreach (LessonNode l in LessonStore) { XmlNode lesson = xml_doc.CreateElement ("lesson"); XmlAttribute a_id = xml_doc.CreateAttribute ("id"); XmlAttribute a_description = xml_doc.CreateAttribute ("description"); a_id.Value = l.Id.ToString (); a_description.Value = l.Description; lesson.Attributes.Append (a_id); lesson.Attributes.Append (a_description); foreach (PairNode p in l.PairStore) { XmlNode pair = xml_doc.CreateElement ("pair"); XmlNode en = xml_doc.CreateElement ("en"); XmlNode de = xml_doc.CreateElement ("de"); en.InnerText = p.En; de.InnerText = p.De; pair.AppendChild (en); pair.AppendChild (de); lesson.AppendChild (pair); } root.AppendChild (lesson); } xml_doc.AppendChild (root); xml_doc.Save (xml_path); }
private string GetWebConfigModValue(string nodeName, XmlAttributeCollection attributes) { XmlDataDocument xDoc = new XmlDataDocument(); XmlAttribute newAttribute; XmlNode modValueNode = xDoc.AppendChild(xDoc.CreateElement(nodeName)); foreach (XmlAttribute attribute in attributes) { newAttribute = xDoc.CreateAttribute(attribute.Name); newAttribute.Value = attribute.Value; modValueNode.Attributes.Append(newAttribute); } return string.Format("{0}\n", modValueNode.OuterXml); }