XmlDocument doesn't handle the fragment values properly, i.e. if we try to load XmlDocument with the xml fragment value then we get the error saying "There are multiple root elements".
I have also tried using the XmlReaderSettings.ConformanceLevel to
Fragment(and Auto) to load the xml fragment value to properly, that also
doesn't work.
http://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings.conformancelevel(v=vs.110).aspx
static void Main(string[] args)
{
XmlDocument xmlDoc = null;
XmlReader xmlReader = null;
XmlReaderSettings settings = new XmlReaderSettings();
// ConformanceLevel.Fragment: This setting accepts XML data that has multiple root elements or text nodes at the top level
settings.ConformanceLevel = ConformanceLevel.Fragment;
xmlReader = XmlReader.Create(new
StringReader("<NAME>Name0</NAME><EXPERIENCE YEARS=\"0\"
/>"), settings);
try
{
xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(xmlReader); //throws the error : This document already has a 'DocumentElement' node
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
I have also tried using the XmlReaderSettings.ConformanceLevel to
Fragment(and Auto) to load the xml fragment value to properly, that also
doesn't work.
http://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings.conformancelevel(v=vs.110).aspx
static void Main(string[] args)
{
XmlDocument xmlDoc = null;
XmlReader xmlReader = null;
XmlReaderSettings settings = new XmlReaderSettings();
// ConformanceLevel.Fragment: This setting accepts XML data that has multiple root elements or text nodes at the top level
settings.ConformanceLevel = ConformanceLevel.Fragment;
xmlReader = XmlReader.Create(new
StringReader("<NAME>Name0</NAME><EXPERIENCE YEARS=\"0\"
/>"), settings);
try
{
xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(xmlReader); //throws the error : This document already has a 'DocumentElement' node
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}