Click here to Skip to main content
13,626,873 members
Click here to Skip to main content

Stats

30.6K views
468 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.Text;
using System.Xml;

namespace Elmax
{
    /// <summary>
    /// Helper class to use the .NET XmlDocument's XPath methods
    /// </summary>
    public class Document
    {
        /// <summary>
        /// Non-default constructor
        /// </summary>
        /// <param name="doc"></param>
        public Document(XmlDocument doc) 
        {
            _Doc = doc;
        }
        /// <summary>
        /// Get elements by tag name
        /// </summary>
        /// <param name="tagName">Is Tag name</param>
        /// <returns>List of elements that have the same tag name</returns>
	    public List<Elmax.Element> GetElementsByTagName(string tagName)
        {
            if (_Doc == null)
                throw new System.InvalidOperationException("Invalid document");

            List<Elmax.Element> list = new List<Elmax.Element>();

            XmlNodeList nodeList = _Doc.GetElementsByTagName(tagName);

            ConvNodeListToList(nodeList, ref list);

            return list;
        }
        /// <summary>
        /// Get elements by tag name within certain namespace URI
        /// </summary>
        /// <param name="tagName">Is Tag name</param>
        /// <param name="namespaceURI"></param>
        /// <returns>List of elements that have the same tag name</returns>
        public List<Elmax.Element> GetElementsByTagName(string tagName, string namespaceURI)
        {
            if (_Doc == null)
                throw new System.InvalidOperationException("Invalid document");

            List<Elmax.Element> list = new List<Elmax.Element>();

            XmlNodeList nodeList = _Doc.GetElementsByTagName(tagName, namespaceURI);

            ConvNodeListToList(nodeList, ref list);

            return list;
        }
        /// <summary>
        /// Get element by Id
        /// </summary>
        /// <param name="id">is Id</param>
        /// <returns>Element</returns>
        public Elmax.Element GetElementById(string id)
        {
            if (_Doc == null)
                throw new System.InvalidOperationException("Invalid document");

            XmlNode node = _Doc.GetElementById(id);

            if(node!=null)
                return new Element(_Doc, node, "", node.Name, true, false);

            return new Element();
        }
        /// <summary>
        /// Select the elements using XPath expression
        /// </summary>
        /// <param name="xpath">XPath expression</param>
        /// <returns>List of elements which satisfy the XPath expression</returns>
	    public List<Elmax.Element> SelectNodes(string xpath)
        {
            if (_Doc == null)
                throw new System.InvalidOperationException("Invalid document");
            
            List<Elmax.Element> list = new List<Elmax.Element>();

	        XmlNodeList nodeList = _Doc.SelectNodes(xpath);

	        ConvNodeListToList(nodeList, ref list);

	        return list;
        }
        /// <summary>
        /// Select the element using XPath expression
        /// </summary>
        /// <param name="xpath">XPath expression</param>
        /// <returns>Element which satisfies the XPath expression</returns>
	    public Elmax.Element SelectSingleNode(string xpath)
        {
            if (_Doc == null)
                throw new System.InvalidOperationException("Invalid document");

	        XmlNode node = _Doc.SelectSingleNode(xpath);

            if (node != null)
            {
                return new Element(_Doc, node, "", node.Name, true, false);
            }

            return new Element();
        }
        /// <summary>
        /// Convert a list of nodes to a list of Elmax elements
        /// </summary>
        /// <param name="pList"></param>
        /// <param name="list"></param>
        private void ConvNodeListToList(XmlNodeList pList, ref List<Elmax.Element> list)
        {
	        if(pList==null)
		        return;

	        for(int i=0; i<pList.Count; ++i)
	        {
		        Element ele = new Element(_Doc, pList[i], "", pList[i].Name, true, false );
		        list.Add(ele);
	        }
        }

        /// <summary>
        /// Save with the indentation and newlines
        /// </summary>
        /// <param name="file">file path</param>
        /// <returns>true if successful</returns>
        public bool PrettySave(string file)
        {
            if (_Doc == null)
                return false;

            XmlWriter writer = null;
            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.IndentChars = "  ";
                settings.NewLineChars = "\r\n";
                settings.NewLineHandling = NewLineHandling.Replace;
                settings.OmitXmlDeclaration = false;
                writer = XmlWriter.Create(file, settings);
                _Doc.Save(writer);
            }
            catch (System.Exception)
            {
                return false;            	
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                    writer = null;
                }
            }
            return true;
        }

        /// <summary>
        /// Internal XML document object
        /// </summary>
        private XmlDocument _Doc;
    }
}

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
Web03 | 2.8.180712.1 | Last Updated 12 Apr 2016
Article Copyright 2016 by Cake Processor
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid