2012-05-10 54 views
0

我無法使用linq將xml修改爲xml。 下面是我使用的代碼:嘗試使用linq將xml修改爲xml和xpath

在下面的代碼參數[0]具有值「支付」

XElement modMethodXElement = new XElement(xdoc1.XPathSelectElement("//INPUT[@VALUE='SLN7DEACTIVATION']/METHOD[@NAME='"+Parameters[0]+"']")); 
       modMethodXElement.SetAttributeValue("PARAMETERS", "ParameterValue"); 

而且這裏的XML我想修改的部分:

SESSIONANALYSER> 
    <AP> 
    <MODES> 
     <NI> 
     <STATEINFORMATION TYPE="PREPAID"> 
      <INIT INVALIDINPUTRESPONSE="-2"> 
      <INPUT VALUE="SLN7ACTIVATION"> 
       <METHOD NAME="CHECKSUBSCRIBERBALANCE" PARAMETERS="SLN7" POSSIBLEACTIONS="FALLOUTON_FAILUREREGION_SETPSOFAILED_FALSE" MAINTAINSESSION="NO" RESPONSE="-4" NEXTLEVEL="" /> 
       <METHOD NAME="SETPSOINUCIP" PARAMETERS="SLN7" POSSIBLEACTIONS="FALLOUTON_FAILUREREGION_SETPSOFAILED_FALSE" MAINTAINSESSION="NO" RESPONSE="-4" NEXTLEVEL="" /> 
       <METHOD NAME="SENDMESSAGE" PARAMETERS="" POSSIBLEACTIONS="FALLOUTONSUCCESS" MAINTAINSESSION="NO" RESPONSE="0" NEXTLEVEL="" /> 
      </INPUT> 
      <INPUT VALUE="SLN7DEACTIVATION"> 
       <METHOD NAME="UNSETPSOINUCIP" PARAMETERS="SLN7" POSSIBLEACTIONS="FALLOUTON_FAILUREREGION_SETPSOFAILED_FALSE" MAINTAINSESSION="NO" RESPONSE="-4" NEXTLEVEL="" /> 
       ***<METHOD NAME="payments" PARAMETERS="fsdg" POSSIBLEACTIONS1="fg,f" MAINTAINSESSION="fdjfg89" RESPONSE="ndfgm" NEXTLEVEL="msdfm"></METHOD>*** 
       <METHOD NAME="SENDMESSAGE" PARAMETERS="23" POSSIBLEACTIONS1="FALLOUTONSUCCESS" MAINTAINSESSION="yes" RESPONSE="format" NEXTLEVEL="msdfm"></METHOD> 
       <METHOD NAME="payments" PARAMETERS="12" POSSIBLEACTIONS1="FALLOUTONSUCCESS" MAINTAINSESSION="yes" RESPONSE="format" NEXTLEVEL="msvm"></METHOD> 
       <METHOD NAME="SENDMESSAGE" PARAMETERS="23" POSSIBLEACTIONS1="FALLOUTONSUCCESS" MAINTAINSESSION="yes" RESPONSE="0" NEXTLEVEL="3"></METHOD> 
       <METHOD NAME="SENDMESSAGE" PARAMETERS="23" POSSIBLEACTIONS1="FALLOUTONSUCCESS" MAINTAINSESSION="NO" RESPONSE="format" NEXTLEVEL="3"></METHOD> 
      </INPUT> 
      </INIT> 

回答

0

我不知道爲什麼你想創建一個新的XElement實例? 我會簡單地選擇給定的元素,xdoc1將包含更改後的屬性。

 XElement modMethodXElement = xdoc1.XPathSelectElement("//INPUT[@VALUE='SLN7DEACTIVATION']/METHOD[@NAME='" + arg + "']"); 

     modMethodXElement.SetAttributeValue("PARAMETERS", "ParameterValue");