Click here to Skip to main content
12,243,752 members (56,169 online)
Click here to Skip to main content

Stats

21.3K views
427 downloads
27 bookmarked
Posted

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

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

namespace Elmax
{
    /// <summary>
    /// Helper class to join similar elements based on certain criteria
    /// </summary>
    public class HyperElement
    {
        public delegate bool DoubleElementPredicate(Elmax.Element elem1, Elmax.Element elem2);

        /// <summary>
        /// Join one element to another element
        /// </summary>
        /// <param name="listElem1">1st list of elements to join to</param>
        /// <param name="attrName1">Attribute name of the 1st list whose value to be matched. If empty, element value will be used to match instead.</param>
        /// <param name="listElem2">2nd list of elements to join to</param>
        /// <param name="attrName2">Attribute name of the 2nd list whose value to be matched. If empty, element value will be used to match instead.</param>
        /// <param name="caseSensitive">States if the comparision is case-sensitive.</param>
        /// <returns>List of pair of element</returns>
        public static List< KeyValuePair<Elmax.Element, Elmax.Element> >
	        JoinOneToOne(
	            List<Elmax.Element> listElem1,
	            string attrName1,
	            List<Elmax.Element> listElem2,
	            string attrName2,
	            bool caseSensitive)
        {
            string str1, str2;
            List<KeyValuePair<Elmax.Element, Elmax.Element>> vecResults = new List<KeyValuePair<Elmax.Element, Elmax.Element>>();
            for (int i = 0; i < listElem1.Count; ++i)
            {
                if (attrName1 == "")
                    str1 = listElem1[i].GetString("");
                else
                    str1 = listElem1[i].Attribute(attrName1).GetString("");

                for (int j = 0; j < listElem2.Count; ++j)
                {
                    if (attrName2 == "")
                        str2 = listElem2[j].GetString("");
                    else
                        str2 = listElem2[j].Attribute(attrName2).GetString("");

                    if (caseSensitive)
                    {
                        if (str1 == str2)
                        {
                            vecResults.Add(new KeyValuePair<Elmax.Element, Elmax.Element>(listElem1[i], listElem2[j]));
                            break;
                        }
                    }
                    else
                    {
                        if (str1.ToLower() == str2.ToLower())
                        {
                            vecResults.Add(new KeyValuePair<Elmax.Element, Elmax.Element>(listElem1[i], listElem2[j]));
                            break;
                        }
                    }
                }

            }

            return vecResults;
        }


        /// <summary>
        /// Join one element to many element
        /// </summary>
        /// <param name="listElem1">1st list of elements to join to</param>
        /// <param name="attrName1">Attribute name of the 1st list whose value to be matched. If empty, element value will be used to match instead.</param>
        /// <param name="listElem2">2nd list of elements to join to</param>
        /// <param name="attrName2">Attribute name of the 2nd list whose value to be matched. If empty, element value will be used to match instead.</param>
        /// <param name="caseSensitive">States if the comparision is case-sensitive.</param>
        /// <returns>List of pair of element and elements</returns>
        public static List<KeyValuePair<Elmax.Element, List<Elmax.Element>>>
            JoinOneToMany(
            List<Elmax.Element> listElem1,
            string attrName1,
            List<Elmax.Element> listElem2,
            string attrName2,
            bool caseSensitive)
        {
            string str1, str2;
            List<KeyValuePair<Elmax.Element, List<Elmax.Element>>> vecResults = new List<KeyValuePair<Elmax.Element, List<Elmax.Element>>>();
            bool makepair = false;
            for (int i = 0; i < listElem1.Count; ++i)
            {
                makepair = false;
                if (attrName1 == "")
                    str1 = listElem1[i].GetString("");
                else
                    str1 = listElem1[i].Attribute(attrName1).GetString("");

                for (int j = 0; j < listElem2.Count; ++j)
                {
                    if (attrName2 == "")
                        str2 = listElem2[j].GetString("");
                    else
                        str2 = listElem2[j].Attribute(attrName2).GetString("");

                    if (caseSensitive)
                    {
                        if (str1 == str2)
                        {
                            if (makepair == false)
                            {
                                List<Elmax.Element> vecChild = new List<Elmax.Element>();
                                vecResults.Add(new KeyValuePair<Elmax.Element, List<Elmax.Element>>(listElem1[i], vecChild));
                                makepair = true;
                            }
                            vecResults[vecResults.Count - 1].Value.Add(listElem2[j]);
                        }
                    }
                    else
                    {
                        if (str1.ToLower() == str2.ToLower())
                        {
                            if (makepair == false)
                            {
                                List<Elmax.Element> vecChild = new List<Elmax.Element>();
                                vecResults.Add(new KeyValuePair<Elmax.Element, List<Elmax.Element>>(listElem1[i], vecChild));
                                makepair = true;
                            }
                            vecResults[vecResults.Count - 1].Value.Add(listElem2[j]);
                        }
                    }
                }

            }

            return vecResults;
        }

