XPathEvaluate behave differently in other environments

流过昼夜 提交于 2020-01-05 09:06:24

问题


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


回答1:


/@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.



来源:https://stackoverflow.com/questions/24978146/xpathevaluate-behave-differently-in-other-environments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!