创建 XML 文件

本文关键字:文件 XML 创建 | 更新日期: 2023-09-27 18:35:05

我有这个格式的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("answerswer").Value,
    }).ToList();

问题是。我可以读取外部创建的 xml 文件。但是我不知道如何通过代码创建/编写xml文件。并使用我给定的代码来阅读它。有什么想法吗?谢谢!

创建 XML 文件

使用 XElement 对象。例:

XDocument document = new XDocument();
XElement rootElement = new XElement("Questions");
foreach(Question question in QuestionsCollection)
{
    XElement questionElement = new XElement("Question");
    questionElement.Add(new XElement("questiontext") { Value = question.Text });
    questionElement.Add(new XElement("choice1") { Value = question.Question1 });
    questionElement.Add(new XElement("choice2") { Value = question.Question2 });
    questionElement.Add(new XElement("choice3") { Value = question.Question3 });
    questionElement.Add(new XElement("choice4") { Value = question.Question4 });
    questionElement.Add(new XElement("answerswer") { Value = question.Answer });
    rootElement.Add(questionElement);
}
document.Add(rootElement);
document.Save("C:'Location.xml");

这样的事情应该有效。

使用此代码通过将 XML 保存文件路径传递给方法 CreateXml 来创建和保存 XML 文件

查看此链接在 C#/VB 中创建并保存 XML 文件

public void CreateXml(string XmlPath)
{ 
try
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    string Name = string.Empty;
                    int Age = 0;
                    int Experience = 0;
                    Name = ds.Tables[0].Rows[0]["EmployeeName"].ToString();
                    Age = int.Parse(ds.Tables[0].Rows[0]["EmployeeAge"].ToString());
                    Experience = int.Parse(ds.Tables[0].Rows[0]["EmployeeExperience"].ToString());
                    string xml = XmlTemplate().ToString().Replace("EmpName", Name).Replace("EmpAge", Age.ToString(),Replace("EmpExperience", Experience.ToString());
                    XmlPath = XmlPath + "Employee_" + Name + ".xml";
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(xml);
                    xdoc.Save(XmlPath);
                    lblMessage.Text = "XML Created Successfully.";
                }
                else
                {
                    lblMessage.Text = "InValid Employee ID.";
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }
}

public string XmlTemplate()
{
            string Xml = "<Employee>" +
                            "<Name>EmpName</Name>" +
                            "<Age>EmpAge</Age>" +
              "<Experience>EmpExperience</Experience>" +
                            "</Employee>";
            return Xml;
}

希望这对你有帮助。谢谢。