VB.Net代码未根据需要进行序列化. [英] VB.Net code not serializing as needed.

查看:100
本文介绍了VB.Net代码未根据需要进行序列化.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我一直在努力和谷歌搜索努力使此代码完全与其他程序一样进行序列化.

基本上,我有一个XML文件,想将其读取到一个类中,然后在以后再次对其进行序列化.对一个文件进行硬编码是行得通的,但这仅是因为当我不使用列表时可以使用属性.

我无法使用列表时将NoOfItems ="2"添加到XML中!

Hello,
I have been trying and Googling hard to get this code to serialize exactly as it does with another program.

Basically I have a XML file and want to read it into a class, then serialize it again at a later time. Hard coding one file works, but only because I can use attributes when I am not using lists.

I can not get it do add NoOfItems="2" to the XML when I am using my lists!!!

<BatchItems NoOfItems="2">
- <Item>
   <ProductID>112800</ProductID>
   <ItemID>112800</ItemID>
  </Item>
- <Item>
   <ProductID>115250</ProductID>
   <ItemID>115250</ItemID>
  </Item>



我有这样的东西:



I have something like this:

Public Property Batches As New BatchList 

Public Class BatchList
        Public Property Batch As New List(Of Batch)
        <XmlAttribute("NoOfBatches")> _
        Public Property NoOfBatches As String = Batch.Count
    End Class




我真的很感谢您的帮助!




I would really appreciate any help!

推荐答案

为什么要手动处理XML文件以进行序列化?最好的序列化方法(尤其是XML)是基于 Data Contract 的.这种方法是非侵入性的,最易于使用并且非常可靠.实际上,您可以存储和还原任何对象或对象图(即使其中包含循环引用),而无需在数据模型中进行任何更改:您只需向要成为合同一部分的类型和成员添加一些属性.您还可以通过使用唯一的公司或个人URL作为XML命名空间,使基于XML的持久数据表示形式成为世界唯一的标准.

请参阅:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

请在我倡导这种方法的地方查看我过去的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [
Why messing with XML files manually for serialization? The best way of serialization, XML in particular, is based on Data Contract. This method is non-intrusive, easiest to use and very reliable. Practically, you can store and restore any object or object graph (even with circular references in it) without any changes in the data model: you only add some attributes to types and members which you want to make part of contract. You can also make your persistent XML-base form of data presentation a world-unique standard, by using your unique company or personal URL for the namespace of XML.

Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please also see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA


这篇关于VB.Net代码未根据需要进行序列化.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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