C# SQL Server XML 数据类型在 .NET 中转换为什么,如何将其转换为 XmlDocument?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13690991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:26:31  来源:igfitidea点击:

What does the SQL Server XML datatype translate to in .NET and how do I convert it to XmlDocument?

c#.netsql-server-2005

提问by Sinaesthetic

We have a column in the database that has a type of xml. I am reading this information via the .net SqlDataReader, but I'm not sure what to cast it to.

我们在数据库中有一个类型为xml. 我正在通过 .net 阅读此信息SqlDataReader,但我不确定要将其转换为什么。

The msdn table (http://msdn.microsoft.com/en-us/library/cc716729.aspx) suggests that is of the .net type Xml, but there is no System.Xml, only System.Web.UI.WebControls.Xmlso I'm not sure if that is correct.

msdn 表 ( http://msdn.microsoft.com/en-us/library/cc716729.aspx) 表明它是 .net 类型Xml,但没有System.Xml,只是System.Web.UI.WebControls.Xml所以我不确定这是否正确。

So my question is this:

所以我的问题是:

What do I cast the SqlDbType.Xmlto as I'm reading it from a SqlDataReader, and how do I convert that to XmlDocument?

我怎么投的SqlDbType.Xml作为。我现在是读它SqlDataReader,并且我怎么将其转换成XmlDocument

采纳答案by Wiktor Zychla

I remember casting it to a string. Feeding XmlDocument with a string works as usual then.

我记得把它转换成一个字符串。然后像往常一样使用字符串提供 XmlDocument。

回答by Jared Harley

Sure, there's a System.Xml namespace:

当然,有一个System.Xml 命名空间

The System.Xml namespace provides standards-based support for processing XML.

System.Xml 命名空间为处理 XML 提供基于标准的支持。

To use it, though, you'll probably have to add it as a reference in your project. Microsoft has instructions for doing this in Visual Studio.

但是,要使用它,您可能必须将其添加为项目中的参考。Microsoft 有在 Visual Studio 中执行此操作的说明

回答by Serge Belov

It translates to SqlXmland you can get an XmlReaderwith SqlXml.CreateReaderfrom it. You'd have to use SqlDataReader.GetSqlXmlmethod to get the type instead of a string.

它翻译SqlXml,你可以得到一个XmlReaderSqlXml.CreateReader来自它。您必须使用 SqlDataReader.GetSqlXml方法来获取类型而不是字符串。

For example:

例如:

        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            SqlXml xmlData =
            reader.GetSqlXml(0);
            XmlReader xmlReader = xmlData.CreateReader();

            xmlReader.MoveToContent();
            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element)
                {
                    string elementName = xmlReader.LocalName;
                    xmlReader.Read();
                    Console.WriteLine(elementName + ": " + xmlReader.Value);
                }
            }
        }

UPDATE:To answer a helpful comment from @Wiktor Zychla

更新:回答来自@Wiktor Zychla 的有用评论

The performance of this approach is better and can be a lotbetter when dealing with large XML fields because SqlReader.GetStringwill load the field content into a string first while SqlReader.GetSqlXmlcreates an XmlReader from the stream directly. That can be quickly verified with a look at System.Data in Reflector or a similar tool.

这种方法的性能更好,可以有很多更好地与大XML领域打交道时,因为SqlReader.GetString同时会首先加载字段内容转换成字符串SqlReader.GetSqlXml创建直接从流一个XmlReader。这可以通过查看 Reflector 中的 System.Data 或类似工具来快速验证。

回答by sa_ddam213

I use this method myself, using SqlCommand.ExecuteXmlReader();

我自己使用这种方法,使用 SqlCommand.ExecuteXmlReader();

XmlDocument xdoc = new XmlDocument();
using (SqlCommand command = new SqlCommand(queryString, connection))
{
    XmlReader reader = command.ExecuteXmlReader();
    if (reader.Read())
    {
        xdoc.Load(reader);
    }
}