0

我正在像这样加载我的文档:

WebClient client = new WebClient();
        client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
        client.OpenReadAsync(new Uri("Rolls.xml", UriKind.Relative));

然后在阅读完成时:

 XDocument doc = XDocument.Load(XmlReader.Create(e.Result));

       using (Stream stream = e.Result)
       {
           {
               foreach (var roll in _rollsToAddStudentTo)
               {
                   doc.Element("rolls").Add(new XElement("rollid", roll));
               }
               doc.Save(stream);
           }

       }

问题是当它到达保存时我得到了错误

“不支持指定的方法。”

帮助将不胜感激。干杯

谢谢杰霍夫,

那么,如何将我的文档合并到该异步方法中?

foreach (var roll in _rollsToAddStudentTo)
               {
                   doc.Element("rolls").Add(new XElement("rollid", roll));
               }

               WebClient client = new WebClient();
               client.OpenWriteCompleted += new OpenWriteCompletedEventHandler(client_OpenWriteCompleted);
               client.OpenWriteAsync(new Uri("Rolls.xml", UriKind.Relative));

我已经通过将我的逻辑更改为以下来解决这个问题。

 using (IsolatedStorageFile isoStore =
           IsolatedStorageFile.GetUserStoreForApplication())
            {
                // Create new file
                using (IsolatedStorageFileStream isoStream =
                    new IsolatedStorageFileStream("Rolls.xml",
                        FileMode.Create, isoStore))
                {
                    // Write to the Isolated Storage for the user.
                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Indent = true;
                    // Create an XmlWriter.
                    using (XmlWriter writer = XmlWriter.Create(isoStream, settings))
                    {
                        writer.WriteStartDocument();
                        writer.WriteStartElement("Rolls");

                        foreach (var roll in _rollsToAddStudentTo)
                        {
                            writer.WriteStartElement("roll");
                            writer.WriteAttributeString("rollid", roll);
                            writer.WriteEndElement();
                        }

                        writer.WriteEndElement();
                        writer.WriteEndDocument();
                    }
                }
            }
4

1 回答 1

0

您尝试保存 XDocument 的流是readonly. 因为它是您作为参数传递给client_OpenReadCompleted注册到 event的方法的流OpenReadCompleted

如果您想通过 WebClient 将 XDocument 保存回来,您需要调用OpenWriteAsync方法之一。

于 2013-06-04T06:37:06.540 回答