////////////////////////////////////////////////////////////////////////////
// Function: Create an XML DOM Document from Scratch in memory
////////////////////////////////////////////////////////////////////////////
HRESULT CXMLDocument::Build()
{
	// Create a root node
	MSXML::IXMLDOMNodePtr pRoot;
	m_pDoc->createNode(CComVariant(MSXML::NODE_ELEMENT), CComBSTR("Root"), NULL, &pRoot);

	// add child nodes
	HRESULT hr = S_OK;
	for (int i = 0; i < 10; i++)
	{
		MSXML::IXMLDOMNodePtr pNode;
		m_pDoc->createNode(CComVariant(MSXML::NODE_ELEMENT), CComBSTR("Order"), NULL, &pNode);

		MSXML::IXMLDOMElementPtr pElement = pNode;
		if (pElement != NULL)
			pElement->setAttribute(CComBSTR("id"), CComVariant(i));

		MSXML::IXMLDOMNodePtr p0 = NULL;
		CComVariant after;
		hr = pRoot->insertBefore(pNode, after, &p0);
		hr = CheckHR(hr, "inserting node");

		// The XML Document should now own the node.
		for (int j = 0; j < 10; j++)
		{
			MSXML::IXMLDOMNodePtr pNode2;
			m_pDoc->createNode(CComVariant(MSXML::NODE_ELEMENT), CComBSTR("Item"), NULL, &pNode2);

			MSXML::IXMLDOMElementPtr pElement2 = pNode2;
			if (pElement2 != NULL)
				pElement2->setAttribute(CComBSTR("id"), CComVariant((i*10) + j));
	
			MSXML::IXMLDOMNodePtr p1 = NULL;
			CComVariant after;
			hr = p0->insertBefore(pNode2, after, &p1);
			hr = CheckHR(hr, "inserting node");
			// The XML Document should now own the node.
		}
	}

	// Now attach this new subtree to the document.
	m_pDoc->appendChild(pRoot, NULL);

	return hr;
}