Load With Includes : XmlDocument « XML « C# / C Sharp






Load With Includes

        

#region using
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
#endregion

namespace XmlGenSharp.Lib.Utility
{
  public class XmlDocumentLoader
  {
    // Public Static Methods

    #region LoadWithIncludes(string filename)
    public static XmlDocument LoadWithIncludes(string filename)
    {
      XmlDocument xml = new XmlDocument();

      xml.Load(filename);
      ProcessIncludeNodes(xml, xml.DocumentElement, Path.GetDirectoryName(filename));

      return xml;
    }
    #endregion

    // Private Static Methods

    #region ProcessIncludeNodes(XmlDocument xml, XmlNode parent, string basePath)
    public static void ProcessIncludeNodes(XmlDocument xml, XmlNode parent, string basePath)
    {
      List<XmlNode> removeList = new List<XmlNode>();

      foreach (XmlNode node in parent.ChildNodes)
      {
        if (node.Name.ToLower() == "include")
        {
          string filename = node.Attributes["filename"].Value as string;
          if (!Path.IsPathRooted(filename))
            filename = Path.Combine(basePath, filename);

          XmlDocument include = XmlDocumentLoader.LoadWithIncludes(filename);

          foreach (XmlNode inner in include.DocumentElement.ChildNodes)
          {
            XmlNode imported = xml.ImportNode(inner, true);
            parent.InsertBefore(imported, node);
          }

          removeList.Add(node);
        }
        else
          ProcessIncludeNodes(xml, node, basePath);
      }

      foreach (XmlNode node in removeList)
        parent.RemoveChild(node);
    }
    #endregion
  }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.A Simple XML Example
2.LoadXml
3.Save, AppendChild, CreateXmlDeclaration, CreateElement
4.Call GetElementsByTagName to get an element
5.Use Load method in XmlDocument to load xml document
6.Use SelectNodes to query nodes by XPath
7.Get XML Nodes in a Specific XML Namespace
8.Find Elements with an XPath Search
9.Add XmlElement
10.Non CData Normalize