2011-11-16 29 views
3

我目前正在制作一个新的WP7应用程序,它应该使用户能够填写两个文本框,单击一个按钮,并使内容成为.XML文件的一部分。如何保存到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模式中,该模式应该能够包含相当多的行(例如待办事项/购物清单)?

回答

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();    
+0

你好。谢谢您的回答。但是,据我所见,WP7没有XmlTextWriter,只有XmlWriter。 – AndreasB

+0

我很抱歉,我在工作的中间回答了你,所以我希望和skkiped的wp7 :)生病的样子,试图让你的答案是正确的。 – Liran

+0

嘿,似乎wp7有XmlWriter的对象,像我worte一样工作..试试看,并告诉我它是否有帮助。 你可以看看[this](http://www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage---Read-and-Save-XML-files-using-XmlWriter) – Liran

2

这是一个通用的保护我已经为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); 
    } 
} 
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可以被称为多次,每次调用增加一个项目到您的<项目>元素。每次用户点击按钮时都会调用它。

你的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); 
    } 
} 
+0

Hello ,感谢一个非常好评的代码!我设法创建了一个看起来有点像这样的片段,但我仍然不确定“如何将它保存到现有的XML”:/ – AndreasB

+0

什么意思是“现有”?你是否指隔离存储中的xml文件? –

+0

我还没有与独立存储工作。 .xml主要位于“items/xmlFile.xml”中,所以我猜这没关系?只需要设法让一切正常工作,谢谢! – AndreasB