- Home /
XMLDocument GetElementByID returning null
Hi guys. I'm trying to get an element of a xml by using the function GetElementById, but the function is always returning null.
//get xml text from a web service
string xml = aS.createTree();
XmlDocument tree = new XmlDocument();
tree.LoadXml(xml);
//get all nodes with the tag name "item"
XmlNodeList node = tree.GetElementsByTagName("item");
//just for test to see if i could get the attribute value which returns the expected
string idTemp = node[0].Attributes["ID"].Value;
XmlElement elem = tree.GetElementById("1");
elem is always returning null. Can you guys help me out?
By the way this is the xml that i'm trying to parse:
<?xml version="1.0" encoding="utf-8"?>
<tree>
<item id="1">
<item id="2"></item>
</item>
<item id="5">
<item id="6"></item>
<item id="7">
<item id="8">
<item id="10">
<item id="11"></item>
</item>
</item>
<item id="9"></item>
</item>
</item>
</tree>
Comment
Probably better to ask over at StackOverflow. Your question is not really Unity related.
Best Answer
Answer by Caixotim · Jul 16, 2012 at 02:49 PM
Thanks Graham, already solved it there. This was the answer, if anybody has the same problem:
I added a doctype to refer my attribute "id" as an ID and the value of the id cannot start with a number, like the following example.
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE tree [
<!ELEMENT tree ANY>
<!ELEMENT item ANY>
<!ATTLIST item id ID #REQUIRED>
]>
<tree>
<item id="id_1">
<item id="id_2"></item>
</item>
<item id="id_5">
<item id="id_6"></item>
<item id="id_7">
<item id="id_8">
<item id="id_10">
<item id="id_11"></item>
</item>
</item>
<item id="id_9"></item>
</item>
</item>
</tree>