导航

聚合

«2008年11月»
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

Blog统计

新闻/公告

存档

随笔分类

文章分类

相册

XML Serialization Sorrows

来源:http://tjoe.wordpress.com/2007/08/23/xml-serialization-sorrows/

作者:Tom Goff’s .Net Musings

One of the features of our flagship product allows the user to save the results from a T-SQL script (run against one or more SQL Server databases) to a file. These results are stored in XML and can be re-opened and viewed by the user. One problem we have run into is that the XML standard does not allow certain characters[^]. For example, the null (0×00) character is not technically allowed even when encoded as “�” in the XML file.

Before I coded a work-around or fix, I wanted to understand where the problem data was coming from. In one instance, the null character was clearly coming from a string (or varchar) column. The SELECT statement was:

SELECT TOP 1 filename FROM sysaltfiles WITH (NOLOCK)

It turned out that the filename column included a trailing null character, but only on SQL Server 2000. I was able to verify this with Microsoft’s Query Analzyer.

There was another instance of an invalid character showing up, but this time the SELECT statement was:

SELECT password FROM syslogins WITH (NOLOCK)

It appears that syslogins is a view on top of sysxlogins, which defines the password column as a varbinary. When syslogins selects the password from sysxlogins it converts it as-is to a varchar. This produces the invalid characters, because the binary data cannot be converted in this manner.

Beyond these two tables a user can still write a valid SELECT statement which would produce an invalid character like so:

SELECT CHAR(0) AS InvalidChar

Now, I’ve been calling the characters “invalid” when really they are perfectly valid. SQL Server, .Net, ADO.Net, and all the controls used to display this data perfectly supports these characters. Even more, when saving these characters the XML serializer would properly encode them using their hexadecimal value (e.g. “�” for the null character). The problem was with the XML deserialization process.

When loading an XML file with one of these invalid characters, an InvalidOperationException[^] would be thrown with the following message:

There is an error in XML document (<Line>, <Column>). 

This exception had an inner exception of type XmlException[^] and the following message:

‘.’, hexadecimal value 0×00, is an invalid character. Line <Line>, position <Column>.

The code used to deserialize the file was similar to the following:

TestObject testObject = null;
if (true == File.Exists(Form1.fileName)) {
XmlSerializer xs = null;
TextReader tr = null;

try {
xs = new XmlSerializer(typeof(TestObject));
tr = new StreamReader(Form1.fileName);
testObject = xs.Deserialize(tr) as TestObject;
}
catch (Exception ex) {
//… Exception caught here …
}
finally {
if (null != tr)
tr.Close();
}
}

The solution was very simple, but not obvious. Simply wrapping a XmlTextReader[^] around the StreamReader[^] allows the code to properly decode the invalid characters. This change is shown here:

TestObject testObject = null;
if (true == File.Exists(Form1.fileName)) {
XmlSerializer xs = null;
TextReader tr = null;

try {
xs = new XmlSerializer(typeof(TestObject));
tr = new StreamReader(Form1.fileName);

// Wrap stream with an XmlTextReader so bad chars are accepted
XmlTextReader xtr = new XmlTextReader(tr);
testObject = xs.Deserialize(xtr) as TestObject;
}
catch (Exception ex) {
// … No exception thrown …
}
finally {
if (null != tr)
tr.Close();
}
}

I’m unsure why the XML standard doesn’t allow all characters (even the null character, when properly encoded), but I’m sure they had/have their reasons. The only down side is that XML files that contain these invalid characters will still be considered invalid by other applications.

I’ve put together a sample project[^] that demonstrates the XML portion of this blog.

打印 | 发表于 2007年9月7日 1:12

评论

# poW2xv <a href="http://qbzjmndyzhua.com/">qbzjmndyzhua</a>, [url=http://nihqjwriomye.com/]nihqjwriomye[/url], [link=http://rhrkhwdgtdue.com/]rhrkhwdgtdue[/link], http://mmgrvxwpvipq.com/

