Generate XML from ASP.Net GridView

After fetching data on Gridview from any Source some time we need to generate a XML for the data displayed on the Grid, in this example I'm using JQuery to fetch data from GridView and generate XML:

<script type="text/javascript">
        function getChanges() {
            var xmlData = ""
            $("#<%=grd1.ClientID%> tr").each(function (e1) {
                if ($("td", this).length > 0) {
                    xmlData += "<Record>";
                }
                $("td", this).each(function (e2) {
                    var selTD = $(this).closest("td");
                    var th = $(selTD).closest('table').find('th').eq($(selTD).index());
                    xmlData += "<" + th.text() + ">" + htmlEscape(selTD.html()) + "</" + th.text() + ">"
                });
                if ($("td", this).length > 0) {
                    xmlData += "</Record>";
                }
                if ($("td", this).length > 0) {
                    xmlData += "<TextRecord>";
                }
                $("td", this).each(function (e2) {
                    var selTD = $(this).closest("td");
                    var th = $(selTD).closest('table').find('th').eq($(selTD).index());
                    xmlData += "<" + th.text() + ">" + selTD.text() + "</" + th.text() + ">"
                });
                if ($("td", this).length > 0) {
                    xmlData += "</TextRecord>";
                }
            });
            $.ajax({
                async: false,
                type: "POST",
                url: "testAspose.aspx",
                contentType: "application/xml;",
                data: xmlData,
                dataType: "xml",
                success: ajaxCallSuccess,
                error: ajaxCallFailure
            });
        }
        //Call the page method 
   


    function ajaxCallSuccess(response) {
        var msg1 = response.d;
        alert("Data updated sucessfully in DataBase.");
    }

    function ajaxCallFailure(response,es,em) {
        alert("Data updated sucessfully in DataBase.");
    }

        function htmlEscape(str) {
            return String(str)
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
        }

                }
    </script>


And the ASP.net code for Grid is:
<div>
        <asp:GridView ID="grd1" runat="server" OnRowDataBound="grd1_RowDataBound">
        </asp:GridView>
        <asp:HiddenField ID="hdfData" runat="server" />
    </div>

Comments

Technology