【问题标题】:Can't find a path to my xml file找不到我的 xml 文件的路径
【发布时间】:2019-01-21 19:32:18
【问题描述】:

我正在尝试查找名为 ClassData.xml 的 XML 文件,但找不到。该文件本身隐藏在名为 Data 的文件夹中。我不知道如何进入文件所在的项目目录。我用的是ios模拟器。

public override void ViewDidLoad ()     
{       
    base.ViewDidLoad ();

    this.lbl_Werkt.Text = "het werkt wtf";

    XmlDocument Doc = new XmlDocument();

    try
    {                
        Doc.Load("/Data/ClassData.xml");
    }
    catch (Exception error)
    {
        Console.WriteLine("The File could not be read:");
        Console.WriteLine(error.Message);
    }
    finally
    {
        foreach (XmlNode node in Doc.SelectNodes("//Warrior"))
        {
            string Name = node["Name"].InnerText;

            lbl_Name.Text = Name;
        }
    }
}

【问题讨论】:

  • 如果您的项目,Data 文件夹是其中的一个文件夹吗?你在使用 Visual Studio 吗?
  • 数据文件夹在项目内,我正在使用 Visual Studio

标签: c# ios xml path


【解决方案1】:

对于项目中的文件,试试这个:

public override void ViewDidLoad ()     
{       
   base.ViewDidLoad ();

   this.lbl_Werkt.Text = "het werkt wtf";

   try{

      XmlDocument Doc = new XmlDocument();
      string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Data\ClassData.xml");

       if(!file.Exists(path))
          throw new IOExpection("File not found");

       Doc.Load(path);

       foreach (XmlNode node in Doc.SelectNodes("//Warrior"))
       {
          string Name = node["Name"].InnerText;

          lbl_Name.Text = Name;
       }
    }
    catch(IOException ioEx)
    {
       Console.WriteLine("The File could not be read:");
       Console.WriteLine(ioEx.Message);
    }
    catch(Exception ex)
    {
       Console.WriteLine(ex.Message);
    }
}  

【讨论】:

  • 我就是不明白怎么找不到文件夹
【解决方案2】:

我真的很笨!如果其他人遇到此问题,请查看您的文件(在我的情况下为 xml)并将构建操作从 Embedded 更改为 Content。如果你这样做,你可以这样做:

Doc.Load(@"\Data\ClassData.xml");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 2013-08-10
    • 2022-06-13
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 2016-11-25
    相关资源
    最近更新 更多