如何在 XDocument.Load 中使用相对路径与 Xamarin?

克里斯托夫·切内尔

我总是得到系统未找到异常。如何在我的 xamarin.android 应用程序包中找到 xml 文件?

public Dictionary<string, string> TilePropertiesForTileID(short tileGid)
    {
        Dictionary<string, string> propertiesDict = null;

        try
        {

            // Loading from a file, you can also load from a stream
            var xml = XDocument.Load(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "Assets/Content/tileMap/wood_tileset.tsx"));

            // Query the data and write out a subset of contacts
            var propertiesQuery = from c in xml.Root.Descendants("tile")
                                  where (int)c.Attribute("id") == tileGid
                                  select c.Element("property").Value;


            foreach (string property in propertiesQuery)
            {
                Console.WriteLine("Property: {0}", property);
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw e;
        }

        return propertiesDict;
    }

这一行:

var xml = XDocument.Load(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "Assets/Content/tileMap/wood_tileset.tsx"));

总是抛出异常。我不知道如何访问此文件。

更新:感谢 apineda,我现在可以找到文件:这里是 xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.2" tiledversion="1.2.1" name="wood_tileset" 
    tilewidth="32" tileheight="32" tilecount="256" columns="16">
 <image source="wood_tileset.png" width="512" height="512"/>
 <tile id="68">
  <properties>
   <property name="IsTreasure" value="true"/>
  </properties>
 </tile>
</tileset>
松树

为此,您不一定需要文件的完整路径。由于您将文件放在 Assets 目录中,并且我希望也将其标记为AndroidAssetBuild Action 中,因此您只需要使用AssetManager.

如果您在活动中,您可以在资产中打开一个文件,如下所示:

Assets.Open("fileName.ext")

如果您在 Assets 目录中添加了子目录,则需要包含完整路径(不包含“Assets”一词)。

使用您的代码作为示例,它应该如下所示:

using (var sReader = new StreamReader(Assets.Open("Content/titleMap/wood_tileset.tsx")))
{
    var xmlDoc = XDocument.Load(sReader);

    // Query the data and write out a subset of contacts
    var propertiesQuery = xmlDoc.Root.Descendants("tile")
                                .Where(item => (int)item.Attribute("id") == tileGid)
                                .SelectMany(a => a.Descendants("property"))
                                .Select(property => new
                                { 
                                    Name = property.Attribute("name").Value, 
                                    Value = property.Attribute("value").Value
                                })
                                .ToList();

    foreach (var property in propertiesQuery)
    {
        Console.WriteLine($"Property Name: {property.Name}, Value: {property.Value}");
    }
}

如您所见,该Assets.Open方法的返回值被传递到 StreamReader 中。using不是必需的,但这将防止打开资源。

然后将 StreamReader 传递给 的Load()方法,XDocument其余部分与您所拥有的完全一样。

注意:这里假设您的文件结构如下:

Assets    
     Content
       titleMap
          wood_titleset.tsx

希望这可以帮助。-

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 SQL - ODBC 中使用相对路径?

如何在Python中使用相对路径

如何在CodeIgniter中使用相对路径?

如何让`zip` 在脚本中使用相对路径?

如何使用Rstudio相对路径

如何在 React 或 Next.js 中使用绝对路径而不是相对路径?

如何在JSON中使用Groovy Gpath获取相对路径?

如何在Play框架项目中使用相对路径访问资源文件?

如何在Taleo Connect客户端中使用相对路径?

如何在Scala中使用相对路径读取文本文件

如何在ReSharper DotSettings图层中使用相对路径

如何在环境变量或相对路径中使用替换指令

如何在Orchard和System.IO.File方法中使用(根)相对路径?

如何在CodeBuild / Ubuntu(TS2307)中使用tsc解析相对路径?

如何在Databricks中使用R Notebook中的相对路径?

我如何在Doxygen配置中使用相对路径名

如何在Angular Library中使用文件的相对路径?

如何在Node-Loader和本机插件中使用相对路径

如何在XML配置中的Spring集成中使用相对路径

如何在open()中使用相对路径在python中打开文件?

如何在Visual Studio中使用python设置相对路径?

Thunderbird扩展-如何在Javascript中使用相对路径?

如何在Windows CMD上使用相对路径?

如何使用相对路径在Webpack中使用SCSS(SASS)加载真棒字体?

如何使Excel在外部工作簿链接中使用相对路径

如何避免在Angular 2中使用相对路径较长的导入?

如何在使用MVC的现有ASP.NET Core应用中使用React Router设置相对路径

如何在CMake生成的解决方案中使用相对路径打开源代码中的文件

如何使用pathlib标准化相对路径