求救:一种数据结构存取XML

yuanchao1985 2013-12-30 01:50:05

用VB2010实现一种数据结构存取XML文件
数据结构描述
类1的对象1
类2的对象1
类2的对象2
类1的对象2
类3的对象1
类1的对象3
……
……
……
对象的数量不定,有顺序
怎样实现?
小弟是新手,请各位大神指点。谢谢!
...全文
178 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sjxwb 2013-12-31
  • 打赏
  • 举报
回复
转成Datatable 存入数据库, 用到时再生成XML文件
yuanchao1985 2013-12-30
  • 打赏
  • 举报
回复
引用 1 楼 feiyun0112 的回复:
XmlElementAttribute 可以多次应用于返回对象数组的字段。其用途在于指定(通过 Type 属性 (Property))可插入数组中的不同类型。例如,下面的 C# 代码中的数组既接受字符串,又接受整数。

public class Things{
[XmlElement(DataType = typeof(string)),
XmlElement(DataType = typeof(int))]
public object[] StringsAndInts;
}

签名档: http://feiyun0112.cnblogs.com/


感谢版主,我用你的方法实现了存取
   ' Create the XmlSerializer.
Dim s As New XmlSerializer(GetType(TestProgram))

' To write the file, a TextWriter is required.
Dim writer As New StreamWriter(filename)

' Create an instance of the group to serialize, and set
' its properties.
Dim Test As New TestProgram()

Dim productInfo As New productInfo
productInfo.strProductName = "苹果充电器"
Test.ProductInfomation = productInfo
'#Zone " 1 "
Dim opentime As New TestTypeOpenTime
opentime.ID = 1
opentime.Name = "开机时间"
ReDim Test.OpenTime(0)
Test.OpenTime(0) = opentime
'#End Zone

'#Zone " 2 "
ReDim Preserve Test.CommonTest(0)
Dim comm1 As New TestTypeCommon
comm1.ID = 2
comm1.Name = "轻载"
Test.CommonTest(0) = comm1
'#End Zone

'#Zone " 3 "
Dim ocp As New TestTypeOcp
ocp.ID = 3
ocp.Name = "OCP测试"
ReDim Test.OCP(0)
Test.OCP(0) = ocp
'#End Zone

'#Zone " 4 "
ReDim Preserve Test.CommonTest(1)
Dim comm2 As New TestTypeCommon
comm2.ID = 4
comm2.Name = "重载"
Test.CommonTest(1) = comm2
'#End Zone

s.Serialize(writer, Test)
writer.Close()

效果如图所示

遇到了新的问题,怎样实现排序呢?
每一个对象都有独一无二的序号,我要对这些对象进行增加、删除、移动。
TestProgram类里的每一个元素都是不定数组,改1个影响所有
feiyun0112 2013-12-30
  • 打赏
  • 举报
回复
XmlElementAttribute 可以多次应用于返回对象数组的字段。其用途在于指定(通过 Type 属性 (Property))可插入数组中的不同类型。例如,下面的 C# 代码中的数组既接受字符串,又接受整数。

public class Things{
[XmlElement(DataType = typeof(string)),
XmlElement(DataType = typeof(int))]
public object[] StringsAndInts;
}

所生成的 XML 可能类似于:

<Things>
<string>Hello</string>
<int>999</int>
<string>World</string>
</Things>


http://msdn.microsoft.com/zh-cn/library/system.xml.serialization.xmlelementattribute%28v=VS.80%29.aspx

*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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