將添加一些節點添加到隔離存儲中的現有XML。將節點添加到隔離存儲中的現有XML
using (Stream stream = myIsolatedStorage.OpenFile("History.xml", FileMode.Open, FileAccess.ReadWrite))
{
XDocument loadedData = XDocument.Load(stream);
var RootNode = new XElement("History");
var RecipentN = new XElement("RecipentName", "ABC");
var RecipentNo = new XElement("RecipentNumber", "ABABAB");
var Time = new XElement("TimeStamp", "Monday");
var MessageBody = new XElement("Message", "23");
RootNode.Add(RecipentN, RecipentNo, Time, MessageBody);
loadedData.Root.Add(RootNode);
loadedData.Save(stream);
}
}
我想我的XML會是這樣
<?xml version="1.0" encoding="utf-8"?>
<History>
<RecipentName>Arslan</RecipentName>
<RecipentNumber>03336329631</RecipentNumber>
<TimeStamp>123456</TimeStamp>
<Message>ABC</Message>
</History
<History>
<RecipentName>Arslanaa</RecipentName>
<RecipentNumber>03336329631</RecipentNumber>
<TimeStamp>123456</TimeStamp>
<Message>ABC</Message>
</History
但我的代碼合併到現有的XML錯誤
<?xml version="1.0" encoding="utf-8"?>
<History>
<RecipentName>Arslan</RecipentName>
<RecipentNumber>03336329631</RecipentNumber>
<TimeStamp>123456</TimeStamp>
<Message>ABC</Message>
<History>
<RecipentName>Java</RecipentName>
<RecipentNumber>John</RecipentNumber>
<TimeStamp>23</TimeStamp>
<Message>23</Message>
</History>
</History>
誰能請幫助我,我怎麼能節點,以現有的XML哪在Isolatedstorage中。
我有沒有問題要添加主根節點喜歡讀史,但後來我怎麼能寫操作和XMLTextWritter意味着我將如何添加史根節點與作家
writer.WriteStartElement("History", "");
writer.WriteStartElement("RecipentName", "");
writer.WriteString("Arslan");
writer.WriteEndElement();
writer.WriteStartElement("RecipentNumber", "");
writer.WriteString("03336329631");
writer.WriteEndElement();
writer.WriteStartElement("TimeStamp", "");
writer.WriteString("123456");
writer.WriteEndElement();
writer.WriteStartElement("Message", "");
writer.WriteString("ABC");
writer.WriteEndElement();
// Ends the document
writer.WriteEndDocument();
writer.flush();
有沒有其他的方法呢? –
我相信還有很多其他的方法。但是你想要的方式(在頂層有兩個'History'節點)是無效的XML。如果你想要有無效的XML,你可以寫到文件流的末尾,但我認爲這是一個壞主意。 – yamen