Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Christian Palm 277 posts 272 karma points
    Jul 02, 2010 @ 13:27
    Christian Palm
    1

    AddXmlNodeToWebConfig

    I have created a AddXmlNodeToWebConfig - you can use it whenever you wan't to add something to the web.config.

    You can provide a XPathCheckIfExist to ensure you do not create duplicates

    Sample

    <Action runat="install" undo="false" alias="AddXmlNodeToWebConfig">
    <Item>
    <!-- Where to insert the node -->
    <XPath>/configuration/configSections</XPath>
    <!-- How to check if the node already exist -->
    <XPathCheckIfExist>/configuration/configSections/section[@name='log4net']</XPathCheckIfExist>
    <XmlNode>
    <!-- The node to append -->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </XmlNode>
    </Item>
    <Item>
    <XPath>/configuration</XPath>
    <XPathCheckIfExist>/configuration/log4net</XPathCheckIfExist>
    <InsertAfter>/configuration/configSections</InsertAfter>
    <XmlNode>
    <log4net configSource="App_Data\Log4Net.config" />
    </XmlNode>
    </Item>
    </Action>

    Source (using log4net, undo not implemented)

    namespace CPalm.Packager.Actions
    {
    using System;
    using System.IO;
    using System.Text;
    using System.Web.Hosting;
    using System.Xml;

    using log4net;
    using umbraco.cms.businesslogic.packager.standardPackageActions;
    using umbraco.interfaces;

    public class AddXmlNodeToWebConfig : IPackageAction
    {
    #region Fields
    private static readonly ILog log = LogManager.GetLogger(typeof(AddXmlNodeToWebConfig));
    #endregion Fields

    #region Methods
    public string Alias()
    {
    return "AddXmlNodeToWebConfig";
    }

    public bool Execute(string packageName, XmlNode xmlData)
    {
    try
    {
    string webConfigPath = HostingEnvironment.MapPath("/web.config");
    if (string.IsNullOrEmpty(webConfigPath))
    {
    return false;
    }
    FileStream fsRead = new FileStream(webConfigPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    XmlDocument webconfig = new XmlDocument();
    webconfig.Load(fsRead);

    fsRead.Dispose();

    bool changed = false;

    XmlNodeList nodes = xmlData.SelectNodes("Item");
    if (nodes != null)
    {
    foreach (XmlNode node in nodes)
    {
    if (AddNode(webconfig, node))
    {
    changed = true;
    }
    }
    }

    if (changed)
    {
    FileStream fsWrite = new FileStream(webConfigPath, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
    webconfig.Save(fsWrite);
    fsWrite.Dispose();
    }
    return true;
    }
    catch (Exception exception)
    {
    log.Error("Exception", exception);
    return false;
    }
    }

    public XmlNode SampleXml()
    {
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("<Action runat=\"install\" undo=\"false\" alias=\"AddXmlNodeToWebConfig\">");
    sb.AppendLine(" <Item>");
    sb.AppendLine(" <XPath>/configuration/configSections</XPath>");
    sb.AppendLine(" <XPathCheckIfExist>/configuration/configSections/section[@name='log4net']</XPathCheckIfExist>");
    sb.AppendLine(" <XmlNode>");
    sb.AppendLine(" <section name=\"log4net\" type=\"log4net.Config.Log4NetConfigurationSectionHandler, log4net\"/>");
    sb.AppendLine(" </XmlNode>");
    sb.AppendLine(" </Item>");
    sb.AppendLine(" <Item>");
    sb.AppendLine(" <XPath>/configuration</XPath>");
    sb.AppendLine(" <XPathCheckIfExist>/configuration/log4net</XPathCheckIfExist>");
    sb.AppendLine(" <InsertAfter>/configuration/configSections</InsertAfter>");
    sb.AppendLine(" <XmlNode>");
    sb.AppendLine(" <log4net configSource=\"App_Data\\Log4Net.config\" />");
    sb.AppendLine(" </XmlNode>");
    sb.AppendLine(" </Item>");
    sb.AppendLine(" <Item>");
    sb.AppendLine(" <XPath>/configuration/system.web/httpModules</XPath>");
    sb.AppendLine(" <XPathCheckIfExist>/configuration/system.web/httpModules/add[@name='UpacEventHttpModule']</XPathCheckIfExist>");
    sb.AppendLine(" <XmlNode>");
    sb.AppendLine(" <add name=\"UpacEventHttpModule\" type=\"Upac.Core.Events.EventHttpModule, Upac.Core\" />");
    sb.AppendLine(" </XmlNode>");
    sb.AppendLine(" </Item>");
    sb.AppendLine(" <Item>");
    sb.AppendLine(" <XPath>/configuration/system.webServer/modules</XPath>");
    sb.AppendLine(" <XPathCheckIfExist>/configuration/system.webServer/modules/add[@name='UpacEventHttpModule']</XPathCheckIfExist>");
    sb.AppendLine(" <XmlNode>");
    sb.AppendLine(" <add name=\"UpacEventHttpModule\" type=\"Upac.Core.Events.EventHttpModule, Upac.Core\" />");
    sb.AppendLine(" </XmlNode>");
    sb.AppendLine(" </Item>");
    sb.AppendLine("</Action>");
    return helper.parseStringToXmlNode(sb.ToString());
    }

    public bool Undo(string packageName, XmlNode xmlData)
    {
    try
    {
    return false;
    }
    catch
    {
    return false;
    }
    }

    private bool AddNode(XmlDocument webconfig, XmlNode xmlData)
    {
    XmlNode xpathNode = xmlData.SelectSingleNode("XPath");
    if (xpathNode == null)
    {
    log.Error("XPath node not found!");
    return false;
    }

    string xpath = xpathNode.InnerText;
    if (string.IsNullOrEmpty(xpath))
    {
    log.Error("XPath value not found!");
    return false;
    }

    XmlNode nodeToInsertNewNodeInto = webconfig.SelectSingleNode(xpath);
    if (nodeToInsertNewNodeInto == null)
    {
    log.Error("nodeToInsertNewNodeInto node not found via xpath!");
    return false;
    }

    XmlNode xmlNode = xmlData.SelectSingleNode("XmlNode");
    if (xpathNode == null)
    {
    log.Error("XmlNode node not found!");
    return false;
    }

    if (xmlNode.ChildNodes.Count == 0)
    {
    log.Error("No child node to XmlNode found!");
    return false;
    }

    if (xmlNode.ChildNodes.Count > 1)
    {
    log.Error("More than one child node found!");
    return false;
    }

    XmlNode checkIfExistXPathNode = xmlData.SelectSingleNode("XPathCheckIfExist");
    if (checkIfExistXPathNode != null && !string.IsNullOrEmpty(checkIfExistXPathNode.InnerText))
    {
    XmlNode nodeAlreadyExist = webconfig.SelectSingleNode(checkIfExistXPathNode.InnerText);
    if (nodeAlreadyExist != null)
    {
    log.Error("New node already exist!");
    return false;
    }
    }

    XmlNode newNode = webconfig.ImportNode(xmlNode.FirstChild, true);

    XmlNode insertAfterNode = null;

    XmlNode insertAfterNodeXpath = xmlData.SelectSingleNode("InsertAfter");
    if (insertAfterNodeXpath != null && !string.IsNullOrEmpty(insertAfterNodeXpath.InnerText))
    {
    insertAfterNode = webconfig.SelectSingleNode(insertAfterNodeXpath.InnerText);
    if (insertAfterNode == null)
    {
    log.Error("insertAfterNode not found, but will insert the new node to XPath location!");
    }
    }

    if (insertAfterNode != null)
    {
    nodeToInsertNewNodeInto.InsertAfter(newNode, insertAfterNode);
    }
    else
    {
    nodeToInsertNewNodeInto.PrependChild(newNode);
    }
    return true;
    }

    #endregion Methods
    }
    }
  • Jamie Howarth 306 posts 773 karma points c-trib
    Jul 02, 2010 @ 14:49
    Jamie Howarth
    0

    That's awesome - good job! Great for not using WebConfigurationManager too, modifying the web.config only works through an XmlDocument in Medium Trust.

    I'll bring this to Rich Soeteman's attention.

    Best,

    Benjamin :-)

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Jul 05, 2010 @ 09:39
    Richard Soeteman
    0

    Nice Christian,

    Will add it for the nex release.

    Thanks,

    Richard

Please Sign in or register to post replies

Write your reply to:

Draft