尝试使用 VB.net 序列化和反序列化 xml 文件 [英] trying to serialize and deserialize an xml file using VB.net

查看:69
本文介绍了尝试使用 VB.net 序列化和反序列化 xml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

序列化后的xml文件应该是这样的,然后我想在vb.net中反序列化它.我是编程的初学者.任何帮助表示赞赏.

The xml file should like this after I serialize and then I want to deserialize this in vb.net. I am a begginer in programming. Any help is appreciated.

<?xml version="1.0"?>
<Countries>
  <Country>
    <CID>1</CID>
    <CountryName>India</CountryName>
    <States>
      <State> New Delhi </State>
      <State> Maharashtra </State>
      <State> Rajasthan </State>
    </States>
  </Country>
  <Country>
    <CID>2</CID>
    <CountryName>United States</CountryName>
    <States>
      <State> Washington </State>
      <State> Texas </State>
    </States>
  </Country>
  <Country>
    <CID>3</CID>
    <CountryName>Australia</CountryName>
    <States>
      <State> Queensland </State>
      <State> Victoria </State>
    </States>
  </Country>
</Countries>

推荐答案

我建议您一定要研究一下 XML 序列化.在 MSDN 上可以找到很多信息(也可以使用任何搜索引擎).例如在 MSDN 上:介绍 XML 序列化.

I'd advise you to definitely look into XML serialization. A lot of information can be found on MSDN (but also using any search engine). For example on MSDN: Introducing XML Serialization.

如果你还什么都没有,那就找代码吧.我会非常简单地反序列化给定的 XML 结构.您可以为 Country 创建一个简单的类定义,如下所示:

If you have nothing yet, for code. I would keep it very simple to deserialize the given XML structure. You can create a simple class definition for a Country, as shown below:

Public Class Country
    Public Property CID As Integer
    Public Property CountryName As String
    Public Property States As List(Of String)

    Public Sub New()
        States = New List(Of String)()
    End Sub
End Class

现在这还不能 100% 工作.您必须使用状态列表帮助序列化对象.您可以注释(使用属性)States,因此序列化程序知道每个项目的名称不同(默认为 item</string>).您可以使用 XmlArrayItem 属性这个.

Now this doesn't work yet 100%. You have to help the serialization object with the list of states. You can annotate (with attributes) the States, so the serializer knows that each item is named differently (default it would be <string>item</string>). You can use the XmlArrayItem attribute for this.

<Serializable()>
Public Class Country
    Public Property CID As Integer
    Public Property CountryName As String
    <XmlArrayItem("State")>
    Public Property States As List(Of String)

    Public Sub New()
        States = New List(Of String)()
    End Sub
End Class

最后,进行反序列化.我会反序列化为 List(Of Country),因为它显然是一个列表.(假设上述 XML 存储在文件obj.xml"中.)

Finally, for deserialization. I'd deserialize to a List(Of Country), as it clearly is a list. (Assuming the above XML is stored in a file "obj.xml".)

Dim serializer As New XmlSerializer(GetType(List(Of Country)))
Dim deserialized As List(Of Country) = Nothing
Using file = System.IO.File.OpenRead("obj.xml")
    deserialized = DirectCast(serializer.Deserialize(file), List(Of Country))
End Using

现在我们仍然需要帮助序列化器对象,否则它不知道如何反序列化给定的 XML;因为它没有正确确定根节点.我们可以在这里使用构造函数的重载,在其中我们可以说出根节点是什么 (XmlSerializer 构造函数(类型,XmlRootAttribute)).

Now we still have to help the serializer object, because otherwise it doesn't know how to deserialize the given XML; as it doesn't determine the root node correctly. We can use an overload of the constructor here, in which we can say what the root node is (XmlSerializer Constructor (Type, XmlRootAttribute)).

反序列化的最终代码是:

Final code for deserialization would be:

Dim serializer As New XmlSerializer(GetType(List(Of Country)), New XmlRootAttribute("Countries"))
Dim deserialized As List(Of Country) = Nothing
Using file = System.IO.File.OpenRead("obj.xml")
    deserialized = DirectCast(serializer.Deserialize(file), List(Of Country))
End Using

序列化代码(写入文件obj.xml"):

Code for serialization (writing to file "obj.xml"):

Dim countries As New List(Of Country)()
' Make sure you add the countries to the list

Dim serializer As New XmlSerializer(GetType(List(Of Country)), New XmlRootAttribute("Countries"))
Using file As System.IO.FileStream = System.IO.File.Open("obj.xml", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
    serializer.Serialize(file, countries)
End Using

通过搜索和阅读文档可以很容易地找到所有这些.

All this could have been found quite easily by searching and reading the documentation.

这篇关于尝试使用 VB.net 序列化和反序列化 xml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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