关于XML的序列化问题--十万火急

XiongBaoBao 2008-12-15 12:24:22
怎样实现下面的Xml类结构
<page name="Test">
<header height="95">
<item text="C1" />
<item text="C2" />
</header>
</Page>

我对item写了一个序列化的类Item
再写了一个Header类继承CollectionBase,打了个标签[XmlType("header")]
再写了一个page类
但现在问题出来了,我的header的height属性出不来,同时序列化的XML为Header,不是标签的"header"形式
...全文
161 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
pcjbird 2008-12-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 XiongBaoBao 的回复:]
其实就是一个:
<page name="Test">
<header height="95">
<item text="C1" />
<item text="C2" />
</header>
</Page>

能实现height属性和包含item的集合类HeaderClass的序列化问题
[/Quote]

那你的header应该是page的Element才对喔,你到底要不要把page一起序列化啊。。。。
pcjbird 2008-12-15
  • 打赏
  • 举报
回复
[XmlRoot("header")]
XiongBaoBao 2008-12-15
  • 打赏
  • 举报
回复
其实就是一个:
<page name="Test">
<header height="95">
<item text="C1" />
<item text="C2" />
</header>
</Page>

能实现height属性和包含item的集合类HeaderClass的序列化问题
pcjbird 2008-12-15
  • 打赏
  • 举报
回复
[XmlType("header")]
这个东东干啥用的。。。貌似不要吧。。。
qinhongfeng 2008-12-15
  • 打赏
  • 举报
回复
UP
XiongBaoBao 2008-12-15
  • 打赏
  • 举报
回复
以下是我写的对象:

[Serializable]
[XmlType("header")]
public class HeaderData : CollectionBase
{
private string m_height = "90";
/// <summary>
/// Header的高度
/// </summary>
[XmlAttribute(AttributeName = "height")]
public string Height
{
get { return m_height; }
set { m_height = value; }
}

//下面的把Item集合进来了
...
XiongBaoBao 2008-12-15
  • 打赏
  • 举报
回复
以下是我写的对象:

[Serializable]
[XmlType("header")]
public class HeaderData : CollectionBase
{
private string m_height = "90";
/// <summary>
/// Header的高度
/// </summary>
[XmlAttribute(AttributeName = "height")]
public string Height
{
get { return m_height; }
set { m_height = value; }
}

//下面的把Item集合进来了
...
XiongBaoBao 2008-12-15
  • 打赏
  • 举报
回复
我就是用的是XmlAttribute,但序列化成XML,height属性看不见了,不晓得是不是因为继承CollectionBase的原因
pcjbird 2008-12-15
  • 打赏
  • 举报
回复
[XmlAttribute("height")]
public string height
{
get
{
return _height;
}
set
{
_height=value;
}
}
wutianwutian 2008-12-15
  • 打赏
  • 举报
回复
UP!
pcjbird 2008-12-15
  • 打赏
  • 举报
回复
Attribute
pcjbird 2008-12-15
  • 打赏
  • 举报
回复
你用List也行,最后用ToArray就OK了。
pcjbird 2008-12-15
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 XiongBaoBao 的回复:]
谢谢 pcjbird ,用数组确实可以省掉 <ItemCollection>
[XmlElement("item")]
public ItemData[] DataItems//Item是你写得新的一个类
{
get { return this.items; }
set
{
this.items = value;
}
}
但ItemData[]实例化时本身要指定大小,你怎么解决大数据问题?
[/Quote]

不需要指定数组大小。。。。
private ItemData[] items;
这样就OK了。
当然你在set里也可以写成这样:

set
{
if (value == null)
throw new ArgumentException("data items cannot be null!");
this.items = value;
}
XiongBaoBao 2008-12-15
  • 打赏
  • 举报
回复
恩,问题解决了,谢谢热情的pcjbird,结贴了。
很奇怪,我之前的Header继承CollectionBase出来奇奇怪怪的东西,真不晓得是为什么?
也谢谢 wuyq11,你发的我看了,散分了。
wuyq11 2008-12-15
  • 打赏
  • 举报
回复
参考
http://www.cnblogs.com/surfsky/archive/2007/03/13/673620.html
XiongBaoBao 2008-12-15
  • 打赏
  • 举报
回复
谢谢 pcjbird ,用数组确实可以省掉<ItemCollection>
[XmlElement("item")]
public ItemData[] DataItems//Item是你写得新的一个类
{
get { return this.items; }
set
{
this.items = value;
}
}
但ItemData[]实例化时本身要指定大小,你怎么解决大数据问题?
pcjbird 2008-12-15
  • 打赏
  • 举报
回复
[XmlArrayItem("item")]
[XmlArray("ItemCollection")]
换成
[XmlElement("item")]
XiongBaoBao 2008-12-15
  • 打赏
  • 举报
回复
现在,我的Header类是这样写的,他不继承CollectionBase
private List<ItemData> m_ItemCollection;
[XmlArrayItem("item")]
[XmlArray("ItemCollection")]
public List<ItemData> ItemCollection
{
get
{
if (m_ItemCollection == null)
{
m_ItemCollection = new List<ItemData>();
}
return m_ItemCollection;
}
set
{
m_ItemCollection = value;
}
}
但现在序列化出来的形式为
<Page>
<header height="90">
<ItemCollection>
<item text="C1" />
<item text="C2" />
...
中间多了一个<ItemCollection>
不晓得有办法去掉没?
pcjbird 2008-12-15
  • 打赏
  • 举报
回复
要继承CollectionBase 也应该是Item来继承啊,不过根本不需要。。。。
pcjbird 2008-12-15
  • 打赏
  • 举报
回复
貌似看你的那个结果就是CollectionBase 出的问题。。。。
加载更多回复(6)

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