internal System.Xml.XmlNode ToXml(string defaultNamespaceUri, string xml)
{
// See ECMA357 10.3.1
DocumentBuilder builder = null;
try
{
string syntheticXml = "<parent xmlns=\"" + defaultNamespaceUri + "\">" + xml + "</parent>";
builder = GetDocumentBuilderFromPool();
XmlDocument document = builder.Parse(new InputSource(new StringReader(syntheticXml)));
if (ignoreProcessingInstructions)
{
IList<System.Xml.XmlNode> list = new List<System.Xml.XmlNode>();
AddProcessingInstructionsTo(list, document);
foreach (System.Xml.XmlNode node in list)
{
node.ParentNode.RemoveChild(node);
}
}
if (ignoreComments)
{
IList<System.Xml.XmlNode> list = new List<System.Xml.XmlNode>();
AddCommentsTo(list, document);
foreach (System.Xml.XmlNode node in list)
{
node.ParentNode.RemoveChild(node);
}
}
if (ignoreWhitespace)
{
// Apparently JAXP setIgnoringElementContentWhitespace() has a different meaning, it appears from the Javadoc
// Refers to element-only content models, which means we would need to have a validating parser and DTD or schema
// so that it would know which whitespace to ignore.
// Instead we will try to delete it ourselves.
IList<System.Xml.XmlNode> list = new List<System.Xml.XmlNode>();
AddTextNodesToRemoveAndTrim(list, document);
foreach (System.Xml.XmlNode node in list)
{
node.ParentNode.RemoveChild(node);
}
}
XmlNodeList rv = document.DocumentElement.ChildNodes;
if (rv.Count > 1)
{
throw ScriptRuntime.ConstructError("SyntaxError", "XML objects may contain at most one node.");
}
else
{
if (rv.Count == 0)
{
System.Xml.XmlNode node = document.CreateTextNode(string.Empty);
return node;
}
else
{
System.Xml.XmlNode node = rv.Item(0);
document.DocumentElement.RemoveChild(node);
return node;
}
}
}
catch (IOException)
{
throw new Exception("Unreachable.");
}
catch (ParserConfigurationException e)
{
throw new Exception(e);
}
finally
{
if (builder != null)
{
ReturnDocumentBuilderToPool(builder);
}
}
}