Click here to Skip to main content
13,662,961 members
Click here to Skip to main content

Stats

31.1K views
469 downloads
28 bookmarked
Posted 12 Apr 2016
Licenced Ms-PL

Linq-To-XML Style of Node Creation for C++

, 12 Apr 2016
Linq-To-XML Node Creation for Native C++
BuildProcessTemplates
Elmax.vsmdi
ElmaxNet
Properties
Elmax
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
StringUtils
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
TryoutNet
Properties
Tryout
res
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax-ver089
Elmax.vsmdi
Elmax
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
StringUtils
ElmaxNet
Properties
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout
res
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
TryoutNet
Properties
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Elmax
{
    public class RootElement : Element
    {
        /// <summary>
        /// Can the file be written to?
        /// </summary>
        /// <param name="FilePath">the fully qualified file and path name for the file</param>
        /// <param name="OverWrite">true if the existing file can be overwritten</param>
        /// <param name="CreateFolder">true to create the folders if they are not already there</param>
        /// <returns></returns>
        protected bool CanWriteFile(string FilePath, bool OverWrite, bool CreateFolder)
        {
	        if (File.Exists(FilePath))
	        {
		        return OverWrite;
	        }

            string Folder = Path.GetDirectoryName(FilePath);

            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(Folder);
                if(dirInfo.Exists)
                    return true;
            }
            catch (System.Exception)
            {
            	
            }

	        if (CreateFolder)
	        {
		        DirectoryInfo dirInfoFolder = Directory.CreateDirectory(Folder);

                return dirInfoFolder.Exists;
	        }

	        return false;

        }
        /// <summary>
        /// Constructor loads the supplied file if it exists or creates an empty XML DOM if it does not
        /// </summary>
        /// <param name="FilePath"></param>
        public RootElement(string FilePath = "") 
            : base()
        { 
            LoadFile(FilePath); 
        }
        
        /// <summary>
        /// Load a XML file
        /// </summary>
        /// <param name="FilePath">loads the supplied file</param>
        /// <returns>return true if successful</returns>
        public bool LoadFile(string FilePath = "")
        {
            UnloadFile();

            _Doc = new XmlDocument();

            if (File.Exists(FilePath))
            {
                _Doc.Load(FilePath);
            }

            SetDomDoc(_Doc);
            m_FilePath = FilePath;

            return true;
        }

		//! 
        /// <summary>
        /// Check if a file is loaded into the object
        /// </summary>
        /// <returns>returns true if file is loaded</returns>
        public bool IsFileLoaded() 
        { 
            return null != _Doc; 
        }

        /// <summary>
        /// Unload the file from memory - it is NOT saved first
        /// </summary>
        /// <returns>always true</returns>
        public bool UnloadFile()
        {
            _Doc = null;  // calls Release() for us
            m_FilePath = string.Empty;
            return true;
        }
        
        /// <summary>
        /// Get the path to the XML file
        /// </summary>
        /// <returns>XML File Path</returns>
        public string GetFilePath() 
        { 
            return m_FilePath; 
        }

        /// <summary>
        /// Set a new path for the XML file
        /// </summary>
        /// <param name="FilePath">XML File Path</param>
        /// <returns>false if no document is currently loaded</returns>
        public bool SetFilePath(string FilePath)
        {
            if (null == _Doc)
            {
                return false;
            }

            m_FilePath = FilePath;
            return true;
        }

        /// <summary>
        /// Save the file, specify a new path to change the files name and/or location
        /// </summary>
        /// <param name="FilePath">the fully qualified file and path name for the file</param>
        /// <param name="OverWrite">true if the existing file can be overwritten</param>
        /// <param name="CreateFolder">true to create the folders if they are not already there</param>
        /// <returns>true if the file was successfully saved</returns>
        public bool SaveFile(string FilePath = "", bool OverWrite = true, bool CreateFolder = false)
        {
	        if (null == _Doc)
	        {
		        return false;
	        }

	        if (string.IsNullOrEmpty(FilePath)==false)
	        {
		        m_FilePath = FilePath;
	        }

	        if (CanWriteFile(m_FilePath, OverWrite, CreateFolder))
	        {
		        Elmax.Document doc = new Elmax.Document(_Doc);
		        return doc.PrettySave(m_FilePath);
	        }

	        return false;
        }

        /// <summary>
		/// File path name for the XML document
		/// </summary>
        private string m_FilePath;

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Cake Processor
Software Developer (Senior)
United States United States
C++ developer transitioning to Python

Right now, I am picking up DevOps skills at Pluralsight and pursuing CCNA certification. Stay tuned for my CCNA related article!

Coding Tidbit Blog

Latest blogpost: C++ – The Forgotten Trojan Horse by Eric Johnson

Yinyang Theory Blog

If you like unconventional wisdom based on Yinyang philosophy, hop over to read my Yinyang blog.

Latest blogpost: Fortune Telling: Good News or Bad News For You?

IT Certifications

  • IT Infrastructure Library Foundational (ITIL v3)
  • Scrum Alliance Certified Scrum Master (CSM)
  • Certified Secure Software Lifecycle Professional (CSSLP)

View my certificates here.

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 12 Apr 2016
Article Copyright 2016 by Cake Processor
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid