public static void RunTest_Memory(string _filename, string _rootNode) { _xmlDoc = new PFXmlDocument(_rootNode); XmlElement xmlElement; XmlElement saveXmlElement; xmlElement = _xmlDoc.AddNewElement(_xmlDoc.DocumentRootNode, "ProcessingDate", DateTime.Now.ToString()); saveXmlElement = _xmlDoc.AddNewElement(_xmlDoc.DocumentRootNode, "IsEnabled", "True"); xmlElement = _xmlDoc.AddNewElement(saveXmlElement, "node1", "text1"); xmlElement = _xmlDoc.AddNewElement(saveXmlElement, "node2", "text2"); _xmlDoc.AddNewAttribute(xmlElement, "attr1", "val1"); xmlElement = _xmlDoc.AddNewElement(saveXmlElement, "node3", "text3"); _xmlDoc.AddNewAttribute(xmlElement, "attr3a", "val3a"); _xmlDoc.AddNewAttribute(xmlElement, "attr3b", "val3b"); xmlElement = _xmlDoc.AddNewElement(_xmlDoc.DocumentRootNode, "FormBackground", "Red"); _xmlDoc.SaveToFile(_filename); PFXmlDocument xmlDoc2 = new PFXmlDocument(); xmlDoc2.LoadFromFile(_filename); Program._messageLog.WriteLine(xmlDoc2.OuterXml); Program._messageLog.WriteLine("\r\n" + xmlDoc2.InnerText); Program._messageLog.WriteLine("\r\n" + xmlDoc2.InnerXml); }
public static void CreateXMLDoc(string _filename, string _rootNode) { _xmlDoc = new PFXmlDocument(_rootNode); XmlDeclaration xmldec = _xmlDoc.XmlDoc.CreateXmlDeclaration("1.0", "utf-16", null); //XmlElement root = _xmlDoc.XmlDoc.DocumentElement; //_xmlDoc.XmlDoc.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); //_xmlDoc.XmlDoc.DocumentElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); //_xmlDoc.XmlDoc.InsertBefore(xmldec, root); _xmlDoc.SaveToFile(_filename); _msg.Length = 0; if (File.Exists(_filename)) { _msg.Append("File created: "); _msg.Append(_filename); } else { _msg.Append("File create failed for : "); _msg.Append(_filename); } Program._messageLog.WriteLine(_msg.ToString()); }
public static void AddNodes(MainForm frm) { bool err = false; XmlElement xmlElement; StringBuilder nodePath = new StringBuilder(); Program._messageLog.Clear(); if (frm.txtNodePath.Text.Length == 0) { Program._messageLog.WriteLine("Node path must be specified."); err = true; } if (err) { return; } if (frm.txtNodePath.Text.Length > 0) { if (frm.txtNodeTag.Text.Length > 0) { nodePath.Length = 0; nodePath.Append(frm.txtRootNode.Text); nodePath.Append("/"); nodePath.Append(frm.txtNodePath.Text); try { xmlElement = _xmlDoc.FindElement(nodePath.ToString()); if (xmlElement == null) { xmlElement = _xmlDoc.FindElement(frm.txtRootNode.Text); _xmlDoc.AddNewElement(xmlElement, frm.txtNodePath.Text, string.Empty); xmlElement = _xmlDoc.FindElement(nodePath.ToString()); } _xmlDoc.AddNewElement((XmlNode)xmlElement, frm.txtNodeTag.Text, frm.txtNodeInnerText.Text); } catch (Exception ex) { _msg.Length = 0; _msg.Append("Attempt to add element failed.\n"); _msg.Append("Node tag: "); _msg.Append(frm.txtNodeTag.Text); _msg.Append("\n\n"); _msg.Append(AppMessages.FormatErrorMessage(ex)); AppMessages.DisplayErrorMessage(_msg.ToString()); } _xmlDoc.SaveToFile(frm.txtFilename.Text); } if (frm.txtAttribute.Text.Length > 0) { nodePath.Length = 0; nodePath.Append(frm.txtRootNode.Text); nodePath.Append("/"); nodePath.Append(frm.txtNodePath.Text); if (frm.txtNodeTag.Text.Length > 0) { nodePath.Append("/"); nodePath.Append(frm.txtNodeTag.Text); } try { xmlElement = _xmlDoc.FindElement(nodePath.ToString()); _xmlDoc.AddNewAttribute(xmlElement, frm.txtAttribute.Text, frm.txtAttributeValue.Text); } catch (Exception ex) { _msg.Length = 0; _msg.Append("Attempt to add attribute failed.\n"); _msg.Append("Node tag: "); _msg.Append(frm.txtNodeTag.Text); _msg.Append("\n\n"); _msg.Append(AppMessages.FormatErrorMessage(ex)); AppMessages.DisplayErrorMessage(_msg.ToString()); } _xmlDoc.SaveToFile(frm.txtFilename.Text); } } OutputContentsOfFile(frm.txtFilename.Text); }