无法将XmlDocument存储在数据类型为“xml”的表列中

时间:2021-01-27 16:58:13

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.

我目前正在使用Visual Studio 2008 Express开发一个小型Web应用程序。我正在尝试使用客户端库从服务器检索XML文档,然后将文档保存到数据库列中(使用Linq)。数据库列的数据类型指定为xml。不幸的是,在我的前几次尝试中,我一直没有成功。

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.

我是ASP.NET MVC和Linq的新手,所以我知道我错过了一些东西。除了答案之外,我还很好奇为什么在没有任何额外处理的情况下简单地保存XML是不可能的。

3 个解决方案

#1


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

您应该使用XDocument而不是XmlDocument,然后尝试将XDocument直接分配给您的oLog.InfoXML属性。

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.

在不知道oClient.GetDataAsXML()如何工作的情况下,不清楚是否可以从该调用轻松创建XDocument,但如果您使用XDocument而不是XmlDocument工作,您的生活会更容易。

#2


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

这是一篇很棒的帖子。基本上你有新的XML类型和旧的XML类型在那里发生冲突。

#3


You can also switch your project from .net 3.5 to 3.0 which will default you back to system.xml. But that would get you away from using LINQ.

您还可以将项目从.net 3.5切换到3.0,这将默认您返回到system.xml。但这会让你远离使用LINQ。

#1


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

您应该使用XDocument而不是XmlDocument,然后尝试将XDocument直接分配给您的oLog.InfoXML属性。

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.

在不知道oClient.GetDataAsXML()如何工作的情况下,不清楚是否可以从该调用轻松创建XDocument,但如果您使用XDocument而不是XmlDocument工作,您的生活会更容易。

#2


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

这是一篇很棒的帖子。基本上你有新的XML类型和旧的XML类型在那里发生冲突。

#3


You can also switch your project from .net 3.5 to 3.0 which will default you back to system.xml. But that would get you away from using LINQ.

您还可以将项目从.net 3.5切换到3.0,这将默认您返回到system.xml。但这会让你远离使用LINQ。