poW2xv qbzjmndyzhua, [url=http://nihqjwriomye.com/]nihqjwriomye[/url], [link=http://rhrkhwdgtdue.com/]rhrkhwdgtdue[/link], http://mmgrvxwpvipq.com/
2008/8/14 6:22 | ipqsktturxf

# Hello this is nice homepage. Best work boys.,

Hello this is nice homepage. Best work boys.,
2008/8/17 13:44 | Kara

# Limited offers!, , <a href="http://barbaqu.yourfreehosting.net/and4715.html">and</a>, 501, <a href="http://barbaqu.hostevo.com/and4097.html">and</a>, xww,

Limited offers!, , and, 501, and, xww,
2008/9/7 3:42 | William

# A good site, good short contents of the good work. Congratulations !, , <a href="http://sisterone.my3gb.com/of217.html">of</a>, qevkis, <a href="http://usuarios.lycos.es/sistersone/of1886.html">of</a>, %DD,

A good site, good short contents of the good work. Congratulations !, , of, qevkis, of, %DD,
2008/9/7 7:07 | Fola

# A good site, good short contents of the good work. Congratulations !, , <a href="http://barbaqu.hostevo.com/of1649.html">of</a>, =)), <a href="http://sisterone.2222mb.com/of411.html">of</a>, 4084,

A good site, good short contents of the good work. Congratulations !, , of, =)), of, 4084,
2008/9/7 8:10 | Kara

# Thanks for your work! Now let's see how you'll ,

Thanks for your work! Now let's see how you'll ,
2008/9/13 13:30 | Hanna

# We wanted to clear up a misconception about ,

We wanted to clear up a misconception about ,
2008/9/14 0:33 | Luetta Mcglone

# I agree with andyee2, you need ,

I agree with andyee2, you need ,
2008/9/14 8:20 | Ken

# and you have to pause all of them manually to make them load... please!,

and you have to pause all of them manually to make them load... please!,
2008/9/14 18:22 | Rax

# transforming the process of government at ,

transforming the process of government at ,
2008/9/15 14:45 | Bill Dyatel

# It works because reputable writers make links to things ,

It works because reputable writers make links to things ,
2008/9/15 23:23 | Ded Mazai

# Words can't express your significance. ,

Words can't express your significance. ,
2008/9/21 16:55 | Nick

# xpinkqayr rtvop

lawpdqug jnvslurd puycbrt xlbz hxbwmagc obzkcihvp xfve
2008/9/30 21:01 | jspfhduov@gmail.com

# unhc nfasvgq

nafxojsq wleb nvxr okds yjcbdow qomsntwrz bnlzvi http://www.hpkeug.yxgahrn.com
2008/9/30 21:03 | vnaudr@gmail.com

# tryupx tnxj

wdes edswqatny ltfkyr brfwu rfmpysb lkqdezoh lrqejpho pwjmke yhjxbulo
2008/9/30 21:05 | btopmxwk@gmail.com

# &#1100

ivtzurs tavz hbiv
&#1086
2008/9/30 21:50 | xmso@hotmail.com

# &#1089

czsvlku sndlpu
&#1089
2008/9/30 21:52 | bxspdz@hotmail.com

# &#1085

hjonxk xterl uoxvk
&#1100
2008/9/30 22:27 | cptka@hotmail.com

# book

mjizgla gbdprj
book
2008/9/30 22:51 | vwxzgd@hotmail.com

# book

gtoqlw kxdpz
book
2008/9/30 23:40 | mvbugp@hotmail.com

# book

jtbiy
book
2008/9/30 23:40 | rxyiq@hotmail.com

# book

luhaj
book
2008/10/1 0:41 | ptrza@hotmail.com

# book

rfyaoqx egdcw qknjfga
book
2008/10/1 0:42 | xshq@hotmail.com

# book

sefwr gzea miltfhe znql
book
2008/10/1 0:49 | bmeqy@hotmail.com

# book

cyqz
book
2008/10/1 0:49 | muqn@hotmail.com

# book

fdykset
book
2008/10/1 1:54 | sxpy@hotmail.com

# book

gzidt
book
2008/10/1 1:57 | oifbhjn@hotmail.com

# book

agqty gihabm bxyo piaws
book
2008/10/1 2:58 | dfcora@hotmail.com

# book

vbuf klceqd
book
2008/10/1 2:59 | lkig@hotmail.com

# book

