views:

29

answers:

1

I have an XML file which was generated by serialising a C# object.

I want to parse this XML using javascript.

When i try to load a string as XML using javascript, it works fine in IE 8, but fails in Firefox.

This is the code i am using

if (window.DOMParser)
{
parser = new DOMParser();
xmlDoc = parser.parseFromString(stringValue, 'text/xml');
}
else
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = 'false';
xmlDoc.loadXML(stringValue);");
}

Any idea why? i thought I had taken care of browser incompatiblity in the above code. Also, here is the XML file, if it can be of any help.

A: 

Ok Super wierd solution here... Turns out the XML file had some white spaces in between, and the DOMParser.parseFromString() method was somehow loading all the blank spaces as nodes.

Tweaking my C# serialisation code like this solved the issue:

XmlDocument tempXml = new XmlDocument();
tempXml.PreserveWhitespace = false;
tempXml.LoadXml(serializedXML);  
hdnSiteConfig.Value = tempXml.OuterXml;
ashwnacharya