Root Caveat

A element is designated as a root element when SetDomDoc is called on the element. However, it is to be noted that at this point there is no tag name associated with the root element at this point. A tag name will only be set when the [] operator is called. For subsequent access, we need not specify the root tag name again, as shown in the code snippet below. At first, root["aa"]["bb"] is used to access <aa><bb>11</bb></aa>. Subsequent same access, only tag name "bb" need to be stated because root has stored "aa" as its tag name.

// root does not have any tag name associated with it.
Element root = new Element();

// Accessing <aa><bb>11</bb></aa> for the 1st time
// root has tag name "aa" associated with it after this call.
int x = root["aa"]["bb"].GetInt(0);

// Accessing <aa><bb>11</bb></aa> again
// Since root is associated with tag name "aa",
// we need not reference "aa" again to get "bb".
x = root["bb"].GetInt(0); // correct

// Wrong example of accessing <aa><bb>11</bb></aa> again
// Actually accessing <aa><aa><bb>11</bb></aa></aa>
x = root["aa"]["bb"].GetInt(0); // wrong!

If the user does not know the root name, he/she can call Element.GetRootName method to retrieve its value.

Element root = new Element();

int x = root[root.GetRootName()]["bb"].GetInt(0);

Last edited Sep 8, 2011 at 2:29 AM by shaovoon, version 6


No comments yet.