XmlDocument doc = new XmlDocument();
doc.Load("library.xml");
XmlNodeList movies = doc.SelectNodes("//movie");
XmlAttribute attribute = doc.CreateAttribute("damaged");
attribute.Value = "no";
foreach (XmlNode item in movies)
{
item.Attributes.Append(attribute);
}
XML文件如下:
<?xml version="1.0" ?>
<library xmlns:network="www.library.com">
<books>
<book checkout="no">
<title>To Kill a Mockingbird1</title>
<author>232323_1_lee</author>
</book>
<book checkout="no">
<title> To Kill a Mockingbird2</title>
<author>232323_2_lee</author>
</book>
<book checkout="yes">
<title> To Kill a Mockingbird3</title>
<author>232323_3_lee</author>
</book>
<book checkout="no">
<title> To Kill a Mockingbird4</title>
<author>232323_4_lee</author>
</book>
</books>
<movies>
<movie checkedout="no">
<title>King Kong1</title>
<year>1933</year>
</movie>
<movie checkedout="no">
<title>King Kong2</title>
<year>2933</year>
</movie>
<movie checkedout="yes">
<title>King Kong3</title>
<year>3933</year>
</movie>
<movie checkedout="no">
<title>King Kong4</title>
<year>4933</year>
</movie>
<movie checkedout="no">
<title>King Kong5</title>
<year>5933</year>
</movie>
</movies>
</library>