使用XmlWriter附加现有XML文件

use*_*160 6 c# xml xmlwriter xmlreader

我使用以下代码来创建XML文件:

XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
xmlWriterSettings.NewLineOnAttributes = true;
using (XmlWriter xmlWriter = XmlWriter.Create("Test.xml", xmlWriterSettings))
{
   xmlWriter.WriteStartDocument();
   xmlWriter.WriteStartElement("School");
   xmlWriter.WriteEndElement();
   xmlWriter.WriteEndDocument();
   xmlWriter.Close();
 }
Run Code Online (Sandbox Code Playgroud)

我需要动态插入节点,创建以下结构:

<?xml version="1.0" encoding="utf-8"?>
<School />
   <Student>
      <FirstName>David</FirstName>
      <LastName>Smith</LastName>
   </Student>
   ...
   <Teacher>
      <FirstName>David</FirstName>
      <LastName>Smith</LastName>
   </Teacher>
   ...
</School>
Run Code Online (Sandbox Code Playgroud)

我该怎么做?应从键盘读取"FirstName"和"LastName"的值,并且可以随时输入值,当然在现有值下.

Gok*_*l E 20

你可以使用Linq Xml

XDocument doc = XDocument.Load(xmlFilePath);
XElement school = doc.Element("School");
school.Add(new XElement("Student",
           new XElement("FirstName", "David"),
           new XElement("LastName", "Smith")));
doc.Save(xmlFilePath);
Run Code Online (Sandbox Code Playgroud)

编辑

如果要将Element添加到Existing <Student>,只需添加一个Attribute

school.add(new XElement("Student",
           new XAttribute("ID", "ID_Value"),
           new XElement("FirstName", "David"),
           new XElement("LastName", "Smith")));
Run Code Online (Sandbox Code Playgroud)

然后,您可以<Student>通过搜索 - > get - > add 将更多详细信息添加到Existing

XElement particularStudent = doc.Element("School").Elements("Student")
                                .Where(student => student.Attribute("ID").Value == "SearchID")
                                .FirstOrDefault();
if(particularStudent != null)
    particularStudent.Add(new XElement("<NewElementName>","<Value>");
Run Code Online (Sandbox Code Playgroud)


use*_*160 12

终于我成功了:)

if (!File.Exists("Test.xml"))
{
   XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
   xmlWriterSettings.Indent = true;
   xmlWriterSettings.NewLineOnAttributes = true;
   using (XmlWriter xmlWriter = XmlWriter.Create("Test.xml", xmlWriterSettings))
   {
      xmlWriter.WriteStartDocument();
      xmlWriter.WriteStartElement("School");

      xmlWriter.WriteStartElement("Student");
      xmlWriter.WriteElementString("FirstName", firstName);
      xmlWriter.WriteElementString("LastName", lastName);
      xmlWriter.WriteEndElement();

      xmlWriter.WriteEndElement();
      xmlWriter.WriteEndDocument();
      xmlWriter.Flush();
      xmlWriter.Close();
   }
}
else
{
   XDocument xDocument = XDocument.Load("Test.xml");
   XElement root= xDocument.Element("School");
   IEnumerable<XElement> rows = root.Descendants("Student");
   XElement firstRow= rows.First();
   firstRow.AddBeforeSelf(
      new XElement("Student",
      new XElement("FirstName", firstName),
      new XElement("LastName", lastName)));
   xDocument.Save("Test.xml");
}
Run Code Online (Sandbox Code Playgroud)

  • XDocument会将整个文件读入内存,这并不总是一个问题但应该知道. (6认同)