如何反序列化变量元素/节点 [英] how to deserialize variable element/node

查看:69
本文介绍了如何反序列化变量元素/节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的问题:


我正在将一长串XML反序列化为一个非常大的对象

foo:


< foo>

.......

< bar>有时一个简单的字符串在这里< /吧>

.......

< / foo>


或有时它是'br />

< foo>

.......

< bar>

< ; zip>有时< / zip>

< zap>更多结构< / zap>

< zoo>在这里< / zoo>

< / bar>

.......

< / foo>


foo类是foo1


class foo {

....

string bar;

。 ...

}


或foo2


class foo {

。 ..

string bar [];

...

}


class bar {

string zip;

string zap;

string zoo;

}


短条元素是大foo1和

foo2之间的唯一区别。


什么是最好的(ok)最简单;强大的;最快的方法吗?

1.选择正确的类型

2.从字符串中移除< bar> ...< / bar并单独反序列化

3.使用XMLAnyElement

4.使用未知元素事件


谢谢,


Bill

I''ve a complex problem:

I''m deserializing a very long string of XML into a very large object
foo:

<foo>
.......
<bar>sometimes a simple string is here</bar>
.......
</foo>

or sometimes it''s

<foo>
.......
<bar>
<zip>sometimes</zip>
<zap>more structure</zap>
<zoo>is here</zoo>
</bar>
.......
</foo>

the foo class is either foo1

class foo {
....
string bar;
....
}

or foo2

class foo {
...
string bar[];
...
}

class bar {
string zip;
string zap;
string zoo;
}

The short bar element is the only difference between the large foo1 and
foo2.

What is the best (ok, easiest; robust; fastest) way to do this?
1. select the correct type
2. remove the <bar>...</barfrom the string and deserialize separately
3. use XMLAnyElement
4. use the unknown element event

Thanks,

Bill

推荐答案


wp ********* @ gmail.com 写道:

我有一个复杂的问题:
I''ve a complex problem:



用不同的方式说:我需要一个元素可以是一个

complexType或一个字符串

Said a different way: i need an element that can be either a
complexType or a string


我正在将一长串XML反序列化为一个非常大的对象

foo:


< foo>

.......

< bar>有时一个简单的字符串就在这里< / bar>

...... 。

< / foo>


或有时它是


< foo >

.......

< bar>

< zip>有时< / zip>

< zap>更多结构< / zap>

< zoo>在这里< / zoo>

< / bar>

.......

< / foo>


foo类是foo1


class foo {

...

string bar;

...

}


或foo2


class foo {

...

string bar [] ;

...

}


class bar {

string zip;

string zap;

string zoo;

}


短栏元素是唯一的区别大foo1和

foo2。


