c# - 如何保存到 XML?

标签 c# xml windows-phone-7

我目前正在制作一个新的 WP7 应用程序,它应该允许用户填写两个文本框,单击一个按钮,然后使内容成为 .XML 文件的一部分。

此时,我正在使用以下代码:

string ItemName = ItemNameBox.Text;
string ItemAmount= ItemAmountBox.Text;

string xmlString = "<Items><Name>"+ItemName+"</Name></Item>";
XDocument document = XDocument.Parse(xmlString);
document.Root.Add(new XElement("Amount", ItemAmount));
string newxmlString = document.ToString();

MessageBox.Show(newxmlString);

现在,当我单击按钮时(因为这是在 onclick 按钮事件中),MessageBox 向我显示输出是正确的 XML 方式。

但是,我如何将其放入现有的 XML 架构中,该架构应该能够包含相当多的行(例如,待办事项/购物 list )?

最佳答案

这是我为 windows phone 编写的通用保护程序。

public static void SaveDataToIsolatedStorage(string filePath, FileMode fileMode, XDocument xDoc)
{
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    using (IsolatedStorageFileStream location = new IsolatedStorageFileStream(filePath, fileMode, storage))
    {
        System.IO.StreamWriter file = new System.IO.StreamWriter(location);
        xDoc.Save(file);
    }
}

关于c# - 如何保存到 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8148562/

相关文章:

.net - 使用 Windows Phone 7.1 的 Web 应用程序

windows-phone-7 - MVVM Light SimpleIoC 是否支持单例?

c# - 如何防止ServiceStack EventLogFactory记录DEBUG事件?

C# WPF OxyPlot x, y 应该具有相同的缩放比例

Android:PreferenceManager 与 Context.getSharedPreferences() 以及为什么后者让我失败

具有 unicode 支持且没有 COM 注册的 C++ xml 解析器

java - Spring - cURLS 不使用 Jackson XML Mapper 反序列化

c# - 可能的 JSON 到 C# 中的字典

c# - Windows 应用商店应用程序中的 TopAppBar 按钮 AutomationProperties 样式

silverlight - 当我尝试获取值时,IsolatedStorageSettings 会抛出IsolatedStorageFileStream