2015-04-06 57 views
-6

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> 
+2

做一个简单的谷歌搜索'C#计算器如何更改XML file'万吨互联网 – MethodMan

+1

C#中的XML已多次和广泛报道了许多地方从事工作的例子在那里的节点值。像推荐的@MethodMan这样粗略的Google搜索是一个很好的开始。请先尝试一下,然后再回答一些关于*代码*的更具体问题。 – tnw

回答

0

您可以尝试使用LINQ to XML,因为这:

var doc = XDocument.Load("book.xml"); 
doc.Element("Data") 
    .Element("Product") 
    .Element("Price") 
    .SetElementValue("value"); 
doc.Save("book_modified.xml"); 

About Linq to XML

没有尝试编译,但应尽量接近。

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; 
     } 

    } 
}