0
我有这种格式的xml文件。创建XML文件
<Questions>
<Question>
<questiontext>The remains of the Tabon man was discovered in the Tabon Caves in </questiontext>
<choice1>Lipuun Point</choice1>
<choice2>Callao Cave</choice2>
<choice3>Hinagdanan Cave</choice3>
<choice4>Montfort Bat Sanctuary</choice4>
<answer>Lipuun Point</answer>
</Question>
</Questions>
我通过记事本++创建这个。并阅读它就像这样。
System.IO.Stream stream = TitleContainer.OpenStream("Content//Level1Trivia.xml");
XDocument doc = XDocument.Load(stream);
level1Trivia = new List<Trivias>();
level1Trivia = (from question in doc.Descendants("Question")
select new Trivias()
{
Question = question.Element("questiontext").Value,
Choice1 = question.Element("choice1").Value,
Choice2 = question.Element("choice2").Value,
Choice3 = question.Element("choice3").Value,
Choice4 = question.Element("choice4").Value,
Answer = question.Element("answer").Value,
}).ToList();
问题是。我可以读取一个外部创建的xml文件。但我不知道如何通过代码创建/写入xml文件。并使用我提供的代码来阅读它。有任何想法吗?谢谢!
我会试试这个。我如何将它保存到我的内容文件夹? – ljpv14
按照内容,我假设你是指程序驻留在同一个文件夹中? 有几种方法。我最喜欢的方式就是“document.Save(”File.xml“);”,因为当没有指定驱动器时,路径就是程序的运行路径。 – Falgantil
有没有办法检查文件是否存在?因为我会尝试将数据保存到XML。如果文件已经存在。我想获取它的数据并添加一组数据。 – ljpv14