Click here to Skip to main content
15,861,172 members
Articles / Programming Languages / Visual C++ 10.0

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

Rate me:
Please Sign up or sign in to vote.
4.78/5 (14 votes)
12 Apr 2016Ms-PL11 min read 43.3K   500   29  
Linq-To-XML Node Creation for Native C++
using System;
using System.Xml;
using System.Text;
using System.IO;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Elmax;

namespace TestNetElmax
{
	[TestClass]
	public class UnitTestSetterGetter
	{
		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;
		}
		[TestMethod]
        public void NetSetGetDateTime()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				DateTime dd = new DateTime(2006, 7, 23, 9, 0, 23);
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetDateTime(dd);
				DateTime ddcheck = new DateTime();
				DateTime dd3 = elem["dd"].GetDateTime(ddcheck);

				Assert.AreEqual(dd.Year, dd3.Year);
				Assert.AreEqual(dd.Month, dd3.Month);
				Assert.AreEqual(dd.Day, dd3.Day);
				Assert.AreEqual(dd.Hour, dd3.Hour);
				Assert.AreEqual(dd.Minute, dd3.Minute);
				Assert.AreEqual(dd.Second, dd3.Second);
			}
		}
		[TestMethod]
        public void NetSetGetDate()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				DateTime dd = new DateTime(2010, 10, 1);
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetDate(dd);
				DateTime ddcheck = new DateTime();
				DateTime dd3 = elem["dd"].GetDate(ddcheck);

				Assert.AreEqual(dd.Year, dd3.Year);
				Assert.AreEqual(dd.Month, dd3.Month);
				Assert.AreEqual(dd.Day, dd3.Day);
			}
		}
		[TestMethod]
        public void NetSetGetGuid()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				Guid dd = Guid.NewGuid();
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetGuid(dd, false);
				Guid random1 = Guid.NewGuid();
				Guid dd3 = elem["dd"].GetGuid(random1);

                Assert.AreEqual(dd.ToString(), dd3.ToString());
			}
		}
		[TestMethod]
        public void NetSetGetGuidNoBraces()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				Guid dd = Guid.NewGuid();
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetGuid(dd, true);
				Guid random1 = Guid.NewGuid();
				Guid dd3 = elem["dd"].GetGuid(random1);

                Assert.AreEqual(dd.ToString(), dd3.ToString());
			}
		}
		[TestMethod]
        public void NetSetGetBoolean()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				bool dd = true;
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetBool(dd);

				bool dd2 = elem["dd"].GetBool(false);

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetSetGetLong()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				long dd = 14000000000L;
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetLong(dd);

				long dd2 = elem["dd"].GetLong(10);

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetSetGetInt()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				int dd = 2000000000;
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetInt(dd);

				int dd2 = elem["dd"].GetInt(10);

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetSetGetShort()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				short dd = 32000;
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetShort(dd);

				short dd2 = elem["dd"].GetShort(10);

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetSetGetSByte()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				sbyte dd = 127;
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetSByte(dd);

				sbyte dd2 = elem["dd"].GetSByte(10);

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetSetGetULong()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				ulong dd = 14000000000UL;
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetULong(dd);

				ulong dd2 = elem["dd"].GetULong(10);

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetSetGetUInt()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				uint dd = 4000000000;
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetUInt(dd);

				uint dd2 = elem["dd"].GetUInt(10);

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetSetGetUShort()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				ushort dd = 65000;
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetUShort(dd);

				ushort dd2 = elem["dd"].GetUShort(10);

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetSetGetByte()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				byte dd = 255;
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetByte(dd);

				byte dd2 = elem["dd"].GetByte(12);

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetSetGetString()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				System.String dd = "ABCD";
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetString(dd);

				System.String dd2 = elem["dd"].GetString("A");

				System.String s1 = dd;
				System.String s2 = dd2;

				Assert.AreEqual(s1, s2);
			}
		}
		[TestMethod]
        public void NetSetGetDouble()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				double dd = 123.0;
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetDouble(dd);

				double dd2 = elem["dd"].GetDouble(10.0);

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetSetGetFloat()
		{
			XmlDocument doc;
			bool created = CreateAndInitDom(out doc);
			if (created)
			{
				Element root = new Element();
				root.SetDomDoc(doc);

				Element elem = root["aa"]["bb"]["cc"].CreateNew(null);
				float dd = 123.0f;
				Assert.IsTrue(elem.Exists);
				elem["dd"].SetFloat(dd);

				float dd2 = elem["dd"].GetFloat(10.0f);

				Assert.AreEqual(dd, dd2);
			}
		}
	}
}

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)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions