2014-10-16 65 views
4

我遇到了XmlWriter中的Create方法問題。使用XmlWriter創建xml文件

即使當我使用msdn的例子,它將無法正常工作。 http://msdn.microsoft.com/en-us/library/kcsse48t.aspx

我爲Windows應用商店應用程序創建了一個「空白頁面」項目。 其中,ive插入了來自msdn的示例代碼,以測試XmlWriter類的工作方式。

VS給我的錯誤:

Cannot resolve method 'Create(string)', candidates are: 
System.Xml.XmlWriter Create(System.IO.Stream) (in class XmlWriter) 
System.Xml.XmlWriter Create(System.IO.TextWriter) (in class XmlWriter) 
System.Xml.XmlWriter Create(System.Text.StringBuilder) (in class XmlWriter) 
System.Xml.XmlWriter Create(System.Xml.XmlWriter) (in class XmlWriter) 

這個工程沒有在控制檯應用程序的問題。但我需要製作一個Windows應用商店。

我希望最終做的是添加到現有的xml文件。

有人能告訴我爲什麼這不起作用,或者如何以不同的方式達到我的目標。

謝謝你的時間。

編輯:

對不起,我的代碼:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using System.Xml; 
using System.Xml.Linq; 
using Windows.Data.Xml.Dom; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 

namespace DatabaseTest 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class MainPage : Page 
    { 
     public MainPage() 
     { 
      this.InitializeComponent(); 
     } 


     private void button_submit_Click(object sender, RoutedEventArgs e) 
     { 
      using (XmlWriter writer = XmlWriter.Create("output.xml")) 
      { 
       writer.WriteStartElement("book"); 
       writer.WriteElementString("price", "19.95"); 
       writer.WriteEndElement(); 
       writer.Flush(); 
      } 
     } 
    } 
} 
+2

顯示您的代碼。 – 2014-10-16 11:36:32

+2

和完整的錯誤信息 – Vladimirs 2014-10-16 11:38:03

+0

對不起,添加了我的代碼。 – 2014-10-16 11:41:14

回答

1

這是一個Windows應用商店應用,不是嗎?編輯你的問題標籤,並試試這個:

StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("output.xml", CreationCollisionOption.ReplaceExisting); 
using (IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite)) 
{ 
    System.IO.Stream s = writeStream.AsStreamForWrite(); 
    System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings(); 
    settings.Async = true; 
    using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(s, settings)) 
    { 
    // Do stuff... 

    await writer.FlushAsync(); 
    } 
} 

一旦你知道它的作品,你可以刪除命名空間限定符。

檢查this瞭解如何在Windows應用商店應用中存儲文件。還可以看看this的樣品。我的代碼將使用本地app文件夾,即是這樣的:C:\Users\[name]\AppData\Local\Packages\[appName]\LocalState\

+0

是的。這是一個Windows應用商店。我只用了一個月左右的C#,所以我不知道它有所作爲。此外,您的解決方案似乎工作。但是,沒有文件顯示。 在'code' //做東西'代碼'區域,我把'code'writer.WriteStartElement'code'填入msdn示例中 – 2014-10-16 12:07:55

+0

這個文件將被保存在你的本地存儲文件夾中,例如: 「C:\ Users \\ [name] \ AppData \ Local \ Packages \\ [appName] \ LocalState \」 – 2014-10-16 12:25:20

+0

我已更新我的答案,在Windows應用商店應用中包含有關文件存儲的鏈接。 – 2014-10-16 12:28:16

0

這可能是有幫助的,

using (XmlWriter writer = XmlWriter.Create("employees.xml")) 
    { 
     writer.WriteStartDocument(); 
     writer.WriteStartElement("Employees"); 

     foreach (Employee employee in employees) 
     { 
     writer.WriteStartElement("Employee"); 

     writer.WriteElementString("ID", employee.Id.ToString()); // <-- These are new 
     writer.WriteElementString("FirstName", employee.FirstName); 
     writer.WriteElementString("LastName", employee.LastName); 
     writer.WriteElementString("Salary", employee.Salary.ToString()); 

     writer.WriteEndElement(); 
     } 

     writer.WriteEndElement(); 
     writer.WriteEndDocument(); 
    } 
+0

謝謝你的建議。但是,這給了我完全相同的結果。你在控制檯應用程序中測試? – 2014-10-16 11:58:46

+0

是的,它在控制檯應用程序中運行平穩,嘗試使用它,XmlTextWriter w = new XmlTextWriter(「test.xml」,Encoding.UTF8); w.WriteStartElement(「root」); w.WriteAttributeString(「xmlns」,「x」); w.WriteStartElement(「item1」); w.WriteEndElement(); w.WriteStartElement(「item2」); w.WriteEndElement();或使用此鏈接http://stackoverflow.com/questions/24615574/windows-8-store-app-xmlwriter-does-not-write-anything-to-file – LostCoder 2014-10-16 12:10:32

+0

「無法解析符號XmlTextWriter」 – 2014-10-16 12:19:03

0

試圖將button_submit_Click方法一點點改變這樣的無using內使用。適用於我:

XmlWriter writer = XmlWriter.Create("output.xml")); 
writer.WriteStartElement("book"); 
writer.WriteElementString("price", "19.95"); 
writer.WriteEndElement(); 
writer.Flush(); 
+0

謝謝你的建議。但是,這給了我完全相同的結果。你在控制檯應用程序中測試? – 2014-10-16 11:57:39

+0

不。讓我嘗試PLZ,我會告訴你 – 2014-10-16 11:59:42

+0

我試過一個控制檯應用程序,並且構建是OK。你的框架是什麼?Visual Studio? – 2014-10-16 12:04:00

0

在Visual Studio中,到了查看菜單,然後選擇對象瀏覽器

在左側窗格中查找System.Xml,導航下面

1. System.Xml 
    - System.Xml 
     - XmlWriter 

現在點擊XmlWriter和右窗格中會顯示所有該類型的方法。查找Create方法並查看可用的過載。像我一樣,你應該看到Create(string)。如果沒有,那麼老實說,我不確定這裏發生了什麼。這可能是因爲你正在使用不同的.NET Framework,如可移植類庫或什麼?

編輯:

你可以只使用

using (XmlWriter writer = XmlWriter.Create("output.xml", new XmlWriterSettings()))

這樣至少你可以隨身攜帶你的工作。

+0

其實,我沒有看到創建(字符串)。我有.net 4.5和4.5.1 – 2014-10-16 12:14:07

+0

感謝您的建議,但'使用(XmlWriter作家= XmlWriter.Create(「output.xml」,新的XmlWriterSettings()))'給出了與我的原始代碼相同的錯誤 – 2014-10-16 12:20:11

0

因爲你寫道:我創建了一個「空白頁」 項目,爲Windows商店應用。 問題是該方法不支持可移植類庫.NET for Windows應用商店應用。如果您在文檔中比較Create(Stream)Create(string)版本信息,您將看到Create(string)在您選擇的框架中不受支持。

+0

我可以糾正這個?如果是這樣,怎麼樣? – 2014-10-16 12:15:07