开发者

Loading XML from string in javascript works fine in IE 8, fails in firefox

开发者 https://www.devze.com 2023-01-15 19:33 出处:网络
I have an XML file which was generated by serialising a C# object. I want to parse this XML using javascript.

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? 开发者_JAVA技巧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.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号