将2个对象序列化为相同的xml文件? [英] serialize 2 objects to same xml file?

查看:69
本文介绍了将2个对象序列化为相同的xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以添加(追加)到已经包含

序列化对象的xml文件,并且能够从同一文件反序列化为
或两个对象... ???怎么这个

完成...... ??


谢谢,


vince

解决方案
乍得,


感谢您的信息,想你的方法,并得到双方

对象序列化到同一个文件,xml看起来很好。


然而,当试图反序列化第一个对象时

在文件中,我收到错误,xml中有错误

文档(7,1)...第7行是第二行的开头

序列化对象。


在查看以前版本的xml文件时,

只包含第一个对象,xml对于

新对象xml文件中的对象...


似乎Deserialize()方法窒息

事实上,有多个对象可以反映出来。


是否有一步我忘了某个地方......?


感谢您的帮助...


vince

-----原始消息-----

vince < VL ****** @ sdcera.org>在消息中写道
新闻:00 **************************** @ phx.gbl ...

我可以添加(追加)到已经
包含序列化对象的xml文件,并且能够反序列化为
或来自同一文件的两个对象...... ???这是怎么做的???



创建一个XmlTextWriter(我们称之为xw)
创建两个用每种类型初始化的XmlSerializer
和您希望序列化的实例。 (我们称之为
1和2)

1.Serialize(xw);
2.Serialize(xw);

只有catch,你必须以相同的顺序反序列化它们,或者记住1的长度。如果你想要反序列化两个,你必须创建一个
XmlTextReader并前进到
XML中2开始的位置,然后调用:

2。反序列化(xr);

-c



实际上,如果序列化2个对象对于XML,你会

有两个顶级标签,这些标签都是无效的XML。

我只是想到了这个,对不起。

您可能必须拥有一个根XML节点并在其下面添加

序列化节点。


您可能会做的事情是这样的:


< root>

< object num =" 1">

(此处为序列化信息)

< / object>

< object num =" 2">

(此处为序列化信息)

将/对象>

将/根>


打开XML在一个XmlDocument和做:


XmlNodeList objectNodes = doc.SelectNodes(" // object");


Foo [] foos = new Foo [objectNodes.Count];


int fooCtr = 0;


foreach(objectNodes中的XmlNode objectNode)

{

XmlSerializer ser = new XmlSerializer(typeof(Foo));


foos [fooCtr ++] =(Foo)ser.Deserialize(

new StringReader(objectNode.InnerXml));

}


-c

" vince" < VL ****** @ sdcera.org>在消息中写道

news:09 **************************** @ phx.gbl ... < blockquote class =post_quotes> Chad,

感谢您的信息,尝试了您的方法,并将两个
对象序列化到同一个文件,并且xml看起来很好 - 但是,当试图反序列化文件中的第一个对象时,我收到一个错误,xml中有一个错误
文档(7,1) ...第7行是第二个序列化对象的开头。

在查看以前版本的xml文件时,只包含第一个对象, xml与新的2个对象xml文件中的对象相同...

似乎Deserialize()方法窒息了
这个事实还有更多比一个对象要反序列化......

是否有一步我忘了某个地方...... ??

感谢您的帮助.. 。

vince

-----原帖-----

vince < VL ****** @ sdcera.org>在消息中写道
新闻:00 **************************** @ phx.gbl ...

我可以添加(追加)到已经包含序列化对象的xml文件,并且能够反序列化为
或来自同一文件的两个对象...... ???这是怎么做的???



创建一个XmlTextWriter(我们称之为xw)
创建两个用每种类型初始化的XmlSerializer
和您希望序列化的实例。 (我们称之为
1和2)

1.Serialize(xw);
2.Serialize(xw);

只有catch,你必须以相同的顺序反序列化它们,或者记住1的长度。如果你想要反序列化两个,你必须创建一个
XmlTextReader并前进到
XML中2开始的位置,然后调用:

2。反序列化(xr);

-c



Chad,


再次感谢...所以,澄清一下,有两个顶级标签的

问题的解决方法是手动解析

创建的xml文件... ???

对xml进行排序,这样我就得拿你的代码片段来支付
和研究一下...


看起来你加载了一个中间数组,其中包含两个你已经解析过的对象数据的


原始2对象xml文件...这是正确的吗?


感谢您的帮助,


vince

-----原始消息-----
实际上,如果你将2个对象序列化为XML,你将有两个顶级 - 等级标签whi ch将是无效的XML。
我只是想到了,抱歉。

您可能必须拥有一个根XML节点并在其下添加
序列化节点。

您可能会做的事情如下:

< root>
< object num =" 1">
(此处序列化信息)
< / object>
< object num =" 2">
(此处序列化信息)
< / object>
< / root> ;

在XmlDocument中打开XML并执行:

XmlNodeList objectNodes = doc.SelectNodes(" // object");

Foo [] foos = new Foo [objectNodes.Count];

int fooCtr = 0;

