views:

450

answers:

2

I'm currently working on a small web application using Visual Studio 2008 Express. I'm attempting to retrieve an XML document from a server using a client library and then save the document into a database column (using Linq). The database column has the data type specified as xml. Unfortunately, I've been unsuccessful during my first few attempts.

Assuming that I've already got a reference to the data context object, here is the basics of what it is that I'm attempting to do:

// using a client library, requestthe XML document from the server
XmlDocument oXmlDoc = oClient.GetDataAsXML();

InformationLog oLog = new InformationLog();
oLog.InfoXML = oXmlDoc.InnerXml; // this is where the problem occurs

dbContext.InformationLogs.InsertOnSubmit(oLog);
dbContext.SubmitChanges();

Specifically, the error I get is:

Cannot implicitly convert type 'System.Xml.XmlNode' to 'System.Xml.Linq.XElement'

I'm new to ASP.NET MVC and Linq, so I know that I'm missing something. In addition to the answer, I'm also curious as to why it's impossible to simply save the XML as-is without any additional processing.

+1  A: 

You should look to use XDocument rather than XmlDocument, and then try assigning the XDocument directly to your oLog.InfoXML property.

Without knowing how oClient.GetDataAsXML() works it's not clear whether you can easily create an XDocument from that call, but your life will be easier if you work in terms of XDocument instead of XmlDocument.

Martin Peck
Thanks a lot - this ended up putting me on the right track. The client returns an XmlDocument, so I used XDocument.Parse() on the OuterXml string of the returned XmlDocument.
Tom
+1  A: 

Here's a great post on that. Basically you have the new XML types and old XML types clashing there.

JP Alioto
@JP - thanks for the link. Glad to have this as a reference as it helps to explain the clashing that I was experiencing.
Tom