0

我无法使用 linq to xml 修改 xml。这是我正在使用的代码:

在下面的代码中,Parameters[0] 的值为“payments”

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>
4

1 回答 1

0

我不确定您为什么要创建 XElement 的新实例?我会像这样简单地选择给定的元素,然后 xdoc1 将包含更改后的属性。

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

        modMethodXElement.SetAttributeValue("PARAMETERS", "ParameterValue");
于 2012-05-10T15:51:52.007 回答