https://www.cnblogs.com/guxia/p/8242483.html
修改属性
//改变属性的值XmlDocument doc = new XmlDocument();doc.Load("Order.xml");XmlNode xn = doc.SelectSingleNode("/Order/Items/OrderItem[@Name='190']");xn.Attributes["Count"].Value = "200";xn.Attributes["Name"].Value = "颜世伟";doc.Save("Order.xml");Console.WriteLine("保存成功");
修改内容
XmlDocument doc = new XmlDocument();doc.Load(@"F:\Books.xml");XmlNodeList nodeList = doc.SelectSingleNode("/Books/Book[@id='3d310e87-6c46-4874-859e-c09f3acce589']").ChildNodes;foreach (XmlNode xn in nodeList)//遍历所有子节点{XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型//Console.WriteLine(xe.GetAttribute("id"));if (xe.Name == "Price"){Console.WriteLine(xe.InnerText);xe.InnerText = "oooooooo";}}doc.Save(@"F:\Books.xml");
<?xml version="1.0" encoding="utf-8"?><Books><Book id="58c865aa-454a-4bb1-98c3-aff815ff9406"><Name>金瓶梅</Name><Price>10</Price><Des>好看</Des></Book><Book id="3d310e87-6c46-4874-859e-c09f3acce589"><Name>金瓶梅</Name><Price>oooooooo</Price><Des>好看</Des></Book></Books>
