We have a data table that uses sparse columns, and a column set. When we query data from this table, we are only bringing back a couple of key columns plus the columnset column.
The columnset column is returned as an SqlXml
field type, and I want to parse through the XML to convert it to a dictionary. But, the XML data doesn't seem to be well formed; as whenever I try this:
var columnSet = rdr.GetSqlXml(rdr.GetOrdinal("ColSet"));
if (!columnSet.IsNull)
{
var xml = XDocument.Load(xmlData.CreateReader());
it throws an exception:
System.InvalidOperationException: This operation would create an incorrectly structured document.
at System.Xml.Linq.XDocument.ValidateDocument(XNode previous, XmlNodeType allowBefore, XmlNodeType allowAfter)
at System.Xml.Linq.XDocument.ValidateNode(XNode node, XNode previous)\r\n at System.Xml.Linq.XContainer.AddNodeSkipNotify(XNode n)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Load(XmlReader reader)
If I use GetString()
instead of GetSqlXml()
, I definitely get back invalid XML -- it has no root element, and I have to perform string manipulation to get it to load into an XDocument
.
Is there some way to get the SqlXml
value for a columnset to produce a valid XML document?