XML文件如下:
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
我想在所有author结点中添加性别结点,但添加时只能添加1个,实在困惑,大家帮我看看下面的代码有啥问题,还有没有其他方法,请各位不吝赐教,先谢谢各位了。
XmlDocument doc = new XmlDocument();
doc.Load("e:\\books.xml"); XmlElement elem = doc.CreateElement("性别");
elem.InnerText="男"; XmlNodeList xnList = doc.GetElementsByTagName("author"); for(int i=0; i<xnList.Count; i++)
{
xnList[i].AppendChild(elem);
}
doc.Save(Console.Out);