2014-06-16 2 views
1

xml 파일은 직렬화 한 후 이와 같아야합니다. 그런 다음 vb.net에서 역 직렬화하려고합니다. 나는 프로그래밍에 초보자이다. 어떤 도움을 주셔서 감사합니다.VB.net을 사용하여 xml 파일을 직렬화 및 비 직렬화하려고 시도합니다.

<?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> 
+0

여기에서 시작해야합니다. http://msdn.microsoft.com/library/58a18dwa(v=vs.90).aspx?cs-save-lang=1&cs-lang=vb – Ksv3n

+0

은 다시로드하거나 주어진 파일 레이아웃으로 데이터를 내보내려면 어떻게해야합니까? – Plutonix

+0

이렇게 직렬화해야하는 데이터 구조가 있습니까? VB.NET 코드를 공유하십시오. 이 XML을 serialize하고 역 직렬화해야합니까? – Neolisk

답변

6

XML 직렬화에 대해 자세히 알아 보시기 바랍니다. 많은 정보는 MSDN에서 찾을 수 있습니다 (또한 모든 검색 엔진 사용). 예를 들어 MSDN : Introducing XML Serialization.

아직 코드가없는 경우. 주어진 XML 구조를 deserialize하는 것은 매우 간단하다. 다음과 같이 국가에 대한 간단한 클래스 정의를 만들 수 있습니다.

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에 주석을 붙일 수 있으므로 각 항목의 이름이 다르게 지정됩니다 (기본값은 <string>item</string>). 이 경우 XmlArrayItem attribute을 사용할 수 있습니다.

<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)으로 deserialize 할 것입니다. (위의 XML을 가정하는 파일 "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 Constructor (Type, XmlRootAttribute)).

직렬화 복원

최종 코드는 다음과 같습니다 직렬화

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"파일에 작성) :

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 

이 검색하여 아주 쉽게 찾을 수 있었다 모든 및 문서를 읽는 중.

관련 문제