什么是最好的(好的,最简单的;强大的;最快的方法吗?

1.选择正确的类型

2.从字符串中移除< bar> ...< / bar并单独反序列化

3.使用XMLAnyElement

4.使用未知元素事件


谢谢,


Bill
I''m deserializing a very long string of XML into a very large object
foo:

<foo>
.......
<bar>sometimes a simple string is here</bar>
.......
</foo>

or sometimes it''s

<foo>
.......
<bar>
<zip>sometimes</zip>
<zap>more structure</zap>
<zoo>is here</zoo>
</bar>
.......
</foo>

the foo class is either foo1

class foo {
...
string bar;
...
}

or foo2

class foo {
...
string bar[];
...
}

class bar {
string zip;
string zap;
string zoo;
}

The short bar element is the only difference between the large foo1 and
foo2.

What is the best (ok, easiest; robust; fastest) way to do this?
1. select the correct type
2. remove the <bar>...</barfrom the string and deserialize separately
3. use XMLAnyElement
4. use the unknown element event

Thanks,

Bill


2006年11月15日19:28:42 -0800, wp ********* @ gmail.com 写道:
On 15 Nov 2006 19:28:42 -0800, wp*********@gmail.com wrote:

我有一个复杂的问题:


我正在将一长串XML反序列化为一个非常大的对象

foo:


< foo>

.......

< bar>有时一个简单的字符串就在这里< / bar>

.......

< / foo>


或有时它是


< foo>

.......

< bar>

< zip>有时< / zip>

< zap>更多结构< / zap> ;

< zoo>在这里< / zoo>

< / bar>

.......

< / foo>


foo类是foo1


class foo {

...

字符串吧;

...

}


或foo2


class foo {

...

string bar [];

... < br $>
}


班级栏{

字符串拉链;

string zap;

string zoo;

}


短条元素是大foo1和

foo2之间的唯一区别。


什么是最好的(好的,最简单的;强大的;最快的方法吗?

1.选择正确的类型

2.从字符串中移除< bar> ...< / bar并单独反序列化

3.使用XMLAnyElement

4.使用未知元素事件


谢谢,


Bill
I''ve a complex problem:

I''m deserializing a very long string of XML into a very large object
foo:

<foo>
.......
<bar>sometimes a simple string is here</bar>
.......
</foo>

or sometimes it''s

<foo>
.......
<bar>
<zip>sometimes</zip>
<zap>more structure</zap>
<zoo>is here</zoo>
</bar>
.......
</foo>

the foo class is either foo1

class foo {
...
string bar;
...
}

or foo2

class foo {
...
string bar[];
...
}

class bar {
string zip;
string zap;
string zoo;
}

The short bar element is the only difference between the large foo1 and
foo2.

What is the best (ok, easiest; robust; fastest) way to do this?
1. select the correct type
2. remove the <bar>...</barfrom the string and deserialize separately
3. use XMLAnyElement
4. use the unknown element event

Thanks,

Bill



嗯,这段代码没有多大意义。 bar是变量名还是

类? ''string bar''是一个名为bar的字符串,''string bar []''是一个名为bar的

字符串数组,但是你有一个名为bar的类:)

在.NET中,据我所知,XML中没有存储XMLAnyElement

的原生方式,因为它总是被转义并存储为字符串。

如果你*必须*存储''raw''XML那么你必须实现ISerializable

接口并执行你自己的XML流读/写,我这样做我们的

产品用于存储和检索XML标记的自由格式文本(包含

嵌套< Band< Itags等)。这很麻烦,如果可能的话,应该避免使用。

你的描述中的一个问题是你为foo提供了两个定义。一个

类可以是其中之一,但除非它们有不同的名字,否则它们都不会存在,在这种情况下你的标签会有所不同,所以你会知道

重新创建它们所需的类类型。

请记住,您不必使用内置的序列化程序;你可以使用XMLWriters和读者自己写你自己的



干杯,

小工具

Well, this code doesn''t quite make sense. Is bar a variable name or a
class? ''string bar'' is a string called bar, ''string bar[]'' is an array of
strings called bar, but then you''ve got a class called bar :)
In .NET as far as I know there is no native way to have an XMLAnyElement
stored in your XML, as it will always be escaped and stored as a string.
If you *must* store ''raw'' XML then you have to implement the ISerializable
interface and perform your own XML stream read/write, and I do this in our
product to store and retrieve XML marked-up freeform text (containing
nested <Band <Itags etc.). It''s messy, and should be avoided if
possible.
One problem in your description is that you give two definitions for foo. A
class can be one or the other, but both can''t exist unless they have
different names, in which case your tags will be different so you will know
the class type needed to recreate them.
Remember that you don''t have to use the built in serializers; you can write
your own using XMLWriters and readers.

Cheers,
Gadget




Gadget写道:

Gadget wrote:

2006年11月15日19:28:42 -0800, wp ********* @ gmail.com 写道:
On 15 Nov 2006 19:28:42 -0800, wp*********@gmail.com wrote:

我有一个复杂的问题:


我正在将一长串XML反序列化为一个非常大的对象

foo:


< foo>

.......

< bar>有时一个简单的字符串就在这里< / bar>

.......

< / foo>


或有时候它是


< foo>

.......

< bar>

< zip>有时< / zip>

< zap>更多结构< / zap>

< zoo>在这里< / zoo>

< / bar>

.......

< / foo>


foo类是foo1


class foo {

...

string bar;

...

}
< br $>
或foo2


class foo {

...

string bar [];

...

}


class bar {

string zip;

string zap;

string zoo;

}


短条元素是大foo1和大foo1之间的唯一区别

foo2。


什么是最好的(好的,最简单的;强大的;最快的方法吗?

1.选择正确的类型

2.从字符串中移除< bar> ...< / bar并单独反序列化

3.使用XMLAnyElement

4.使用未知元素事件


谢谢,


比尔
I''ve a complex problem:

I''m deserializing a very long string of XML into a very large object
foo:

<foo>
.......
<bar>sometimes a simple string is here</bar>
.......
</foo>

or sometimes it''s

<foo>
.......
<bar>
<zip>sometimes</zip>
<zap>more structure</zap>
<zoo>is here</zoo>
</bar>
.......
</foo>

the foo class is either foo1

class foo {
...
string bar;
...
}

or foo2

class foo {
...
string bar[];
...
}

class bar {
string zip;
string zap;
string zoo;
}

The short bar element is the only difference between the large foo1 and
foo2.

What is the best (ok, easiest; robust; fastest) way to do this?
1. select the correct type
2. remove the <bar>...</barfrom the string and deserialize separately
3. use XMLAnyElement
4. use the unknown element event

Thanks,

Bill



嗯,这段代码没有多大意义。 bar是变量名还是

类? ''string bar''是一个名为bar的字符串,''string bar []''是一个名为bar的

字符串数组,但是你有一个名为bar的类:)

在.NET中,据我所知,XML中没有存储XMLAnyElement

的原生方式,因为它总是被转义并存储为字符串。

如果你*必须*存储''raw''XML那么你必须实现ISerializable

接口并执行你自己的XML流读/写,我这样做我们的

产品用于存储和检索XML标记的自由格式文本(包含

嵌套< Band< Itags等)。这很麻烦,如果可能的话,应该避免使用。

你的描述中的一个问题是你为foo提供了两个定义。一个

类可以是其中之一,但除非它们有不同的名字,否则它们都不会存在,在这种情况下你的标签会有所不同,所以你会知道

重新创建它们所需的类类型。

请记住,您不必使用内置的序列化程序;你可以使用XMLWriters和读者写你自己的



干杯,

小工具


Well, this code doesn''t quite make sense. Is bar a variable name or a
class? ''string bar'' is a string called bar, ''string bar[]'' is an array of
strings called bar, but then you''ve got a class called bar :)
In .NET as far as I know there is no native way to have an XMLAnyElement
stored in your XML, as it will always be escaped and stored as a string.
If you *must* store ''raw'' XML then you have to implement the ISerializable
interface and perform your own XML stream read/write, and I do this in our
product to store and retrieve XML marked-up freeform text (containing
nested <Band <Itags etc.). It''s messy, and should be avoided if
possible.
One problem in your description is that you give two definitions for foo. A
class can be one or the other, but both can''t exist unless they have
different names, in which case your tags will be different so you will know
the class type needed to recreate them.
Remember that you don''t have to use the built in serializers; you can write
your own using XMLWriters and readers.

Cheers,
Gadget



我不应该开始使用我有一个复杂的问题;它真的很简单。然而,解决方案可能涉及一些复杂性。


无论如何,我有一个名为foo的类。进入它,我想

为我无法控制的模式反序列化XML。 XML将

有时会有一个元素栏,这是一个字符串,有时它会是b / b
复杂的数据。


< ; foo>

.......

< bar>有时一个简单的字符串就在这里< / bar>

.. .....

< / foo>





(更正)


class foo {

...

bar baz [];

...

}


class bar {

string zip;

string zap;

字符串动物园;

}


感谢您的回复:)

I should not have started of with "I''ve a complex problem"; it''s really
rather simple. The solution, however, may involve some complexity.

Anyway, I have a single class called foo. Into it, I want to
deserialize XML for a schema which I do not control. The XML will
sometimes have an element bar which is a string, and sometimes it will
be complex data.

<foo>
.......
<bar>sometimes a simple string is here</bar>
.......
</foo>

OR

(corrected)

class foo {
...
bar baz[];
...
}

class bar {
string zip;
string zap;
string zoo;
}

Thanks for the reply :)


这篇关于如何反序列化变量元素/节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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