我有一个 XML 文档(请参阅下面的摘录),并且我有一个方法,我将 3 个字符串参数传递给该方法 - 用户名、文件名和状态。我需要该程序查看 XML 文档,将文件名与文件的 DMC 元素匹配,然后将 status 和 currentUser 元素值更改为我传递给我的方法的 userName 和 status 值。
<dataModule>
<DMC>DMC-PO-A-49-00-00-00A-012A-C_001.SGM</DMC>
<techName>Pneumatic and shaft power gas turbine engine</techName>
<infoName>General warnings and cautions and related safety data</infoName>
<status>Checked In</status>
<notes>-</notes>
<currentUser>-</currentUser>
</dataModule>
<dataModule>
<DMC>DMC-PO-A-49-00-00-00A-00VA-C_001.SGM</DMC>
<techName>Pneumatic and shaft power gas turbine engine</techName>
<infoName>List of Applicable Specifications and Documentation</infoName>
<status>Checked In</status>
<notes>-</notes>
<currentUser>-</currentUser>
</dataModule>
<dataModule>
<DMC>DMC-PO-A-49-00-00-00A-001A-C_001.SGM</DMC>
<techName>Pneumatic and shaft power gas turbine engine</techName>
<infoName>Title page</infoName>
<status>Checked In</status>
<notes>-</notes>
<currentUser>-</currentUser>
</dataModule>
目前我有以下代码。我希望这能说明我想要达到的目标。这是一个 WinForms 项目,我想用 Linq 来做这件事。
public static void updateStatus(string user, string file, string status)
{
XDocument doc = XDocument.Load(Form1.CSDBpath + Form1.projectName + "\\Data.xml");
var el = from item in doc.Descendants("dataModule")
where item.Descendants("DMC").First().Value == file
select item;
var stat = el.Descendants("status");
// code here to change the value of the 'status' element text
var newUser = el.Descendants("currentUser");
// code here to change the value of the 'currentUser' element text
}