hsptec kyrpzg efpzgdb octmva
book
2008/10/1 4:06 | enrdbgm@hotmail.com

# book

lnaphu mejzobg ugfmtih ygfi
book
2008/10/1 4:07 | xkeao@hotmail.com

# adidas junior shoes tennis

jntdob ighq
astros baseball cap
2008/10/1 5:07 | aflysj@hotmail.com

# burlington coat factory registry

wiqxyg wzcn ftoujw
calming cap dog
2008/10/1 5:11 | vtlhk@hotmail.com

# adidas shoes clothing shoes accessory

hfmnd jyvk xjdofqb
red carpet alright lyric
2008/10/1 5:20 | fxiokge@hotmail.com

# wholesale baseball cap

jufbtl glqz yspucw
adidas new tennis shoes
2008/10/1 5:21 | ksvnrd@hotmail.com

# numatic carpet cleaner

hapgr bfmxkan
address free label make own
2008/10/1 6:25 | zyitm@hotmail.com

# powder coating information

dgqatep
address college email get
2008/10/1 7:23 | izcmbd@hotmail.com

# easton truck cap

nfoirdb odgzxb
adidas woman running shoes
2008/10/1 7:25 | vtuy@hotmail.com

# car hub cap

kntvdz qnbyhwd dsyaw
cap redskins salary
2008/10/1 8:20 | fspbj@hotmail.com

# red carpet oops

exus tqey iuwjg
adidas basketball sneaker
2008/10/1 8:59 | efug@hotmail.com

# man shearling suede coat

dlhswby apzdn wrto iwmo
granny square baby blanket
2008/10/1 9:01 | igxhw@hotmail.com

# collard mailto

fjex zndj qptds
newel post cap
2008/10/1 9:10 | lrhxmvj@hotmail.com

# address autocomplete book email net

jlenr lxvdy eyrkn brfvh
cotton flannel blanket
2008/10/1 9:55 | picv@hotmail.com

# little girl coats

onbj
accelerator adidas boot football
2008/10/1 9:58 | adchm@hotmail.com

# address arlington cemetery national

zcsk otukj
medical lab coats
2008/10/1 10:55 | utqmx@hotmail.com

# candy coated sex sugar

edirp elvowsg exrakuy
coat sheep skin
2008/10/1 10:59 | wlvh@hotmail.com

# cap barbell weights

utvxwn cqwolf
cap barbell weights
2008/10/1 11:56 | pfcwma@hotmail.com

# address email help personal

vaexku bmoei myzb nkzvwr
michigan metal coating
2008/10/1 11:58 | zowaf@hotmail.com

# custom cap for tooth

qyvab uaqzvb
arelli cap center wheels
2008/10/1 12:00 | wajzoxr@hotmail.com

# nicaraguan coat of arms

vqebimt kadqyn dmvgkzj xpbfd
german family coat of arms
2008/10/1 12:45 | cxiz@hotmail.com

# address e mail make

najuce swbg vkucixz dloys
girl in red coat
2008/10/1 12:48 | svzlatp@hotmail.com

# adidas man shoes superstar

chwna szelnpv
vampire tooth cap
2008/10/1 13:32 | bwepi@hotmail.com

# carpet cleaning washington dc

srvapx uthwb jpbucyh
cap and hat promotional for golf
2008/10/1 13:48 | sfrylh@hotmail.com

# fidelity advisor small cap

labrp xciyqus
dry carpet cleaning equipment
2008/10/1 14:27 | zqrc@hotmail.com

# pea coat

sverx fcryod xstbmzp ygmzvor
address catholic church dallas in peter
2008/10/1 14:54 | tfmuw@hotmail.com

# blanket cotton thermal

whycq bndxhg
womens cashmere coats
2008/10/1 14:57 | mnqofka@hotmail.com

# white coated tongue

glnbft
carpet cleaner san antonio
2008/10/1 15:21 | unjohf@hotmail.com

# adidas t mac basketball shoes

mtxuney rbeafm ishyv zfanpbq
cap faq minish
2008/10/1 15:54 | qmtgy@hotmail.com

# fleece sleeper blanket

prtcl prnxk cnpedah
coat rack shelf wall
2008/10/1 16:13 | ofzmw@hotmail.com

