1

当我尝试反序列化流时发生错误。

public static AllFlags Load()
{
    IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
    AllFlags allFlags;

    //IsolatedStorageFileStream stream = storage.OpenFile(filename, FileMode.Open);
    StringReader stream = new StringReader(filename);
    XmlSerializer xml = new XmlSerializer(typeof(AllFlags));
    allFlags = xml.Deserialize(stream) as AllFlags;// here There is an error in XML
    stream.Close();
    stream.Dispose();

    return allFlags;
}

如果我从隔离存储打开文件没问题

public static AllFlags Load()
{
    IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
    AllFlags allFlags;

    IsolatedStorageFileStream stream = storage.OpenFile(filename, FileMode.Open);
    XmlSerializer xml = new XmlSerializer(typeof(AllFlags));
    allFlags = xml.Deserialize(stream) as AllFlags;
    stream.Close();
    stream.Dispose();

    return allFlags;
}

我怎样才能打开这个文件?

编辑:

我的 xml 文件“Flags.xml”:

    <?xml version="1.0"?>
    <AllFlags>
      <Flags>
        <Flag>
          <Tag>Tag1</Tag>
          <Name>Name1</Name>
        </Flag>

...

      </Flags>
    </AllFlags> 
4

1 回答 1

0

AStringReader不是从文件中读取,而是从字符串中读取。您正在将任何内容解析filename为 XML(不是 XML,而是路径)。

您可能想使用 aStreamReader代替:

StreamReader stream = new StreamReader(filename);
于 2013-08-07T10:26:01.567 回答