簡體   English   中英

使用XmlWriter創建xml文件

[英]Create xml file with XmlWriter

我在XmlWriter中遇到了Create方法的問題。

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

我為Windows商店應用創建了一個“空白頁面”項目。 在其中,我插入了來自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();
            }
        }
    }
}

這是一個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();
  }
}

一旦知道它可行,就可以刪除命名空間限定符。

檢查此項以了解如何在Windows應用商店應用中存儲文件。 也看看這個樣品。 我的代碼將使用本地應用程序文件夾,例如: C:\\Users\\[name]\\AppData\\Local\\Packages\\[appName]\\LocalState\\

這可能會有所幫助,

 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();
    }

嘗試在你的button_submit_Click方法中使用這個有點改變而不使用 .Works給我:

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

在Visual Studio中,進入“ 視圖”菜單並選擇“ 對象瀏覽器”

在左側窗格中查找System.Xml ,然后導航以下內容

1. System.Xml
    - System.Xml
       - XmlWriter

現在單擊XmlWriter ,右側窗格將顯示該類型的所有方法。 查找Create方法並查看可用的重載。 您應該像我一樣看到Create(string) 如果沒有,那么說實話,我不確定這里發生了什么。 可能是你正在使用不同的.NET Framework,比如Portable Class Library或者其他什么?

編輯:

你可以使用

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

所以至少你可以繼續你的工作。

因為您寫道: 為Windows商店應用創建了一個“空白頁面” 項目 問題是可移植類庫.NET for Windows Store應用程序不支持該方法。 如果您比較文檔中的Create(Stream)Create(字符串)版本信息,您將看到所選框架中不支持Create(字符串)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM