Drew
Drew

Reputation: 1337

What is the correct way of writing typeof(Xml) for C# .NET?

I'm making a dictionary that maps .NET types to Sql Server types. I am using MSDN's "SQL Server Data Type Mappings" page to show me what the correct conversions are. At the bottom of the table it says the .NET framework type is Xml, and so I wrote:

{typeof (Xml), "xml"}

as a key-value pair of the dictionary. But Resharper has Xml in red and says it cannot resolve the symbol. What's the correct statement for typeof(Xml)? This is what the rest of my dictionary looks like and it doesn't have any problems:

private static readonly Dictionary<Type, string> SqlServerMap = new Dictionary<Type, string>
{
    {typeof (Int64), "bigint"},
    {typeof (Boolean), "bit"},
    {typeof (Byte[]), "binary"},
    {typeof (Double), "float"},
    {typeof (Int32), "int"},
    {typeof (Decimal), "decimal"},
    {typeof (Single), "real"},
    {typeof (Int16), "smallint"},
    {typeof (DateTime), "date"},
    {typeof (TimeSpan), "time"},
    {typeof (String), "nvarchar"},
    {typeof (Guid), "uniqueidentifier"},
    {typeof (Byte), "tinyint"},
    {typeof (Xml), "xml"}
};

Upvotes: 2

Views: 660

Answers (1)

Mitchel Sellers
Mitchel Sellers

Reputation: 63136

TO make the mapping from .NET to SQL for XML it appears that you need to use the SqlXml type, as there is no direct Xml type.

Here is a detailed implementation example from MSDN.

When it comes to mapping you can use something like XDocument etc if you want and then manually swap it to a SqlXml for persistence possibly?

Upvotes: 2

Related Questions