-1

我有以下 xml,我的需要是在每个 ROW 中设置 WF_PROCESSID,而无需在 c# 中循环和设置。你能请任何人提出解决方案吗?

<RESP_WF_MAP>
 <ROW>
  <PLAN_FOR_RESP>Mombasa</PLAN_FOR_RESP> 
  <WF_PROCESSID>-9999</WF_PROCESSID> 
  <WF_PROCESSNAME /> 
  <DIRECT_MAP_YN>N</DIRECT_MAP_YN> 
  </ROW>
  <ROW>
  <PLAN_FOR_RESP>Mombasa-Changamwe Branch</PLAN_FOR_RESP> 
  <WF_PROCESSID>-9999</WF_PROCESSID> 
  <WF_PROCESSNAME /> 
  <DIRECT_MAP_YN>N</DIRECT_MAP_YN> 
  </ROW>
  <ROW>
  <PLAN_FOR_RESP>Mombasa-Mombasa Branch</PLAN_FOR_RESP> 
  <WF_PROCESSID /> 
  <WF_PROCESSNAME /> 
  <DIRECT_MAP_YN>N</DIRECT_MAP_YN> 
  </ROW>
</RESP_WF_MAP>

我想消除这个循环,需要一个方便的 xml 处理方法

for ( intIndex = 0 ; intIndex < intLength ; intIndex++ )
{
    xmlRowElement           = (XmlElement)xmlRowNodeList.Item(intIndex);
    strWorkFlowId           = DwXMLUtilBO.getElementTagValue(xmlRowElement,"WF_PROCESSID");  // Customized static method to acquire the element value

    if(strWorkFlowId.Trim().Equals(""))
    {
       DwXMLUtilBO.setElementTagValue(xmlRowElement,"WF_PROCESSID" ,strWfId); // Customized static method to set the element value
    }

}
4

2 回答 2

0

LINQ 到 XML:

foreach (var rowElement in XDocument.Load("yourXml").Descendants("ROW"))
{
    var idElement = rowElement.Element("WF_PROCESSID");
    if (string.IsNullOrWhiteSpace(idElement.Value))
    {
        idElement.Value = strWfId;
    }
}
于 2013-02-20T04:52:25.890 回答
0

尝试使用XmlDocument类而不是您的自定义类,如下所示:

            var xml = @"
<RESP_WF_MAP>
<ROW>
<PLAN_FOR_RESP>Mombasa</PLAN_FOR_RESP> 
<WF_PROCESSID>-9999</WF_PROCESSID> 
<WF_PROCESSNAME /> 
<DIRECT_MAP_YN>N</DIRECT_MAP_YN> 
</ROW>
<ROW>
<PLAN_FOR_RESP>Mombasa-Changamwe Branch</PLAN_FOR_RESP> 
<WF_PROCESSID>-9999</WF_PROCESSID> 
<WF_PROCESSNAME /> 
<DIRECT_MAP_YN>N</DIRECT_MAP_YN> 
</ROW>
<ROW>
<PLAN_FOR_RESP>Mombasa-Mombasa Branch</PLAN_FOR_RESP> 
<WF_PROCESSID /> 
<WF_PROCESSNAME /> 
<DIRECT_MAP_YN>N</DIRECT_MAP_YN> 
</ROW>
</RESP_WF_MAP>";

        var doc = new XmlDocument();
        doc.LoadXml(xml);
        var list = doc.GetElementsByTagName("WF_PROCESSID");

        foreach (XmlNode node in list) 
        {
            node.InnerText = "your new value";
        }

        Console.Write(doc.InnerXml);
        Console.ReadKey();
于 2013-02-20T05:03:33.227 回答