c# - Windows Phone 缺少根元素

标签 c# windows-phone-7 silverlight-4.0

我需要读取一个 xml 文件,但在尝试读取时出现此错误。

public void load()
{
  IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
  IsolatedStorageFileStream fileStream;
  try
  {
    fileStream = myIsolatedStorage.OpenFile("sites.xml", FileMode.Open, 
      FileAccess.Read);
  }
  catch (Exception)
  {
    fileStream = myIsolatedStorage.CreateFile("sites.xml");
    XDocument sites= new XDocument(new XElement("root"));
    fileStream = myIsolatedStorage.OpenFile("sites.xml", FileMode.Open, 
      FileAccess.Read);
  }          

  List<Site> listSites= new List<Site>();

  using (StreamReader file= new StreamReader(fileStream))
  {
    XDocument doc= XDocument.Load(file);

    foreach (XElement elemento in doc.Elements())
    {
      String name= elemento.Element("name").Value;
      String url = elemento.Element("url").Value;
      listSites.Add(new Site(name));
    }
    lstSites.ItemsSource = listSites;
  }
}

在线

XDocument doc= XDocument.Load(file); 

我收到主题中的错误。你能帮助我吗 ?

最佳答案

也许您应该改变使用 xml 文件和独立存储的方式。您可以创建一个包含您的名称和 url 属性的 Site 类,并将这些属性的列表序列化到隔离存储。

可以在这里找到一个例子: http://www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage---Read-and-Save-XML-files

关于c# - Windows Phone 缺少根元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8161499/

相关文章:

c# - Emgu CV 不检测 CUDA

c# - 使用 Thinktecture.IdentityModel lib 使用 UseBasicAuthentication 与 Webapi 和 OWIN 我的 Controller 中的身份没有声明

c# - Fluent断言: how to specify that collection should contain a certain number of elements matching predicate?

c# - Caliburn.Micro 的保护子句出错

c# - 插件在 Win 7 上的应用刷新页面时崩溃

c# - TwilioRestClient SendMessage 未解析

windows-phone-7 - 以编程方式切换飞行模式

silverlight - 如何在 XAML 中正确设置图像源 (Silverlight)

银光 4 : Free Split Button

windows-phone-7 - 在应用启动时处理来电崩溃