foreach(objectNodes中的XmlNode objectNode)
{/ / XmlSerializer ser = new XmlSerializer(typeof(Foo));

foos [fooCtr ++] =(Foo)ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c

" vince" < VL ****** @ sdcera.org>在消息中写道
新闻:09 **************************** @ phx.gbl ...

乍得,

感谢您的信息,尝试了您的方法并将两个
对象序列化到同一个文件,并且xml
看起来格式正确。但是,当试图反序列化文件中的第一个
对象时,我收到一个错误,xml
文档(7,1)中有错误。第7行是第二个序列化对象的开头。

在查看以前只包含第一个对象的xml文件版本时,xml是相同的$新的2个对象xml文件中该对象的b $ b ...

看起来Deserialize()方法因为有多个对象而窒息

反序列化...

是否有一步我忘了某个地方...... ??

感谢您的帮助......
vince

> -----原始消息-----
>
>" vince" < VL ****** @ sdcera.org>在消息中写道
>新闻:00 **************************** @ phx.gbl ...
>>我可以添加(追加)到已经


包含

>>的xml文件吗?序列化对象,并能够反序列化为
>>或来自同一文件的两个对象...... ???这是怎么回事
>>完成...... ??
>
>创建一个XmlTextWriter(我们称之为xw)
>创建两个用每种类型初始化的XmlSerializer
>和您希望序列化的实例。 (我们称之为
> 1和2)
>
> 1.Serialize(xw);
> 2.Serialize(xw);
>
>唯一的问题是,你必须在
>相同的顺序中反序列化它们,或者记住1的长度。如果你想要反序列化两个,你必须创建一个
> XmlTextReader并前进到
> XML中的位置,然后调用:
>
> 2.Deserialize(XR);
>
> -c
>
>
> <无线电通信/>>





Can I add (append) to an xml file that already contains a
serialized object, and be able to deserialize to either
or both objects from the same file...??? How is this
done...??

thanks,

vince

解决方案

Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml looks
well-formed.

However, when attempting to deserialize the first object
in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical for
that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking on
the fact that there is more than one object to
deserialize...

Is there a step that I''m forgetting somewhere...??

thanks for any help...

vince

-----Original Message-----

"vince" <vl******@sdcera.org> wrote in message
news:00****************************@phx.gbl...

Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either
or both objects from the same file...??? How is this
done...??



Create an XmlTextWriter (we''ll call him xw)
Create Two XmlSerializers initialized with each type
and instance you wish to serialize. (we''ll call them
1 and 2)

1.Serialize(xw);
2.Serialize(xw);

The only catch is, you have to deserialize them in
the same order, or remember how long 1 is. If you
wish to deserialize two, you have to create an
XmlTextReader and advance to the position in the
XML where 2 starts and then call:

2.Deserialize(xr);

-c
.



Actually, if you serialize 2 objects to XML, you''ll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof(Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c
"vince" <vl******@sdcera.org> wrote in message
news:09****************************@phx.gbl...

Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml looks
well-formed.

However, when attempting to deserialize the first object
in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical for
that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking on
the fact that there is more than one object to
deserialize...

Is there a step that I''m forgetting somewhere...??

thanks for any help...

vince

-----Original Message-----

"vince" <vl******@sdcera.org> wrote in message
news:00****************************@phx.gbl...

Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either
or both objects from the same file...??? How is this
done...??



Create an XmlTextWriter (we''ll call him xw)
Create Two XmlSerializers initialized with each type
and instance you wish to serialize. (we''ll call them
1 and 2)

1.Serialize(xw);
2.Serialize(xw);

The only catch is, you have to deserialize them in
the same order, or remember how long 1 is. If you
wish to deserialize two, you have to create an
XmlTextReader and advance to the position in the
XML where 2 starts and then call:

2.Deserialize(xr);

-c
.



Chad,

thanks again... so, to clarify, the workaround for the
problem of having two top level tags is to manually parse
the xml file that was created...???

Sorta new to xml so I''ll have to take your code snippet
and study it for a bit...

Looks like you load an intermediate array with the two
chunks of object data that you''ve parsed outa the
original 2 object xml file... is this correct?

thanks for the help,

vince

-----Original Message-----
Actually, if you serialize 2 objects to XML, you''ll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof(Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c
"vince" <vl******@sdcera.org> wrote in message
news:09****************************@phx.gbl...

Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml looks well-formed.

However, when attempting to deserialize the first object in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical for that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking on the fact that there is more than one object to
deserialize...

Is there a step that I''m forgetting somewhere...??

thanks for any help...

vince

>-----Original Message-----
>
>"vince" <vl******@sdcera.org> wrote in message
>news:00****************************@phx.gbl...
>> Can I add (append) to an xml file that already


contains a

>> serialized object, and be able to deserialize to either >> or both objects from the same file...??? How is this
>> done...??
>
>Create an XmlTextWriter (we''ll call him xw)
>Create Two XmlSerializers initialized with each type
>and instance you wish to serialize. (we''ll call them
>1 and 2)
>
>1.Serialize(xw);
>2.Serialize(xw);
>
>The only catch is, you have to deserialize them in
>the same order, or remember how long 1 is. If you
>wish to deserialize two, you have to create an
>XmlTextReader and advance to the position in the
>XML where 2 starts and then call:
>
>2.Deserialize(xr);
>
>-c
>
>
>.
>


.



这篇关于将2个对象序列化为相同的xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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