简体   繁体   English

可以从隔离存储中读取xml文件

[英]can read xml file from isolated storage

It's a Silverlight WindowsPhone Project and I'm trying to create a xml file in the isolatedstorage then I try to read from it, here is the code: 这是一个Silverlight WindowsPhone项目,我试图在isolatedstorage中创建一个xml文件,然后尝试从中读取,这是代码:

using (var file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var stream = file.OpenFile("MainLBItems.xml", FileMode.Create))
                    {
                        XDocument MainLBItems = new XDocument(
                            new XDeclaration("1.0", "utf-8", "yes"),
                            new XComment("This is a comment"),
                            new XElement("Items")
                        );                        
                        MainLBItems.Save(stream);                        
                    }
                }

The problem is that when I try to read from it and here is the code 问题是,当我尝试从中读取内容时,这是代码

using (var file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = file.OpenFile("MainLBItems.xml", FileMode.Open))
                {
                    XDocument MainLBItems = XDocument.Load(stream);
                    ...
                }
            }

I have an error telling "Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 3, position 12." 我遇到一个错误,告诉“意外的XML声明。XML声明必须是文档中的第一个节点,并且不允许在其之前出现空格字符。第3行,位置12。” and throwing an unhandled XmlException 并抛出未处理的XmlException

Could you please help me resolve this problem? 您能帮我解决这个问题吗? Thanks in advance. 提前致谢。

I tried to add the XML Declaration separately but it didn't work too: 我试图单独添加XML声明,但是它也没有起作用:

using (var file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var stream = file.OpenFile("MainLBItems.xml", FileMode.Create))
                    {
                        XDocument MainLBItems = new XDocument();
                        MainLBItems.Declaration=  new XDeclaration("1.0", "utf-8", "yes");
                        MainLBItems.Add(
                            new XComment("This is a comment"),
                            new XElement("Items")
                        );                        
                        MainLBItems.Save(stream);                        
                    }
                }

As you haven't posted the XML, I can only take a guess... I have usually struck this problem when I don't have a root node in the document: 因为您还没有发布XML,所以我只能猜测一下...当我在文档中没有根节点时,我通常会遇到这个问题:

<xml version="1.0" encoding="utf-8" xmlns:myNamespace="...">
<MyNodes>
    .... etc ....
</MyNodes>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM