如何保存到XML?

时间:2011-11-16 08:18:05

标签: 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架构,该架构应该能够包含很多行(例如待办事项/购物清单)?

3 个答案:

答案 0 :(得分:2)

这是我为Windows手机编写的通用保护程序。

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

答案 1 :(得分:2)

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

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

AddItem可以多次调用,每次调用都会向您的&lt; Items&gt;添加一项元件。每次用户点击按钮时都会调用它。

XML的结构如下所示:

<Items>
  <Item>
    <Name>My item</Name>
    <Amount>42</Amount>
  </Item>
  <Item>
    <Name>Another item</Name>
    <Amount>84</Amount>
  </Item>
</Items>

编辑:

用于保存和加载来自隔离存储的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);
    }
}

答案 2 :(得分:0)

我认为这是编写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();