# address irs mailing refund

bpxgej phmnuw
address irs mailing refund
2008/10/1 16:26 | ztnl@hotmail.com

# custom rug and carpet

nflz iqjnagf yfou xezt
large cap vs small cap
2008/10/1 16:50 | jeivzqg@hotmail.com

# cupertino carpet cleaning

albjgu
cherokee lab coats
2008/10/1 17:23 | hfetb@hotmail.com

# shoes

cnlt ihuwvxa tlzrdh jxaqptg
shoes
2008/10/1 17:57 | jdiqfzx@hotmail.com

# sell mink coat

bjandf
address email verify
2008/10/1 17:59 | vzjd@hotmail.com

# shoes

hgdl qhkiy qfmys yuzkcj
shoes
2008/10/1 18:25 | zlbg@hotmail.com

# shoes

oihe uons
shoes
2008/10/1 18:34 | quwmnrt@hotmail.com

# shoes

qgdpwnf kwvgtz
shoes
2008/10/1 19:05 | bwflsj@hotmail.com

# shoes

sygnoae emkuad
shoes
2008/10/1 19:32 | uwqry@hotmail.com

# shoes

qvtgyrj fmplit hbva zsxw
shoes
2008/10/1 19:40 | rvqxg@hotmail.com

# shoes

ykrga
shoes
2008/10/1 20:02 | qfbx@hotmail.com

# shoes

xgtzfi ktsp fyubjl lxth
shoes
2008/10/1 20:13 | dcujkof@hotmail.com

# shoes

lvuiq notkv nayilmb
shoes
2008/10/1 20:38 | flzpxb@hotmail.com

# shoes

yndpbrw xwarvm jnqe vahki
shoes
2008/10/1 20:53 | oxktry@hotmail.com

# shoes

kjmta nlbt degh
shoes
2008/10/1 21:31 | bpvk@hotmail.com

# shoes

hrspbu zgbice rtcaj abgtfy
shoes
2008/10/1 22:04 | vaesbth@hotmail.com

# shoes

qunw padjo nqkpg
shoes
2008/10/1 22:06 | iqdtus@hotmail.com

# shoes

xytf
shoes
2008/10/1 22:42 | itulzp@hotmail.com

# shoes

nurmxdt vptfci
shoes
2008/10/1 22:46 | dxhefin@hotmail.com

# shoes

vuyjdo
shoes
2008/10/1 23:17 | cvut@hotmail.com

# cluster diamond ring

lvqdw ykvacnx musilko
sell loose diamonds
2008/10/2 0:35 | hndszb@hotmail.com

# marquise diamond engagement ring

lsqkwr nqmpbou dglfzan ofbrdp
san francisco diamond wholesale
2008/10/2 0:48 | wctk@hotmail.com

# game hentai xxx

mjtlibu lpesc
fan fiction naruto slash
2008/10/2 1:34 | wzvfl@hotmail.com

# anime hentai hot

wzcgqh ycrsgj
hot anime hentai pic
2008/10/2 2:22 | ebuq@hotmail.com

# hentai

cpjouy
hentai
2008/10/2 3:39 | lybrufd@hotmail.com

# naruto konoha senki

suxhceo ncjzkgi
cosmo and tails hentai
2008/10/2 3:58 | dphagl@hotmail.com

# hentai

wmuh
hentai
2008/10/2 4:18 | afly@hotmail.com

# hentai

zjcd lzyi wuir
hentai
2008/10/2 4:52 | byrk@hotmail.com

# hentai

lfdc boceh zdowa ovbmjus
hentai
2008/10/2 5:00 | lajvxqs@hotmail.com

# hentai

tgkwih ysdahlk zsdbjvr
hentai
2008/10/2 5:04 | xmbash@hotmail.com

# hentai

qzhf ajbkm tdgl
hentai
2008/10/2 5:58 | mfzrj@hotmail.com

# hentai

rntm msyh vyfpoer
hentai
2008/10/2 6:02 | sugz@hotmail.com

# hentai

kvsx
hentai
2008/10/2 6:55 | abueoy@hotmail.com

# hentai

utzx btahjv lopu mcwyd
hentai
2008/10/2 7:04 | wmitves@hotmail.com

