如何使用XMLWriter类
附加到XMl文件中XmlWriterSettings xmlFragments = new XmlWriterSettings();
xmlFragments.Indent = true ;
XmlTextWriter xWriter = new XmlTextWriter(_logFilePath, null);
xWriter.WriteStartDocument();//starting document
xWriter.WriteStartElement("values");//starting parent node
xWriter.WriteStartElement("values1");//1st child node
xWriter.WriteAttributeString("id", "200");//attributes of child node
xWriter.WriteAttributeString("name", "ABCD");
xWriter.WriteString("ISO Company");//innertext
xWriter.WriteEndElement();
xWriter.WriteStartElement("num");//2nd child node
xWriter.WriteAttributeString("more", "500");
xWriter.WriteAttributeString("less", "101");
xWriter.WriteString("numeric");
xWriter.WriteEndElement();
xWriter.WriteStartElement("r", "runnnig", "");//3rd child node
xWriter.WriteAttributeString("fast", "500km");
xWriter.WriteAttributeString("slow", "10km");
xWriter.WriteString("killometers");
xWriter.WriteEndElement();
xWriter.WriteStartElement("character");
xWriter.WriteAttributeString("char", "a");
xWriter.WriteAttributeString("another", "b");
xWriter.WriteEndElement();
xWriter.WriteEndElement();
xWriter.WriteEndDocument();
xWriter.Close();
我正在尝试在xmlwriter中加载xml文件,但是这个类会覆盖以前的标记,但我不想覆盖标记。
答案 0 :(得分:1)
可以通过XmlDocument解决。
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlElement el = doc.CreateElement("child");
el.InnerText = "This row is being appended to the end of the document.";
doc.DocumentElement.AppendChild(el);
doc.Save("test.xml");
检查here以获取更多选项。