Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Xamarin项目的项目目录中读取.xml文件_C#_Xamarin - Fatal编程技术网

C# 从Xamarin项目的项目目录中读取.xml文件

C# 从Xamarin项目的项目目录中读取.xml文件,c#,xamarin,C#,Xamarin,我很难从Xamarin项目中读取.xml文件。每当我调试代码时,我都会发现下面的错误 找不到路径“/storage/emulated/0/Xml/SupportedTypes.Xml”的一部分 下面是我正在使用的代码 public static void Initialize(ConnectedDeviceCommType type) { string fileName = @"Xml\SupportedTypes.xml";

我很难从Xamarin项目中读取.xml文件。每当我调试代码时,我都会发现下面的错误

找不到路径“/storage/emulated/0/Xml/SupportedTypes.Xml”的一部分

下面是我正在使用的代码

        public static void Initialize(ConnectedDeviceCommType type)
        {
            string fileName = @"Xml\SupportedTypes.xml";
            string filePath = Path.Combine(OS.Environment.ExternalStorageDirectory.ToString(), fileName);

            try
            {
                var xml = XDocument.Load(filePath);
            }
            catch(Exception ex)
            {
                string st = ex.Message;
            }
        }
我在一些博客中读到,他们说.xml文件需要添加到资产文件夹中,但在我的情况下,我的项目中没有任何资产文件夹,我尝试加载的项目也不是活动


那么,您能告诉我有什么方法可以直接在XAMARIN中读取XML文件吗?

我真的不明白您为什么要直接保存XML文件。因为,如果您发布应用程序,您可能不会设置与开发相同的目录结构。我建议将文件与应用程序放在同一文件夹中。下面的代码可以帮助您从目录访问xml文件

string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "SupportedTypes.xml");

多亏了迪米特里斯,我终于成功了。

检查此项。。因此,如果您的资源位于“Resources/Xml/”中,并且您的默认项目命名空间为“SampleApp.Sample”,则资源名称为:“SampleApp.Sample.Resources.Xml.MyXml.Xml”。我尝试了您所要求的内容,但没有成功,仍然引发异常。我已在调试期间检查了该值,它显示的值为“/storage/emulated/0/[Project namespace]\Xml\SupportedTypes.Xml”检查删除bin obj并重新启动vsj是否将Xml文件生成操作设置为EmbeddedResource@VinodMG将XML嵌入程序集中(
EmbeddedResource
)并将其作为嵌入式资源阅读(我个人不建议使用嵌入式资源),或将XML文件添加到您的Android项目的资产文件夹(并使用Xamarin.Essentials进行阅读)嗨,Devlst,我也尝试了您建议的方式,但没有成功,它引发了异常