我有一个 xml 文档存储在服务器上的一个文件中,现在我想使用 Linq to Xml 修改它。这是我的代码:
String strFile = File.ReadAllText(Server.MapPath("~/SessionAnalyserAccessories.xml"));
XDocument xdoc1 = XDocument.Parse(strFile);
xdoc1.XPathSelectElement("SESSIONANALYSER/AP/MODES/NI/STATEINFORMATION/INIT/INPUT/METHOD").AddAfterSelf(new XElement("METHOD", "", new XAttribute("NAME", "Saurabh")));
我正在使用的 XML 示例,我需要添加一个元素STATEINFORMATION:
<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="" />
</INPUT>
</INIT>
</STATEINFORMATION>
<STATEINFORMATION TYPE="POSTPAID">
<INIT INVALIDINPUTRESPONSE="-2">
<INPUT VALUE="">
<METHOD NAME="SENDMESSAGE" PARAMETERS="" POSSIBLEACTIONS="FALLOUTONSUCCESS" MAINTAINSESSION="NO" RESPONSE="5" NEXTLEVEL="" />
</INPUT>
</INIT>
</STATEINFORMATION>
</NI>
</MODES>
</AP>
</SESSIONANALYSER>