        /// <summary>
        /// Join one element to another element, using a predicate
        /// </summary>
        /// <param name="listElem1">1st list of elements to join to</param>
        /// <param name="listElem2">2nd list of elements to join to</param>
        /// <param name="predicate">Determines if the elements are to be joined.</param>
        /// <returns>List of pair of element</returns>
        public static List<KeyValuePair<Elmax.Element, Elmax.Element>>
            JoinOneToOne(
                List<Elmax.Element> listElem1,
                List<Elmax.Element> listElem2,
                DoubleElementPredicate predicate)
        {
            List<KeyValuePair<Elmax.Element, Elmax.Element>> vecResults = new List<KeyValuePair<Elmax.Element, Elmax.Element>>();
            for (int i = 0; i < listElem1.Count; ++i)
            {
                for (int j = 0; j < listElem2.Count; ++j)
                {
                    if (predicate(listElem1[i], listElem2[j]))
                    {
                        vecResults.Add(new KeyValuePair<Elmax.Element, Elmax.Element>(listElem1[i], listElem2[j]));
                        break;
                    }
                }

            }

            return vecResults;
        }

        /// <summary>
        /// Join one element to many element, using a predicate
        /// </summary>
        /// <param name="listElem1">1st list of elements to join to</param>
        /// <param name="listElem2">2nd list of elements to join to</param>
        /// <param name="predicate">Determines if the elements are to be joined.</param>
        /// <returns>List of pair of element and elements</returns>
        public static List<KeyValuePair<Elmax.Element, List<Elmax.Element>>>
            JoinOneToMany(
            List<Elmax.Element> listElem1,
            List<Elmax.Element> listElem2,
            DoubleElementPredicate predicate)
        {
            List<KeyValuePair<Elmax.Element, List<Elmax.Element>>> vecResults = new List<KeyValuePair<Elmax.Element, List<Elmax.Element>>>();
            bool makepair = false;
            for (int i = 0; i < listElem1.Count; ++i)
            {
                makepair = false;

                for (int j = 0; j < listElem2.Count; ++j)
                {
                    if (predicate(listElem1[i], listElem2[j]))
                    {
                        if (makepair == false)
                        {
                            List<Elmax.Element> vecChild = new List<Elmax.Element>();
                            vecResults.Add(new KeyValuePair<Elmax.Element, List<Elmax.Element>>(listElem1[i], vecChild));
                            makepair = true;
                        }
                        vecResults[vecResults.Count - 1].Value.Add(listElem2[j]);
                    }
                }

            }

            return vecResults;
        }

    }
}

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

Shao Voon Wong
Software Developer (Senior)
Singapore Singapore
IT Certifications

  • IT Infrastructure Library Foundational (ITIL v3)
  • Scrum Alliance Certified Scrum Master (CSM)
  • EC-Council Certified Secure Programmer (ECSP) .NET
  • EC-Council Certified Ethical Hacker (CEH)
  • EC-Council Certified Security Analyst (ECSA)
  • Certified Secure Software Lifecycle Professional (CSSLP)

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 12 Apr 2016
Article Copyright 2016 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid