Please enable Javascript to correctly display the contents on Dot Net Tricks!
 
Coding is Rhyme
Handy Tricks For Beginners & Professionals
Java Script - Tutorial, Article, Archive, Pdf, Handy Tricks, Training, Code Snippets, Reference Manual, Objects, Attributes, Events, Functions, CSS, DOM Manipulation, AJAX Support, Event Handling, Plug-ins

Convert string to xml and xml to string using javascript

Posted By : Shailendra Chauhan, 13 Jun 2012
Updated On : 13 Jun 2012
Keywords : parse xml to string, parse string to xml, xml parsing using javascript, convert xml to string using javascript, cross browser xml parsing using javascript pdf

Sometimes we need to parse xml into string and string into xml. Different browsers parse xml to string and vice-versa in different ways. Here, I am sharing cross browser compatible methods to parse xml.

Parse XML to String

 <script type="text/javascript" language="javascript">
function XMLToString(oXML)
{
 //code for IE
 if (window.ActiveXObject) {
 var oString = oXML.xml; return oString;
 } 
 // code for Chrome, Safari, Firefox, Opera, etc.
 else {
 return (new XMLSerializer()).serializeToString(oXML);
 }
 }
</script> 

Parse String to XML

 <script type="text/javascript" language="javascript">
function StringToXML(oString) {
 //code for IE
 if (window.ActiveXObject) { 
 var oXML = new ActiveXObject("Microsoft.XMLDOM"); oXML.loadXML(oString);
 return oXML;
 }
 // code for Chrome, Safari, Firefox, Opera, etc. 
 else {
 return (new DOMParser()).parseFromString(oString, "text/xml");
 }
}
</script> 
About the Author
Shailendra Chauhan works as Sr. Software Engineer at reputed MNC and has more than 5 years of hand over Microsoft .NET technologies. He is a .NET Consultant and is the founder & chief editor of www.dotnet-tricks.com and www.dotnetinterviewtricks.com blogs. He is an author of books ASP.NET MVC Interview Questions and Answers & LINQ Interview Questions and Answers.
He loves to work with web applications and mobile apps using Microsoft technology including ASP.NET, MVC, C#, SQL Server, WCF, Web API, Entity Framework,Cloud Computing, Windows Azure, jQuery, jQuery Mobile, Knockout.js, Angular.js and many more web technologies. More...
 
 
Recommended Articles!
 
Subscribe & follow Us
 
Creative Commons License
Dot Net Tricks is licensed under a Creative Commons Attribution 4.0 International License.
Free .NET Interview Books
 
Learn In Hindi
Browse By Category
 
Recent Articles
 
Popular Articles
 
Like us on Facebook
 
Featured AD