Reputation: 2749
I have a xml document stored in a file on the server, now i want to modify the same using Linq to Xml. Here is my code:
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 Sample i am using, i need to add an element 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>
Upvotes: 0
Views: 2263
Reputation: 5604
I have found some links which contains samples on adding elements through LINQ in XML tree.
Please visit these :-
http://msdn.microsoft.com/en-us/library/bb387083.aspx
http://msdn.microsoft.com/en-us/library/bb387084.aspx
Upvotes: 1