LIKE US ON FACE BOOK

Tuesday, 14 July 2015

Convert XML to relavent HTML and apply XSLT style sheet as well & display contents in Bootstrap Modal in MVC Using AJAX

Hi Guys ,

Here is the way to convert XML data to corresponsing HTML by apply XSLT style sheet & display contents in Bootstrap Modal.

Bootstrap Modal:

<div class="modal fade" id="ccdaModel" role="dialog">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button"  return false;" class="close" data-dismiss="modal">&times;</button>
                    <h2 class="modal-title">Hi Test Modal </h2>
                </div>
                <div class="modal-body">
                        <div id="ccdaContent"></div>
                        <div id="ccdaMessage" style="display:none;" class='alert alert-info'>File not found!<br></div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" return false;" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>

        </div>

JS Code:-
$(document).on("click", ".cutomCCDA", function () {
            $.ajax({
                type: "GET",
                url: url,
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                success: function (result) {
                    $('#ccdaContent').html(result);
                },
                error: function (xhr, status) {
                    console.log(status);
                }
            });
    });

Action Method:-

public ActionResult ConvertXMLtoHTMLwithXSLT()
        {
            string xmldocument= "xml string";
            string XSLTfile= @"give path here";
            if (!string.IsNullOrEmpty(xmldocument))
            {
                    //load XSLT
apply xslt setting according to your needs
                    XsltSettings xslsettings = new XsltSettings();
                    xslsettings.EnableScript = true;
                    xslsettings.EnableDocumentFunction = true;
//apply xslt
                    XslCompiledTransform proc = new XslCompiledTransform();
                    proc.Load(Server.MapPath(XSLTfile), xslsettings, null);
                    using (StringReader sr = new StringReader(xmldocument))
                    {
                        using (XmlReader xr = XmlReader.Create(sr))
                        {
                            using (StringWriter sw = new StringWriter())
                            {
//apply xsl to out XML
                                proc.Transform(xr, null, sw);
                                return Content(sw.ToString());
                            }
                        }
                    }
            }
            return Content("<div class='alert alert-info'>File not found!<br></div>");
        }

Hope it helps you..please do let meknow if you have any concerns..

No comments:

Post a Comment