XPathEvaluate在其他环境中的行为有所不同 [英] XPathEvaluate behave differently in other environments

查看:176
本文介绍了XPathEvaluate在其他环境中的行为有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在统一开发游戏,并且想在统一脚本中解析XML文件, 我的文件以

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,

我的代码是:

var mapWidth = ((IEnumerable)tiledMapXmlRoot.XPathEvaluate("/@width")).Cast<XAttribute>().Select(a => Int32.Parse(a.Value)).First();

当我在Visual Studio中对其进行测试时,它可以正常工作,但是在统一脚本中运行时,它会失败, 调试时,我看到它失败了,因为 XPathEvaluate 返回类型为 System.Xml.XPath.SimpleSlashIterator

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

在Visual Studio中,返回的类型为 System.Xml.XPath.XPathEvaluator.EvaluateIterator ,它是一个具有预期元素的集合

while in visual studio the returned type is System.Xml.XPath.XPathEvaluator.EvaluateIterator and it is a collection with one element as expected

同一代码在两种环境中如何有可能不同地工作? 我查看了System.Xml.Linq的版本,并在Visual Studio中查看了它的4.0.0.0,而在unity(monodevelop脚本编辑器)中则为3.5.0.0,但查看了库文档,代码仍然可以正常工作

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应该不返回任何内容,而不管您的源文档是什么.它选择文档节点的宽度属性(在XPath 1.0中称为根节点"),而文档节点没有属性.任何返回此表达式结果的XPath处理器都是不合格的.正确的表达式是/*/@width,它返回文档中最外面的元素的width属性.

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

这篇关于XPathEvaluate在其他环境中的行为有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