Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

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

, 7 Jun 2012 Ms-PL
Linq-To-XML Node Creation for Native C++
elmax-84498.zip
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
Settings.settings
Tryout
res
Tryout.ico
Tryout.vcxproj.user
elmax-ver083.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver084.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver085.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver086.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver087.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
Elmax-ver089.zip
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
Settings.settings
ElmaxSrcVer082.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
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 UnitTestVoidGetter
	{
		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 NetGetVoidDateTime()
		{
			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);
				DateTime dd3 = elem["dd"].GetDateTime(dd);

				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 NetGetVoidDate()
		{
			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);
				DateTime dd3 = elem["dd"].GetDate(dd);

				Assert.AreEqual(dd.Year, dd3.Year);
				Assert.AreEqual(dd.Month, dd3.Month);
				Assert.AreEqual(dd.Day, dd3.Day);
			}
		}
		[TestMethod]
        public void NetGetVoidGuid()
		{
			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);
				Guid dd3 = elem["dd"].GetGuid(dd);

                Assert.AreEqual(dd.ToString(), dd3.ToString());
			}
		}
		[TestMethod]
        public void NetGetVoidBoolean()
		{
			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 = false;
				Assert.IsTrue(elem.Exists);

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

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetGetVoidLong()
		{
			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);

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

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetGetVoidInt()
		{
			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);

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

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetGetVoidShort()
		{
			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);

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

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetGetVoidSByte()
		{
			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);

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

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetGetVoidULong()
		{
			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);

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

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetGetVoidUInt()
		{
			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);

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

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetGetVoidUShort()
		{
			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);

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

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetGetVoidByte()
		{
			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);

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

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetGetVoidString()
		{
			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);

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

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

				Assert.AreEqual(s1, s2);
			}
		}
		[TestMethod]
        public void NetGetVoidDouble()
		{
			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);

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

				Assert.AreEqual(dd, dd2);
			}
		}
		[TestMethod]
        public void NetGetVoidFloat()
		{
			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);

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

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

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

                uint ans = elem["dd"].ReadHex(0xee);

                Assert.AreEqual((uint)(0xee), ans);
            }
        }
        [TestMethod]
        public void NetGetEmptyDocSelectSingleNode()
        {
            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 = 100;
                Assert.IsTrue(elem.Exists);
                elem.SetInt(dd);

                Element elem2 = root["aa/bb/dd"].CreateNew(null);
                dd = 120;
                Assert.IsTrue(elem2.Exists);
                elem2.SetInt(dd);

                Document elmaxDoc = new Document(doc);
                Element singleNode = elmaxDoc.SelectSingleNode("//ee");

                Assert.AreEqual(false, singleNode.Exists);
            }
        }
        [TestMethod]
        public void NetGetEmptyDocSelectNodes()
        {
            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 = 100;
                Assert.IsTrue(elem.Exists);
                elem["dd"].SetInt(dd);

                Element elem2 = root["aa/bb/cc"].CreateNew(null);
                dd = 120;
                Assert.IsTrue(elem2.Exists);
                elem2["dd"].SetInt(dd);

                Element elem3 = root["aa/bb/cc"].CreateNew(null);
                dd = 50;
                Assert.IsTrue(elem3.Exists);
                elem3["dd"].SetInt(dd);

                Document elmaxDoc = new Document(doc);
                List<Element> nodes = elmaxDoc.SelectNodes("//cc[dd>200]");

                Assert.AreEqual(0, nodes.Count);
            }
        }
        [TestMethod]
        public void NetTestSelectSingleNode()
        {
            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 = 100;
                Assert.IsTrue(elem.Exists);
                elem.SetInt(dd);

                Element elem2 = root["aa/bb/dd"].CreateNew(null);
                dd = 120;
                Assert.IsTrue(elem2.Exists);
                elem2.SetInt(dd);

                Element singleNode = root.SelectSingleNode("//ee");

                Assert.AreEqual(false, singleNode.Exists);
            }
        }
        [TestMethod]
        public void NetTestSelectNodes()
        {
            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 = 100;
                Assert.IsTrue(elem.Exists);
                elem["dd"].SetInt(dd);

                Element elem2 = root["aa/bb/cc"].CreateNew(null);
                dd = 120;
                Assert.IsTrue(elem2.Exists);
                elem2["dd"].SetInt(dd);

                Element elem3 = root["aa/bb/cc"].CreateNew(null);
                dd = 50;
                Assert.IsTrue(elem3.Exists);
                elem3["dd"].SetInt(dd);

                List<Element> nodes = root.SelectNodes("//cc[dd>200]");

                Assert.AreEqual(0, nodes.Count);
            }
        }
        [TestMethod]
        public void NetGetEmptyDocGetElementsByTagName()
        {
            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 = 100;
                Assert.IsTrue(elem.Exists);
                elem["dd"].SetInt(dd);

                Element elem2 = root["aa/bb/cc"].CreateNew(null);
                dd = 120;
                Assert.IsTrue(elem2.Exists);
                elem2["dd"].SetInt(dd);

                Element elem3 = root["aa/bb/cc"].CreateNew(null);
                dd = 50;
                Assert.IsTrue(elem3.Exists);
                elem3["dd"].SetInt(dd);

                Document elmaxDoc = new Document(doc);
                List<Element> nodes = elmaxDoc.GetElementsByTagName("ee");

                Assert.AreEqual(0, nodes.Count);
            }
        }
        [TestMethod]
        public void NetGetEmptyDocGetElementsByTagNameWithNS()
        {
            XmlDocument doc;
            bool created = CreateAndInitDom(out doc);
            if (created)
            {
                Element root = new Element();
                root.SetDomDoc(doc);

                Element elem = root["aa/bb/cc"].CreateNew("http://www.yahoo.com");
                int dd = 100;
                Assert.IsTrue(elem.Exists);
                elem["dd"].CreateNew("http://www.yahoo.com").SetInt(dd);

                Element elem2 = root["aa/bb/cc"].CreateNew("http://www.yahoo.com");
                dd = 120;
                Assert.IsTrue(elem2.Exists);
                elem2["dd"].CreateNew("http://www.yahoo.com").SetInt(dd);

                Element elem3 = root["aa/bb/cc"].CreateNew("http://www.microsoft.com");
                dd = 50;
                Assert.IsTrue(elem3.Exists);
                elem3["dd"].CreateNew("http://www.microsoft.com").SetInt(dd);

                Document elmaxDoc = new Document(doc);
                List<Element> nodes = elmaxDoc.GetElementsByTagName("ee", "http://www.yahoo.com");

                Assert.AreEqual(0, nodes.Count);
            }
        }
        [TestMethod]
        public void NetGetEmptyDocGetElementById()
        {
            string dtd1 = "<!DOCTYPE root [<!ELEMENT root ANY><!ELEMENT Person ANY>";
            string dtd2 = "<!ELEMENT Customer EMPTY><!ATTLIST Person SSN ID #REQUIRED>";
            string dtd3 = "<!ATTLIST Customer id IDREF #REQUIRED >]>";
            string xml1 = "<root><Person SSN='A111' Name='Fred'/><Person SSN='A222' Name='Tom'/>";
            string xml2 = "<Customer id='A111'/><Customer id='A222334444'/></root>";

            string xml = dtd1 + dtd2 + dtd3 + xml1 + xml2;

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            Document elmaxDoc = new Document(doc);

            Element elem = elmaxDoc.GetElementById("A333");

            Assert.AreEqual(false, elem.Exists);
        }
	}
}

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
Singapore Singapore
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 7 Jun 2012
Article Copyright 2011 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid