如何使用基于ID

时间:2017-07-12 04:50:46

标签: c# asp.net xml nodes parent

我正在尝试根据我在C#中获取的ID来更新Xml中的节点。

我的xml文件如下所示:::: Xml的名称:Contact.xml

<CATALOG>
 <CD>
    <ID>1</ID>
    <HeaderDetailID>1</HeaderDetailID>
    <FirstName>Basant</FirstName>
    <LastName>Gera</LastName>
    <EmployeeID>0012</EmployeeID>
    <Department>SD</Department>
    <Postion>Programmer</Postion>
    <Application>Balance***BI_AP_KL_BALANCE</Application>
    <Filter>Africa_ww_READ</Filter>
    <AreaorCountryorStation>India</AreaorCountryorStation>
    <NetworkDomain> Area / Country / Station Code </NetworkDomain>
    <Action>
      Modification
    </Action>
    <NameOfController>Area</NameOfController>
    <Status>New Request</Status>
  </CD>
  <CD>
    <ID>2</ID>
    <HeaderDetailID>1</HeaderDetailID>
    <FirstName>Ankush</FirstName>
    <LastName>Tyagi</LastName>
    <EmployeeID>0013</EmployeeID>
    <Department>SD</Department>
    <Postion>Programmer</Postion>
    <Application>Balance***BI_AP_KL_BALANCE</Application>
    <Filter>Alps</Filter>
    <AreaorCountryorStation>India</AreaorCountryorStation>
    <NetworkDomain> Area / Country / Station Code </NetworkDomain>
    <Action>
      Modification
    </Action>
    <NameOfController>Area</NameOfController>
    <Status>New Request</Status>
  </CD>
  <CD>
</CATALOG>

我在隐藏字段中获取id,而不是将Status的节点更新为字符串“Request Completed”

所以我需要更改此节点

<Status>New Request</Status>

到节点

<Status>Request Completed</Status>

根据我得到的身份证。

我正在尝试编码如下:

 protected void btnTemp_Click(object sender, EventArgs e) {
     string iIDs = hdnData.Value;
     string[] DateData;
     DateData = hdnData.Value.Split(';');

     for (int i = 0; i < DateData.Length - 1; i++) {
         string iRecordID = DateData[i];

         XmlDocument xmlDoc = new XmlDocument();
         string  filepathsUpdate = Server.MapPath("~/Contact.xml");
         xmlDoc.Load(filepathsUpdate);
         XmlNode node = xmlDoc.SelectSingleNode("/CATALOG/CD[@ID=" + iRecordID + "]");
         node.Attributes["Status"].Value = "Request Completed";
         xmlDoc.Save(filepathsUpdate);
     }
}

但是这里我的节点为null。为什么我无法根据ID

更新节点

1 个答案:

答案 0 :(得分:2)

据我所知,您在选择SingleNode时缺少单引号'。你的代码应该是这样的

XmlNode node = xmlDoc.SelectSingleNode("CATALOG/CD[@ID='" + iRecordID + "']");

请更新以上代码。

它应该有用。

由于