Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.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# 如何为xml文件指定正确的位置?_C#_Cross Platform - Fatal编程技术网

C# 如何为xml文件指定正确的位置?

C# 如何为xml文件指定正确的位置?,c#,cross-platform,C#,Cross Platform,如何为xml文件指定正确的位置 XDocument xDoc = XDocument.Load("Data.xml"); 我在xamarin live中启动该应用程序-一切正常 我收集申请表并签字。我在手机上安装应用程序-当我使用此代码进入页面时,应用程序崩溃 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; u

如何为xml文件指定正确的位置

XDocument xDoc = XDocument.Load("Data.xml");
我在xamarin live中启动该应用程序-一切正常

我收集申请表并签字。我在手机上安装应用程序-当我使用此代码进入页面时,应用程序崩溃

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using System.Xml.Linq;
using Xamarin.Forms.Xaml;

namespace App14
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ListMain : ContentPage
    {


        public ListMain ()
        {

            InitializeComponent ();
            XDocument xDoc = XDocument.Load("Data.xml");


        }


    }
}
这是我的解决方案的概要

Data.xml:build action=嵌入式资源

谢谢大家的帮助。 我根据您的建议更改了代码,以了解是否正在创建文件。 我这样做了,得到了“否”-代码说文件没有被创建。你知道为什么吗

  string fullPath = Path.Combine(
             System.Environment.GetFolderPath(System.Environment.SpecialFolder.Resources), "Data.xml");
            if (File.Exists(fullPath))
            {
                stackLayout.Children.Add(new Label { Text = "YES", FontSize = 20, HorizontalOptions = LayoutOptions.End });
            }
            else
            {
                stackLayout.Children.Add(new Label { Text = "NO", FontSize = 20, HorizontalOptions = LayoutOptions.End });
            }

如何将文件移动到
资产
文件夹下并使用:

XDocument xDoc = XDocument.Load (Assets.Open ("Data.xml"));

我猜是DirectoryNotFoundException,您可以尝试使用System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),即使我建议使用assembly.GetManifestResourceStream(“Data.xml”);你能帮我正确地编写以下代码吗?我试了很多
string fullPath=Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),Data);XDocument xDoc=XDocument.Load(完整路径)
和so
string fullPath=Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Resources),Data);XDocument xDoc=XDocument.Load(完整路径)Visual Studio,在红色的
日期“
下面加下划线,谢谢。我这么做了,结果被拒绝了<代码>字符串fullPath=Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Resources),“Data.xml”);如果(File.Exists(fullPath)){stackLayout.Children.Add(新标签{Text=“YES”,FontSize=20,horizontalpoptions=LayoutOptions.End});}其他{stackLayout.Children.Add(新标签{Text=“NO”,FontSize=20,horizontalpoptions=LayoutOptions.End});}