I am working on a game in unity and want to parse an XML file in a unity script, my file starts with the line
<map version="1.0" orientation="orthogonal" width="16" height="12" tilewidth="64" tileheight="64">
and I want to get the map width attribute,
my code is :
var mapWidth = ((IEnumerable)tiledMapXmlRoot.XPathEvaluate("/@width")).Cast<XAttribute>().Select(a => Int32.Parse(a.Value)).First();
which is working right when I test it in visual studio, but fail when it runs in a unity script, while debugging I can see it fails because XPathEvaluate returns an empty collection of type System.Xml.XPath.SimpleSlashIterator
while in visual studio the returned type is System.Xml.XPath.XPathEvaluator.EvaluateIterator and it is a collection with one element as expected
how is it possible for the same code to work differently in two environments ? I looked at the version of System.Xml.Linq and in visual studio its 4.0.0.0 while in unity (monodevelop script editor) it is 3.5.0.0 but looking at the library documentation the code should still work
/@width
should return nothing, regardless of your source document. It selects the width attribute of the document node (called the "root node" in XPath 1.0), and document nodes do not have attributes. Any XPath processor that returns a result for this expression is non-conformant. The correct expression is/*/@width
, which returns the width attribute of the outermost element in the document.