Home
Options

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)
Data at the root level is invalid. Line 1, position 1
However, 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 ..
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;
dpimental

Comments

  • Options
    Apply a .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 or XElement.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:
    var query =
      from c in CmsContentXmls
      where c.NodeId.Equals("1136")
      select XElement.Parse(c.Xml);
     
    var results =
      from node in query.AsEnumerable()
      from e in node.Descendants()
      select e.Parent;
    
    results.Dump();
    
Sign In or Register to comment.