C# 如何更改节点的值
XML文件数据如下所示,我想更改c#中乌尔都语图书价格节点的值。怎么做C# 如何更改节点的值,c#,xml,C#,Xml,XML文件数据如下所示,我想更改c#中乌尔都语图书价格节点的值。怎么做 <?xml version="1.0" encoding="utf-8"?> <Data> <Product> <Name>English Book</Name> <Pieces>10</Pieces> <RemainingPieces>5</RemainingPiec
<?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>
英语书
10
5.
100
乌尔都语书
20
10
1000
语法书
20
10
1000
您可以尝试使用Linq to XML,如下所示:
var doc = XDocument.Load("book.xml");
doc.Element("Data")
.Element("Product")
.Element("Price")
.SetElementValue("value");
doc.Save("book_modified.xml");
还没有尝试过编译,但应该已经足够接近了。像这样的东西来处理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;
}
}
}
做一个简单的谷歌搜索
C#stackoverflow如何更改xml文件中的节点值
C#中的xml在互联网上的大量工作示例已经在许多地方反复广泛地讨论过。像@MethodMan recommended这样粗略的谷歌搜索是一个很好的开始。请先尝试一下,当您对所编写的代码有更具体的问题时再回来。