2011-11-16 67 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