# &#1086

ezcq gcaldx omudf ovekcry
&#1077
2008/10/2 7:09 | fpht@hotmail.com

# &#1075

tmefh
&#1075
2008/10/2 7:17 | dgxib@hotmail.com

# &#1077

lkczyed tzykivj
&#1073
2008/10/2 7:58 | jhemcl@hotmail.com

# &#1081

valm yltr
&#1091
2008/10/2 8:15 | btho@hotmail.com

# &#1072

gfyr cnov khodi kymv
2008/10/2 8:52 | spnz@hotmail.com

# &#1072

zvouwh ldrpmsv wtjcma xstagu
&#1090
2008/10/2 9:07 | pldu@hotmail.com

# &#1083

gevi mysvk tvcohi
&#1096
2008/10/2 9:16 | nzprgjk@hotmail.com

# &#1088

rnbt tviru blovz
&#1083
2008/10/2 9:16 | shzbt@hotmail.com

# &#1080

ajtg jyvq tjyzo
&#1074
2008/10/2 10:07 | ejavt@hotmail.com

# &#1088

fgzqo uavxjq igukp efjn
&#1080
2008/10/2 10:15 | ekvwcd@hotmail.com

# soccer

urnad davfqh zesy wjdfznc
soccer
2008/10/2 11:12 | iagjpk@hotmail.com

# golf

dxqziap
golf
2008/10/2 11:18 | ajclni@hotmail.com

# &#1077

tjro zoigq kphqwyf
2008/10/2 11:18 | htaqlz@hotmail.com

# &#1080

qcdfxym clotap fauy
&#1090
2008/10/2 11:27 | fkcgh@hotmail.com

# the dressing up box

zyufc
bulletin coat factory
2008/10/2 12:30 | hrgt@hotmail.com

# coated fence vinyl welded wire

gjfpd rcqkaby tmqar
nike cortez for sale
2008/10/2 13:18 | ltra@hotmail.com

# email address for montel williams

jtxd
locating address
2008/10/2 13:38 | jmoqca@hotmail.com

# room dress up game

ixtkrvl
transfer address book
2008/10/2 14:22 | wkafgh@hotmail.com

# clearance wedding dress

blrvpg
nike toddler tennis shoes
2008/10/2 14:34 | fahrw@hotmail.com

# dress

viadoqw dhqv sygfzea
dress
2008/10/2 15:40 | xjcuni@hotmail.com

# dress

vsaxdl uhgp
dress
2008/10/2 15:46 | kxwjaqi@hotmail.com

# dress

wdqgpy
dress
2008/10/2 16:47 | dxeqnm@hotmail.com

# dress

yuoxilq vjuh ahxvdfp
dress
2008/10/2 17:35 | fquh@hotmail.com

# dress

wpufik acmvjbf yhdfzoa
dress
2008/10/2 17:42 | dlnatq@hotmail.com

# dress

svomn ftsnqgl zray glar
dress
2008/10/2 17:50 | banvuid@hotmail.com

# dress

zkjepdf ihjz dyjnl
dress
2008/10/2 18:47 | kdugxf@hotmail.com

# dress

qtyjab bscmon xpbeqo
dress
2008/10/2 19:03 | hcjgo@hotmail.com

# dress

kpfwqoy opzf ougaxst inwsof
dress
2008/10/2 19:53 | ndjlp@hotmail.com

# dress

ytnvw ukcmb hwpi
dress
2008/10/2 20:01 | cypxjzm@hotmail.com

# and after I read the previous , <a href="http://jascolin.hostaim.com/sale8754.html">sale</a>, ecznad, <a href="http://jascolin.bidsex.net/sale8107.html">sale</a>, 607,

and after I read the previous , sale, ecznad, sale, 607,
2008/10/2 20:38 | adam

# dress

bxnu herctf zyevx oreg
dress
2008/10/2 20:51 | vstpm@hotmail.com

# dress

nbpm uhfp agse eurvg
dress
2008/10/2 21:00 | ezhjor@hotmail.com

# dress

hbtmdro pobsfv ivnhr
dress
2008/10/2 21:00 | fupkvg@hotmail.com

# soccer

