Click here to Skip to main content
13,629,942 members
Click here to Skip to main content

Stats

30.7K 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.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using Elmax;
using System.Diagnostics;

namespace TryoutNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool CreateAndInitDom(out XmlDocument doc)
        {
            doc = new XmlDocument();
            //if (doc != null)
            //{
            //    XmlProcessingInstruction pi = doc.CreateProcessingInstruction("xml", " version='1.0' encoding='UTF-8'");
            //    doc.AppendChild(pi);
            //}
            //else
            //    return false;
            return true;
        }
        private bool SaveXml(XmlDocument doc, System.String strFilename)
        {
            System.String szPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            szPath = Path.Combine(szPath, strFilename);
            doc.Save(szPath);
            FileInfo fi = new FileInfo(szPath);

            return fi.Exists;
        }
        private bool PrettySaveXml(XmlDocument doc, System.String strFilename)
        {
            System.String szPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            szPath = Path.Combine(szPath, strFilename);
            Document doc1 = new Document(doc);
            //doc.Save(szPath);
            doc1.PrettySave(szPath);
            FileInfo fi = new FileInfo(szPath);

            return fi.Exists;
        }
        private bool CreateAndLoadXml(out XmlDocument doc, System.String strFilename)
        {
            System.String szPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            szPath = Path.Combine(szPath, strFilename);

            doc = new XmlDocument();
            try
            {
                doc.Load(szPath);
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }
        private bool DeleteFile(System.String strFilename)
        {
            System.String szPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            szPath = Path.Combine(szPath, strFilename);

            File.Delete(szPath);
            FileInfo fi = new FileInfo(szPath);

            return fi.Exists == false;
        }
        private void TestWrite()
        {
            XmlDocument doc;
            bool b = CreateAndInitDom(out doc);
            if (b)
            {
                Element root = new Element();
                root.SetDomDoc(doc);

                Element all = root["All"];
                all["Version"].SetInt(1);
                Element books = all["Books"].CreateNew(null);
                Element book1 = books["Book"].CreateNew(null);
                book1.Attribute("ISBN").SetString("1111-1111-1111");
                book1["Title"].SetString("Pebbles in the Sky");
                book1["Price"].SetFloat(12.99f);
                book1["AuthorID"].SetInt(111);

                Element book2 = books["Book"].CreateNew(null);
                book2.Attribute("ISBN").SetString("2222-2222-2222");
                book2["Title"].SetString("Currents of Space");
                book2["Price"].SetFloat(10.00f);
                book2["AuthorID"].SetInt(111);

                Element book3 = books["Book"].CreateNew(null);
                book3.Attribute("ISBN").SetString("3333-3333-3333");
                book3["Title"].SetString("Foundation");
                book3["Price"].SetFloat(10.00f);
                book3["AuthorID"].SetInt(111);

                Element book4 = books["Book"].CreateNew(null);
                book4.Attribute("ISBN").SetString("4444-4444-4444");
                book4["Title"].SetString("2001: A Space Odyssey");
                book4["Price"].SetFloat(10.00f);
                book4["AuthorID"].SetInt(222);

                Element book5 = books["Book"].CreateNew(null);
                book5.Attribute("ISBN").SetString("5555-5555-5555");
                book5["Title"].SetString("Rendezvous with Rama");
                book5["Price"].SetFloat(10.00f);
                book5["AuthorID"].SetInt(222);
                
                Element authors = all["Authors"].CreateNew(null);
                Element author = authors["Author"].CreateNew(null);
                author.Attribute("Name").SetString("Isaac Asimov");
                author.Attribute("AuthorID").SetInt(111);
                author["Bio"].SetString("Sci-Fic author!");

                Element author2 = authors["Author"].CreateNew(null);
                author2.Attribute("Name").SetString("Arthur C. Clark");
                author2.Attribute("AuthorID").SetInt(222);
                author2["Bio"].SetString("Sci-Fic author!");

                string strFilename = "Books.xml";
                PrettySaveXml(doc, strFilename);
            }
        }
        private void TestRead()
        {
            XmlDocument doc;
            string strFilename = "Books.xml";
            bool b = CreateAndLoadXml(out doc, strFilename);
            if (b)
            {
                Element root = new Element();
                root.SetDomDoc(doc);

                Element all = root["All"];
                if (all.Exists == false)
                {
                    Debug.Print("Error: root does not exists!");
                    return;
                }
                Debug.Print("Version : {0}\n", all["Version"].GetInt(0));

                Debug.Print("Books");
                Debug.Print("=====");
                Element books = all["Books"];
                if (books.Exists)
                {
                    List<Element> listBooks = books.GetCollection("Book");
                    for (int i = 0; i < listBooks.Count; ++i)
                    {
                        Debug.Print("ISBN: {0}", listBooks[i].Attribute("ISBN").GetString("Error: None"));
                        Debug.Print("Title: {0}", listBooks[i]["Title"].GetString("Error: No title!"));
                        Debug.Print("Price: {0}", listBooks[i]["Price"].GetFloat(0.0f));
                        Debug.Print("Desc: {0}", listBooks[i]["Desc"].GetString("Error: None"));
                        Debug.Print("AuthorID: {0}\n", listBooks[i]["AuthorID"].GetInt(-1));
                    }
                }

                Debug.Print("Authors");
                Debug.Print("=======");
                Element authors = all["Authors"];
                if (authors.Exists)
                {
                    List<Element> listAuthors = authors.GetCollection("Author");
                    for (int i = 0; i < listAuthors.Count; ++i)
                    {
                        Debug.Print("Name: {0}", listAuthors[i].Attribute("Name").GetString("Error: None"));
                        Debug.Print("AuthorID: {0}", listAuthors[i].Attribute("AuthorID").GetInt(-1));
                        Debug.Print("Bio: {0}\n", listAuthors[i]["Bio"].GetString("Error: No bio!"));
                    }
                }

                var vec = HyperElement.JoinOneToMany(authors.GetCollection("Author"), books.GetCollection("Book"), 
			        (x, y)=> x.Attribute("AuthorID").GetString("a") == y["AuthorID"].GetString("a"));

		        for(int i=0; i< vec.Count; ++i)
		        {
			        Debug.Print("List of books by {0}", vec[i].Key.Attribute("Name").GetString(""));
			        Debug.Print("=============================================");
			        for(int j=0; j< vec[i].Value.Count; ++j)
			        {
				        Debug.Print("{0}", vec[i].Value[j]["Title"].GetString("None"));
			        }
			        Debug.Print("");
		        }

            }
            //DeleteFile(strFilename);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            TestWrite();
            TestRead();
        }
    }
}

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