C# 如何保存为XML?

C# 如何保存为XML?,c#,xml,windows-phone-7,C#,Xml,Windows Phone 7,我目前正在开发一个新的WP7应用程序,它应该允许用户填写两个文本框,单击一个按钮,并使内容成为.XML文件的一部分 此时,我使用以下代码: string ItemName = ItemNameBox.Text; string ItemAmount= ItemAmountBox.Text; string xmlString = "<Items><Name>"+ItemName+"</Name></Item>"; XDocument document

我目前正在开发一个新的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);
string ItemName=ItemNameBox.Text;
字符串ItemAmount=ItemAmountBox.Text;
字符串xmlString=“”+ItemName+”;
XDocument document=XDocument.Parse(xmlString);
document.Root.Add(新的XElement(“金额”,ItemAmount));
string newxmlString=document.ToString();
Show(newxmlString);
现在,当我单击按钮时(因为这在onclick按钮事件中),MessageBox向我显示输出是正确的XML


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

我认为这是编写XML文件的更好、正确的方法。您可以在循环中使用此代码段 编写所需的全部xml

IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream
XmlWriterSettings settings = new XmlWriterSettings();        
settings.Indent = true;   
XmlWriter writer = new XmlWriter(isoStream, settings);



        writer.Formatting = Formatting.Indented;
        writer.Indentation = 3;
        writer.WriteStartDocument();
        writer.WriteStartElement("elementName");

        writer.WriteAttributeString("attName", "value");
        writer.WriteValue("value of elem");
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Close();            

这是我为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);
    }
}

如果要添加多个“行”数据,可以这样做:

// create XML element representing one "Item", containing a "Name" and an "Amount" 
// and add it to the given parent element
private void AddItem(XElement parent, string itemName, int amount)
{
    // create new XML element for item
    XElement newItem = new XElement("Item");
    // add the name
    newItem.Add(XElement.Parse("<Name>" + itemName + "</Name>"));
    // add the amount
    newItem.Add(XElement.Parse("<Amount>" + amount + "</Amount>"));
    // add to parent XML element given by caller
    parent.Add(newItem);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    // create new document (in your case you would do this only once, 
    // not on every button click)
    XDocument doc = XDocument.Parse("<Items />");

    // doc.Root is <Items /> - lets add some items
    AddItem(doc.Root, "My item", 42);
    AddItem(doc.Root, "Another item", 84);

    // check if we succeeded (of course we did!)
    Debug.WriteLine(doc.ToString());
}
编辑:

用于将XML保存和加载到独立存储中或从独立存储中加载:

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("myfile.xml", System.IO.FileMode.Create, isf))
    {
        doc.Save(stream);
    }
}


using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("myfile.xml", System.IO.FileMode.Open, isf))
    {
        doc = XDocument.Load(stream);
    }
}

你好谢谢你的回答。然而,据我所知,WP7中没有XmlTextWriter,只有XmlWriter。我很抱歉,我在工作中回答了你,所以我希望并跳过了WP7:)我会尝试给你一个正确的答案……嘿,WP7似乎有XmlWriter对象,它的工作方式与我的工作方式一样。。。试试看,告诉我是否有用。您可以在Hello中查看,感谢您提供的注释非常好的代码!我设法创建了一个看起来有点像这样的代码段,但我仍然不确定“如何将其保存到现有的XML”:/什么是“现有的”?你是说隔离存储中的xml文件吗?我还没有使用过隔离存储。xml主要在“items/xmlFile.xml”中,所以我想这样可以吗?只要设法让一切正常工作,谢谢!