2 回答

TA贡献1827条经验 获得超8个赞
有了LinqtoXml,这很容易做到。强烈建议使用Linq To XmL:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/adding-elements-attributes-and-nodes-to-an-xml-tree
try
{
XDocument xmlDoc = XDocument.Load("StudentDoc.xml"));
xmlDoc.Element("Students").Add(
new XElement("Student",
new XElement("Name", "Peter"),
new XElement("Grade", 10.0),
new XElement("Sex", "Male")));
xmlDoc.Save("StudentDoc.xml"));
}
catch{}
然后你可以做不同的事情,比如排序:
IEnumerable<decimal> names =
from student in root.Elements("Students")
orderby student.Name
select student.Name;
foreach (string name in names)
Console.WriteLine(name);

TA贡献1735条经验 获得超5个赞
您可以找到“学生”节点
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ConfigurationManager.AppSettings.Get("studentFile"));
XmlElement root = xmlDoc.DocumentElement;
XmlNode node = root.SelectSingleNode("//Classrooms/Classroom/Students");
然后最后你可以将新节点附加到这个节点
node.AppendChild(student);
//xmlDoc.DocumentElement.AppendChild(student);
xmlDoc.Save(ConfigurationManager.AppSettings.Get("studentFile"));
- 2 回答
- 0 关注
- 119 浏览
添加回答
举报