我在隔离存储中的现有 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。
我没有像历史一样添加主根节点的问题,但是我如何使用 XMLTextWritter 编写意味着我将如何使用 Writer 添加历史根节点
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();