使用XmlSerializer为SQL Server 2005创建Xml文档(具有日期类型属性) [英] Using XmlSerializer to create an Xml document for SQL Server 2005 (having a date typed attribute)

查看:61
本文介绍了使用XmlSerializer为SQL Server 2005创建Xml文档(具有日期类型属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


我有一个xml架构,它有一个日期类型属性。我使用xsd.exe

为XmlSerializer创建一个类库。

XmlSerializer.Serialize(...)的结果应作为SqlCommand参数

的值传递,以便将xml文档插入表的一列其中

列的类型是相同的xml架构。


这听起来很简单,但SQL Server需要指定时区

表示日期值。如何强制XmlSerializer.Serialize(...)包含日期类型属性的

时区(XmlSerializer.Serialize(...)by

默认不包括最佳问候,

Henrik Dahl

Hello!

I have an xml schema which has a date typed attribute. I have used xsd.exe
to create a class library for XmlSerializer. The result of
XmlSerializer.Serialize(...) should be passed as the value for the parameter
of an SqlCommand for inserting the xml document in a column of a table where
the column is typed to be of the same xml schema.

This all sounds simple, but SQL Server REQUIRES the timezone to be specified
for date values. How to force XmlSerializer.Serialize(...) to include the
timezone of the date typed attribute (XmlSerializer.Serialize(...) by
default does not include the timezone)?
Best regards,

Henrik Dahl

推荐答案

您好Henrik,


从您的描述中,您有一个自定义类(通过xsd工具从给定的XML

架构生成)需要进行xml序列化。自定义类

包含一个DateTime成员,该成员将被序列化为xml属性。

但是,您发现序列化日期值始终缺少时区

offset,你想知道如何强制xmlserizer输出时区偏移,

正确吗?


根据我的理解,在.net 2.0,Datetime类已经增强了
。首先,它支持两种DateTime实例


**本地日期时间对象


** UTC日期时间对象

这可以在创建时指定,例如


DateTime dt = new DateTime(DateTime.Now.Ticks,DateTimeKind.Local);


对于本地DateTime实例,xmlserlizer将序列化它包含

时区偏移量。而对于UTC日期时间实例,它不会包含时区偏移量。例如


以下代码片段会给出类似的输出;


===== code =====

StringWriter sw = new StringWriter();


DateTime date1 = DateTime.Now;

DateTime date2 = DateTime.UtcNow;


XmlSerializer xs = new XmlSerializer(typeof(DateTime));

xs.Serialize(sw,date1);


xs .Serialize(sw,date2);


textBox1.Text = sw.ToString();


sw.Close();

=======================

=========输出========

<?xml version =" 1.0" encoding =" utf-16"?>

< dateTime> 2006-12-11T21:17:01.7868744 + 08:00< / dateTime><?xml version =" 1.0" ;

encoding =" utf-16"?>

< dateTime> 2006-12-11T13:17:01.7868744Z< / dateTime>


=====================


所以你可以找到,如果你创建一个本地日期时间对象,您将获得带有时区偏移量的

xmlserlization输出。您是否认为强制您的班级持有本地日期时间实例是可能的?(您可以通过DateTime.Kind属性检查




如果在您的方案中不可行,您可以考虑创建一个自定义的

包装类来表示日期时间,您可以实现我们自己的

XmlSerialization逻辑通过IXmlSerizable接口。


希望这会有所帮助。


此致,


Steven Cheng


Microsoft MSDN在线支持主管

该帖子按原样提供。没有保证,也没有授予任何权利。

Hello Henrik,

From your description, you have a custom class(generate from a given XML
schema through xsd tool) that need to be xml serialized. The custom class
contains a DateTime member which will be serialized as an xml attribute.
However, you found the serialized date value is always lack of the timezone
offset, you''re wondering how to force xmlserizer to output timezone offset,
correct?

Based on my understanding, in .net 2.0, the Datetime class has been
enhanced. first, it support two kinds of DateTime instance

** Local datetime object

**UTC datetime object

this can be specified at the creation time, e.g.

DateTime dt = new DateTime(DateTime.Now.Ticks, DateTimeKind.Local);

And for local DateTime instance, the xmlserlizer will serialize it with
timezone offset included. While for UTC datetime instance, it will not
contain the timezone offset. e.g

the following code snippet will given the output like;

=====code =====
StringWriter sw = new StringWriter();

DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.UtcNow;

XmlSerializer xs = new XmlSerializer(typeof(DateTime));
xs.Serialize(sw, date1);

xs.Serialize(sw, date2);

textBox1.Text = sw.ToString();

sw.Close();
=======================

=========output========
<?xml version="1.0" encoding="utf-16"?>
<dateTime>2006-12-11T21:17:01.7868744+08:00</dateTime><?xml version="1.0"
encoding="utf-16"?>
<dateTime>2006-12-11T13:17:01.7868744Z</dateTime>

=====================

So you can find that if you create a local datetime object, you''ll get the
xmlserlization output with the timezone offset. Do you think it is possible
that you force your class to hold a local datetime instance?(you can check
it through DateTime.Kind property)

If this is not doable in your scenario, you may consider create a custom
wrapper class to represent datetime and you can implement our own
XmlSerialization logic through IXmlSerizable interface.

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.


Mhr。 Cheng,


非常感谢您的回复。不幸的是,我认为.NET框架中存在

错误。你的例子非常好,但是有一个缺点就是
。 xsd类型应为xs:date而不是xs:dateTime。试着把你的两个日期分成一个类,让我们把它命名为CDates。当你XmlSerialize这个类的

实例时,它仍然可以很好地工作,即时区被放置。

现在添加属性

[ System.Xml.Serialization.XmlAttributeAttribute(Dat aType =" date")]"到两个DateTime字段的每个
,然后再试一次。现在xml文档中没有指定时区




看起来这不是.NET框架中的错误,即它不是把上面提到的属性

装饰的DateTime值序列化时

时区?

祝你好运,


Henrik Dahl

Steven Cheng [MSFT]" < st ***** @ online.microsoft.comskrev i en meddelelse

news:Rb ************** @ TK2MSFTNGHUB02.phx.gbl .. 。
Mhr. Cheng,

Thank you very much for your response. Unfortunately I think there''s an
error in the .NET framework. Your example works excellent, however there''s
one drawback. The xsd type should be xs:date not xs:dateTime. Try to put
your two dates into a class, let''s name it CDates. When you XmlSerialize an
instance of this class it still works excellently, i.e. the timezone is put.
Now add the attribute
"[System.Xml.Serialization.XmlAttributeAttribute(Dat aType="date")]" to each
of the two DateTime fields and try again. Now the timezone is NOT specified
in the xml document.

Doesn''t that look as an error in the .NET framework, i.e. it does not put
the timezone when serializing DateTime values decorated with the attributed
mentioned above?
Best regards,

Henrik Dahl
"Steven Cheng[MSFT]" <st*****@online.microsoft.comskrev i en meddelelse
news:Rb**************@TK2MSFTNGHUB02.phx.gbl...

Hello Henrik,


从您的描述中,您有一个自定义类(从给定的XML生成

架构通过xsd工具)需要进行xml序列化。自定义类

包含一个DateTime成员,该成员将被序列化为xml属性。

但是,您发现序列化日期值始终缺少

timezone

抵消,你想知道如何强制xmlserizer输出时区

抵消,

正确吗?


根据我的理解,在.net 2.0中,Datetime类已经增强了
。首先,它支持两种DateTime实例


**本地日期时间对象


** UTC日期时间对象

这可以在创建时指定,例如


DateTime dt = new DateTime(DateTime.Now.Ticks,DateTimeKind.Local);


对于本地DateTime实例,xmlserlizer将序列化它包含

时区偏移量。而对于UTC日期时间实例,它不会包含时区偏移量。例如


以下代码片段会给出类似的输出;


===== code =====

StringWriter sw = new StringWriter();


DateTime date1 = DateTime.Now;

DateTime date2 = DateTime.UtcNow;


XmlSerializer xs = new XmlSerializer(typeof(DateTime));

xs.Serialize(sw,date1);


xs .Serialize(sw,date2);


textBox1.Text = sw.ToString();


sw.Close();

=======================

=========输出========

<?xml version =" 1.0" encoding =" utf-16"?>

< dateTime> 2006-12-11T21:17:01.7868744 + 08:00< / dateTime><?xml version =" 1.0" ;

encoding =" utf-16"?>

< dateTime> 2006-12-11T13:17:01.7868744Z< / dateTime>


=====================


所以你可以找到,如果你创建一个本地日期时间对象,您将获得带有时区偏移量的

xmlserlization输出。你认为你强迫你的班级持有一个本地日期时间实例吗?(你可以通过DateTime来检查它是否为
)。好的属性)


如果在您的场景中不可行,您可以考虑创建一个自定义的

包装类来表示日期时间,您可以实现我们自己的

通过IXmlSerizable接口的XmlSerialization逻辑。


希望这会有所帮助。


此致,


Steven Cheng


Microsoft MSDN在线支持主管


此帖子按原样提供。没有保证,并且不授予

权利。
Hello Henrik,

From your description, you have a custom class(generate from a given XML
schema through xsd tool) that need to be xml serialized. The custom class
contains a DateTime member which will be serialized as an xml attribute.
However, you found the serialized date value is always lack of the
timezone
offset, you''re wondering how to force xmlserizer to output timezone
offset,
correct?

Based on my understanding, in .net 2.0, the Datetime class has been
enhanced. first, it support two kinds of DateTime instance

** Local datetime object

**UTC datetime object

this can be specified at the creation time, e.g.

DateTime dt = new DateTime(DateTime.Now.Ticks, DateTimeKind.Local);

And for local DateTime instance, the xmlserlizer will serialize it with
timezone offset included. While for UTC datetime instance, it will not
contain the timezone offset. e.g

the following code snippet will given the output like;

=====code =====
StringWriter sw = new StringWriter();

DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.UtcNow;

XmlSerializer xs = new XmlSerializer(typeof(DateTime));
xs.Serialize(sw, date1);

xs.Serialize(sw, date2);

textBox1.Text = sw.ToString();

sw.Close();
=======================

=========output========
<?xml version="1.0" encoding="utf-16"?>
<dateTime>2006-12-11T21:17:01.7868744+08:00</dateTime><?xml version="1.0"
encoding="utf-16"?>
<dateTime>2006-12-11T13:17:01.7868744Z</dateTime>

=====================

So you can find that if you create a local datetime object, you''ll get the
xmlserlization output with the timezone offset. Do you think it is
possible
that you force your class to hold a local datetime instance?(you can check
it through DateTime.Kind property)

If this is not doable in your scenario, you may consider create a custom
wrapper class to represent datetime and you can implement our own
XmlSerialization logic through IXmlSerizable interface.

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.



.Net框架中不是错误,查看日期的定义'

& XSD规范中的dateTime:

http://www.w3schools.com/schema/schema_dtypes_date.asp

HTH


Ollie Riches


" Henrik Dahl" < He ******** @ community.nospamwrote in message

news:eU ************* @ TK2MSFTNGP02.phx.gbl ...
It is not an error in the .Net framework, check out the definition of ''Date''
& ''dateTime'' in the XSD specification:

http://www.w3schools.com/schema/schema_dtypes_date.asp

HTH

Ollie Riches

"Henrik Dahl" <He********@community.nospamwrote in message
news:eU*************@TK2MSFTNGP02.phx.gbl...

Mhr。 Cheng,


非常感谢您的回复。不幸的是,我认为.NET框架中存在

错误。你的例子非常好,但是有一个缺点就是
。 xsd类型应为xs:date而不是xs:dateTime。试着把你的两个日期分成一个类,让我们把它命名为CDates。当你XmlSerialize

这个类的一个实例时它仍然可以很好地工作,即时区是

put。现在添加属性

" [System.Xml.Serialization.XmlAttributeAttribute(Dat aType =" date")]"

两个DateTime字段中的每一个,然后重试。现在时区不是在xml文档中指定的



看起来不是.NET框架中的错误,即它不是把

归因于上面提到的日期时间值序列化时,把

的时区设为时区?


祝你好运,


Henrik Dahl


" Steven Cheng [MSFT]" < st ***** @ online.microsoft.comskrev i en meddelelse

news:Rb ************** @ TK2MSFTNGHUB02.phx.gbl .. 。
Mhr. Cheng,

Thank you very much for your response. Unfortunately I think there''s an
error in the .NET framework. Your example works excellent, however there''s
one drawback. The xsd type should be xs:date not xs:dateTime. Try to put
your two dates into a class, let''s name it CDates. When you XmlSerialize
an instance of this class it still works excellently, i.e. the timezone is
put. Now add the attribute
"[System.Xml.Serialization.XmlAttributeAttribute(Dat aType="date")]" to
each of the two DateTime fields and try again. Now the timezone is NOT
specified in the xml document.

Doesn''t that look as an error in the .NET framework, i.e. it does not put
the timezone when serializing DateTime values decorated with the
attributed mentioned above?
Best regards,

Henrik Dahl
"Steven Cheng[MSFT]" <st*****@online.microsoft.comskrev i en meddelelse
news:Rb**************@TK2MSFTNGHUB02.phx.gbl...

> Hello Henrik,

从您的描述中,您有一个自定义类(从给定的XML
模式生成)通过xsd工具)需要进行xml序列化。自定义类
包含一个DateTime成员,它将被序列化为xml属性。
但是,你发现序列化日期值总是缺少
时区
偏移,你' '想知道如何强制xmlserizer输出时区
偏移,
正确吗?

根据我的理解,在.net 2.0中,Datetime类已经被增强了。首先,它支持两种DateTime实例

**本地日期时间对象

** UTC日期时间对象

这可以在创建时指定时间,例如

DateTime dt = new DateTime(DateTime.Now.Ticks,DateTimeKind.Local);

对于本地DateTime实例,xmlserlizer将使用
下面的代码片段会给出类似的输出;

===== code =====
StringWriter sw = new StringWriter();

DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.UtcNow;

XmlSerializer xs = new XmlSerializer(typeof(DateTime));
xs.Serialize(sw,date1);

xs.Serialize(sw,date2);

textBox1.Text = sw.ToString();

sw.Close();
=======================

======= ==输出========
<?xml version =" 1.0" encoding =" utf-16"?>
< dateTime> 2006-12-11T21:17:01.7868744 + 08:00< / dateTime><?xml version =" 1.0"
encoding =" utf-16"?>
< dateTime> 2006-12-11T13:17:01.7868744Z< / dateTime>

======== =============

所以你可以发现,如果你创建一个本地日期时间对象,你会得到
xmlserlization输出与时区偏移。您是否认为可以强制您的班级持有本地日期时间实例?(您可以通过DateTime.Kind属性来检查它)
<如果在您的场景中不可行,您可以考虑创建一个自定义的
包装类来表示日期时间,您可以通过IXmlSerizable接口实现我们自己的XmlSerialization逻辑。
希望这会有所帮助。

真诚的,

Steven Cheng

微软MSDN在线支持主管

这个帖子是提供按原样没有保证,也没有授予
权利。
>Hello Henrik,

From your description, you have a custom class(generate from a given XML
schema through xsd tool) that need to be xml serialized. The custom class
contains a DateTime member which will be serialized as an xml attribute.
However, you found the serialized date value is always lack of the
timezone
offset, you''re wondering how to force xmlserizer to output timezone
offset,
correct?

Based on my understanding, in .net 2.0, the Datetime class has been
enhanced. first, it support two kinds of DateTime instance

** Local datetime object

**UTC datetime object

this can be specified at the creation time, e.g.

DateTime dt = new DateTime(DateTime.Now.Ticks, DateTimeKind.Local);

And for local DateTime instance, the xmlserlizer will serialize it with
timezone offset included. While for UTC datetime instance, it will not
contain the timezone offset. e.g

the following code snippet will given the output like;

=====code =====
StringWriter sw = new StringWriter();

DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.UtcNow;

XmlSerializer xs = new XmlSerializer(typeof(DateTime));
xs.Serialize(sw, date1);

xs.Serialize(sw, date2);

textBox1.Text = sw.ToString();

sw.Close();
=======================

=========output========
<?xml version="1.0" encoding="utf-16"?>
<dateTime>2006-12-11T21:17:01.7868744+08:00</dateTime><?xml version="1.0"
encoding="utf-16"?>
<dateTime>2006-12-11T13:17:01.7868744Z</dateTime>

=====================

So you can find that if you create a local datetime object, you''ll get
the
xmlserlization output with the timezone offset. Do you think it is
possible
that you force your class to hold a local datetime instance?(you can
check
it through DateTime.Kind property)

If this is not doable in your scenario, you may consider create a custom
wrapper class to represent datetime and you can implement our own
XmlSerialization logic through IXmlSerizable interface.

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.




这篇关于使用XmlSerializer为SQL Server 2005创建Xml文档(具有日期类型属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