using System;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;
namespace CustomConfigBuilder
{
public class ConfigXMLBuilder
{
private void appendChildConfigAttributes(XmlDocument doc, XmlElement element, TreeNode node)
{
foreach (TreeNode childNode in node.Nodes)
if (childNode.Tag.ToString() == Shared._attributeImageIndex.ToString())
{
XmlAttribute attr = doc.CreateAttribute(childNode.Text);
element.Attributes.Append(attr);
}
}
private void appendChildConfigElements(XmlDocument document,
XmlElement parentXmlElement,
TreeNode parentTreeNode)
{
foreach (TreeNode node in parentTreeNode.Nodes)
if (node.Tag.ToString() == Shared._elementImageIndex.ToString())
{
if (!Shared.HasChildAttributes(node))
continue;
XmlElement element = document.CreateElement(node.Text + "s");
parentXmlElement.AppendChild(element);
XmlElement elementAdd = document.CreateElement("Add");
element.AppendChild(elementAdd);
appendChildConfigElements(document, elementAdd, node);
}
appendChildConfigAttributes(document, parentXmlElement, parentTreeNode);
}
///
/// Create the actual XML configuration Text for the Web/App Config file.
///
public string BuildXMLConfigDocument(TreeView tvConfigData, string SaveXmlTo)
{
XmlDocument docConfig = new XmlDocument();
docConfig.LoadXml("<" +
tvConfigData.Nodes[Shared._rootConfigNodeName].Text +
">" +
tvConfigData.Nodes[Shared._rootConfigNodeName].Text + ">");
appendChildConfigElements(docConfig,
docConfig.DocumentElement,
tvConfigData.Nodes[Shared._rootConfigNodeName]);
try
{
if (SaveXmlTo.Length > 0 && (!SaveXmlTo.ToUpper().Equals("SAVE XML TO:")))
docConfig.Save(SaveXmlTo);
}
catch (System.Exception exception)
{
MessageBox.Show(exception.Message);
}
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
docConfig.Save(sw);
// txtXMLConfigResults.Text = sb.ToString();
return sb.ToString();
}
}
}
No comments:
Post a Comment