下面给出了XML文件数据我想在c#中更改Urdu Book的Price节点的值。怎么做!!!
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Product>
<Name>English Book</Name>
<Pieces>10</Pieces>
<RemainingPieces>5</RemainingPieces>
<Price>100</Price>
</Product>
<Product>
<Name>Urdu Book</Name>
<Pieces>20</Pieces>
<RemainingPieces>10</RemainingPieces>
<Price>1000</Price>
</Product>
<Product>
<Name>Grammar Book</Name>
<Pieces>20</Pieces>
<RemainingPieces>10</RemainingPieces>
<Price>1000</Price>
</Product>
</Data>
答案 0 :(得分:0)
您可以尝试使用Linq to XML:
var doc = XDocument.Load("book.xml");
doc.Element("Data")
.Element("Product")
.Element("Price")
.SetElementValue("value");
doc.Save("book_modified.xml");
Haven没有尝试编译,但应该足够接近。
答案 1 :(得分:0)
如何处理你的xml?
DataSet ds = new DataSet();
byte[] strAsBytes = new System.Text.UTF8Encoding().GetBytes(strYourXml);
System.IO.MemoryStream ms = new System.IO.MemoryStream(strAsBytes);
ds.ReadXml(ms);
string serach_name = "Urdu Book";
string new_value = 42;
if ((ds.Tables("Product") != null)) {
foreach (DataRow t_row in ds.Tables("Product").Rows) {
if ((t_row("Name") == serach_name)) {
t_row("Price") = new_value;
break;
}
}
}