[英]How can I append information in an XML file using LINQ in C#?
我正在尝试在XML文件中写入一些信息,其结构应为:
<Scoreboard>
<Match>
<name>Dummy</name>
<score>1234</score>
</Match>
<Match>
<name>Sample</name>
<score>4567</score>
</Match>
</Scoreboard>
问题是,如果我插入另一个“匹配”项,那么我只能写一个条目,而旧的条目将被覆盖。
例如,如果我有:
<Scoreboard>
<Match>
<name>Dummy</name>
<score>1234</score>
</Match>
</Scoreboard>
然后我想添加另一个条目,旧条目将被删除,而我只有新条目,例如:
<Scoreboard>
<Match>
<name>Sample</name>
<score>4567</score>
</Match>
</Scoreboard>
我会知道如何写新条目而不覆盖旧条目。
这是我的代码:
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
//If file doesn't exist, then create it.
if (!isoStore.FileExists("scoreboard.xml"))
{
XDocument doc = new XDocument(new XElement("Scoreboard"));
using (IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream("scoreboard.xml", System.IO.FileMode.Create, isoStore))
{
doc.Save(isoStream);
}
}
else
{
//Else open it and write a new element which is a child of Scoreboard
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("scoreboard.xml", System.IO.FileMode.Open, isoStore))
{
XDocument doc1 = XDocument.Load(isoStream);
var newElement = new XElement("Match",
new XElement("name", VarGlobal.Name),
new XElement("score", VarGlobal.Score));
doc1.Element("Scoreboard").Add(newElement);
Messaggio.Text = doc1.ToString();
}
}
}
在这种情况下,使用XmlSerializer会比手动在xml中推送元素更有效。 为记分卡对象建模,并使用基本的序列化器和反序列化器。 以下示例应该可以工作(假设您的操作方法称为SaveXmlValue())。
public class Match
{
public string Name { get; set; }
public string Score { get; set; }
}
public class Scorecard
{
public List<Match> Match { get; set; }
}
public static Scorecard DeSerialize(Stream strm)
{
var ser = new XmlSerializer(typeof(Scorecard));
strm.Seek(0, SeekOrigin.Begin);
return (Scorecard)ser.Deserialize(strm);
}
public static string Serialize(Scorecard scores)
{
var ser = new XmlSerializer(typeof(Scorecard));
using (var strm = new MemoryStream())
{
var writer = XmlWriter.Create(strm, new XmlWriterSettings() { Indent = true, Encoding = System.Text.Encoding.UTF8 });
ser.Serialize(writer, scores);
strm.Seek(0, SeekOrigin.Begin);
var bytes = new byte[strm.Length];
strm.Read(bytes, 0, (int)strm.Length);
return System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}
}
public void SaveXmlValue()
{
Scorecard scores;
if (isoStore.FileExists("scoreboard.xml"))
{
using (IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream("scoreboard.xml", System.IO.FileMode.Open, isoStore))
{
scores = DeSerialize(isoStream);
}
}
else
{
// Xml file doesn't exist, create new score card object
scores = new Scorecard();
scores.Match = new List<Match>();
}
scores.Match.Add(new Match() { Name = VarGlobal.Name, Score = VarGlobal.Score });
var xml = Serialize(scores);
using (IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream("scoreboard.xml", System.IO.FileMode.Create, isoStore))
{
var buffer = System.Text.Encoding.UTF8.GetBytes(xml);
isoStream.Write(buffer, 0, buffer.Length);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.