Link to XML in XML Column in Database
All, I have a database field with a string xml column.
I was trying to query the database for each string value and create a new XDocument / XElement and then query the xml for certain values. I keep getting the following error, when I use the string value from the database ... (This is from Linqpad)
This seems to be an encoding issue; but I haven't been able to solve it. I would rather not have to query each record, write it to a file and then process it. Any ideas on how to solve this problem?
I was trying to query the database for each string value and create a new XDocument / XElement and then query the xml for certain values. I keep getting the following error, when I use the string value from the database ... (This is from Linqpad)
Data at the root level is invalid. Line 1, position 1However, if I take the same value from the database and create an xml file from it. It works fine.
This seems to be an encoding issue; but I haven't been able to solve it. I would rather not have to query each record, write it to a file and then process it. Any ideas on how to solve this problem?
my linq to sql query is ..dpimental
var query = from c in CmsContentXmls
where c.NodeId.Equals("1136")
select c.Xml;
my linq to xml query is ...
string sampleXML = query.ToString();
var doc = XElement.Load(new StringReader(sampleXML));
var results =
from e in doc.Descendants()
select e.Parent;
Comments
.Dump()
to query.ToString(), you'll notice it returns the SQL query and not the expected XML value.Also, you can use the
XDocument.Parse
orXElement.Parse
instead of the StringReader.You can apply AsEnumerable() somewhere in between to break out of LINQ-to-SQL context to regular LINQ-to-objects.
This should work: