How to insert data to xml file using json coding.

Jayakumars
Posted by Jayakumars under ASP.NET AJAX category on | Points: 40 | Views : 1249
hi
I have post the code for insert data to xml file using json coding

Client Side

<script type="text/javascript">

function GoToInsertXML() {

var XMLfile1 = "TestXml";
var Id = "5";
var Name = "Jayakumar";

$.ajax({
type: "POST",
url: "Default2.aspx/AddToXMLfile",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: "{'XMLfile' : '" + XMLfile1 + "', 'Id' : '" + Id + "', 'Name' : '" + Name + "'}",
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
},
success: function (result) {
//console.log(result.d);
}
});
}

</script>


server Side

[WebMethod]
public static string AddToXMLfile(string XMLfile, string Id, string Name)
{
XDocument XMLDoc = XDocument.Load(XMLfile);
XElement root = XMLDoc.Root;
root.Add(new XElement("Customer",
new XElement("Id", Id),
new XElement("Name", Name)));
XMLDoc.Save(XMLfile);
return Id;
}

Comments or Responses

Login to post response