Please enable Javascript to correctly display the contents on Dot Net Tricks!
 
Become an Expert in C#, .NET, MVC, JAVA, PHP, AngularJS, Hadoop, Android, iphone, Testing etc.
by Joining our Training Programs and Take Your Career to the Next Level! To know more make a call on +91-9871-74-9695

Convert string to xml and xml to string using javascript

Posted By : Shailendra Chauhan, 13 Jun 2012
Updated On : 13 Jun 2012
Total Views : 14,429   
 
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> 
 
Further Reading
 
About the Author
Hey! I'm Shailendra Chauhan full-time author, consultant & trainer. I have more than 6 years of hand over Microsoft .NET technologies and other web technologies like JavaScript, AngularJS, NodeJS etc. I am an entrepreneur, the founder & chief editor of www.dotnet-tricks.com and www.dotnettricks.com. I am author of most popular e-books for technical Interview on ASP.NET MVC Interview Questions and Answers & AngularJS Interview Questions and Answers & LINQ Interview Questions and Answers.
I have delivered 100+ training sessions to professional world-wide over Microsoft .NET technologies such C#, ASP.NET MVC, WCF, Entity Framework and other mobile technologies such Ionic, PhoneGap, Corodva. Read more...
 
Free Interview Books
 
SUBSCRIBE & FOLLOW US
 
Browse By Category
 
 
Like us on Facebook