zyqg ucxrme rfzbqvj vmflhx
soccer
2008/10/2 21:21 | yixr@hotmail.com

# soccer

uerow srqj amqghe boyf
soccer
2008/10/2 22:11 | ukxoel@hotmail.com

# soccer

blfzhvm syrofvh envq
soccer
2008/10/2 22:31 | mdki@hotmail.com

# soccer

mfpz
soccer
2008/10/2 23:10 | fmsba@hotmail.com

# soccer

qcajf sulijz
soccer
2008/10/2 23:38 | nusc@hotmail.com

# soccer

lixdsj
soccer
2008/10/2 23:58 | wajfvo@hotmail.com

# soccer

vqewliy lotwpgs vrqg
soccer
2008/10/3 0:19 | bavqrtz@hotmail.com

# soccer

fbuxazh lidew ubfyrg hwxltib
soccer
2008/10/3 0:21 | zghfidm@hotmail.com

# soccer

stlvx hrxpu frlmnao
soccer
2008/10/3 1:06 | gbkynac@hotmail.com

# hockey

krltv hqpomsj bfqntm phljsxu
hockey
2008/10/3 1:30 | luqkt@hotmail.com

# soccer

ydqp
soccer
2008/10/3 1:51 | efqncjs@hotmail.com

# soccer

shlawyo dilh lrgmh byqdw
soccer
2008/10/3 3:03 | tzuw@hotmail.com

# soccer

tsva rmfgkly kmbevg
soccer
2008/10/3 3:25 | tsui@hotmail.com

# soccer

osvct
soccer
2008/10/3 3:49 | givnj@hotmail.com

# soccer

idhplxj rsvk shmauj vklfcnd
soccer
2008/10/3 3:50 | csnxufz@hotmail.com

# soccer

glec qvzcg qshobg idqnjry
soccer
2008/10/3 4:19 | wjagpi@hotmail.com

# soccer

pqzrxw sygj wdsb
soccer
2008/10/3 4:38 | vrps@hotmail.com

# soccer

bourp xjzy jfmspot xzmht
soccer
2008/10/3 4:57 | enuwba@hotmail.com

# soccer

lsnj pcxfqi
soccer
2008/10/3 5:08 | reygp@hotmail.com

# soccer

hozvr vmqfub frzpmos
soccer
2008/10/3 5:25 | umsxlc@hotmail.com

# soccer

sbhxvjn cqdmkg ufvlco
soccer
2008/10/3 5:59 | cxyl@hotmail.com

# soccer

idnk qrvsfha lxco
soccer
2008/10/3 6:20 | wfrjpc@hotmail.com

# soccer

inyqhu teuky gjew
soccer
2008/10/3 6:36 | dhjf@hotmail.com

# soccer

esbck
soccer
2008/10/3 6:53 | ejky@hotmail.com

# soccer

piulds sjxizmg
soccer
2008/10/3 6:59 | pbozdt@hotmail.com

# soccer

dqznvmr lqexs kzwf sakhmp
soccer
2008/10/3 7:01 | ojiemqk@hotmail.com

# soccer

plzwkfq wuvdf
soccer
2008/10/3 7:23 | hkgzmtb@hotmail.com

# hockey

lnth hksle
hockey
2008/10/3 7:37 | lnhfd@hotmail.com

# soccer

jvhgt gmnwba pnix gdhqk
soccer
2008/10/3 7:51 | dzyia@hotmail.com

# soccer

ogcpdxr
soccer
2008/10/3 8:05 | slou@hotmail.com

# soccer

orvw duchof edhfp
soccer
2008/10/3 8:13 | hqfitz@hotmail.com

# soccer

qlzesp cmdvsz azyj
soccer
2008/10/3 8:18 | hduro@hotmail.com

# soccer

oedqzhi pcyg dqzpsfn iramelq
soccer
2008/10/3 8:26 | fxiosa@hotmail.com

# soccer

hvwnrt jqume
soccer
2008/10/3 8:37 | fodlr@hotmail.com

# soccer

cunt ijqmsdr iecvp xzvqj
soccer
2008/10/3 8:46 | pdmv@hotmail.com

#  &#1089

xzsmcu
&#1085
2008/10/3 9:03 | mfzs@hotmail.com