-1

I have XML file (HERE). Now i want to create Deserialize from employees to my Class. What class should make and what function to use to do this automatically? Please note that there are two types of numbers.

Marek Woźniak
  • 1,766
  • 16
  • 34

3 Answers3

1

I would implement something like the following

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim EmployeeData1 As XDocument =
            <?xml version="1.0" encoding="utf-8"?>
            <Employees>
                <Employee>
                    <EmpId>1</EmpId>
                    <Name>Sam</Name>
                    <Sex>Male</Sex>
                    <Phone Type="Home">423-555-0124</Phone>
                    <Phone Type="Work">424-555-0545</Phone>
                    <Address>
                        <Street>7A Cox Street</Street>
                        <City>Acampo</City>
                        <State>CA</State>
                        <Zip>95220</Zip>
                        <Country>USA</Country>
                    </Address>
                </Employee>
                <Employee>
                    <EmpId>2</EmpId>
                    <Name>Lucy</Name>
                    <Sex>Female</Sex>
                    <Phone Type="Home">143-555-0763</Phone>
                    <Phone Type="Work">434-555-0567</Phone>
                    <Address>
                        <Street>Jess Bay</Street>
                        <City>Alta</City>
                        <State>CA</State>
                        <Zip>95701</Zip>
                        <Country>USA</Country>
                    </Address>
                </Employee>
                <Employee>
                    <EmpId>3</EmpId>
                    <Name>Kate</Name>
                    <Sex>Female</Sex>
                    <Phone Type="Home">166-555-0231</Phone>
                    <Phone Type="Work">233-555-0442</Phone>
                    <Address>
                        <Street>23 Boxen Street</Street>
                        <City>Milford</City>
                        <State>CA</State>
                        <Zip>96121</Zip>
                        <Country>USA</Country>
                    </Address>
                </Employee>
                <Employee>
                    <EmpId>4</EmpId>
                    <Name>Chris</Name>
                    <Sex>Male</Sex>
                    <Phone Type="Home">564-555-0122</Phone>
                    <Phone Type="Work">442-555-0154</Phone>
                    <Address>
                        <Street>124 Kutbay</Street>
                        <City>Montara</City>
                        <State>CA</State>
                        <Zip>94037</Zip>
                        <Country>USA</Country>
                    </Address>
                </Employee>
            </Employees>

        Dim EmployeeXMLReader1 As New EmployeeXMLReader
        Dim EmployeeList1 As List(Of EmployeeXMLReader.Employee)

        EmployeeList1 = EmployeeXMLReader1.ReadXML(EmployeeData1)

        'Test
        MsgBox(EmployeeList1(0).Address.Country)
        MsgBox(EmployeeList1(1).Phone_Home)
    End Sub

End Class

Public Class EmployeeXMLReader

    Structure Employee
        Dim EmpId As Integer
        Dim Name As String
        Dim Sex As String
        Dim Phone_Home As String
        Dim Phone_Work As String
        Dim Address As Address
    End Structure

    Structure Address
        Dim Street As String
        Dim City As String
        Dim State As String
        Dim Zip As String
        Dim Country As String
    End Structure

    Public Function ReadXML(ByVal XML As XDocument) As List(Of Employee)
        Dim EmployeeList1 As New List(Of Employee)

        Dim i As Integer

        For i = 0 To XML.Root.Descendants("Employee").Count - 1
            Dim MainData As XElement = XML.Root.Descendants("Employee").ElementAt(i)
            Dim Employee1 As New Employee

            Employee1.EmpId = MainData.Element("EmpId").Value
            Employee1.Name = MainData.Element("Name").Value
            Employee1.Sex = MainData.Element("Sex").Value
            Employee1.Phone_Home = MainData.Elements("Phone").Where(Function(x) x.Attribute("Type").Value = "Home").ElementAt(0)
            Employee1.Phone_Work = MainData.Elements("Phone").Where(Function(x) x.Attribute("Type").Value = "Work").ElementAt(0)

            Dim Address As New Address
            Dim AddressData As XElement = MainData.Element("Address")

            If Not AddressData Is Nothing Then
                Address.Street = AddressData.Element("Street").Value
                Address.City = AddressData.Element("City").Value
                Address.State = AddressData.Element("State").Value
                Address.Zip = AddressData.Element("Zip").Value
                Address.Country = AddressData.Element("Country").Value
            End If

            Employee1.Address = Address
            EmployeeList1.Add(Employee1)
        Next

        Return EmployeeList1
    End Function

End Class
David -
  • 2,009
  • 1
  • 13
  • 9
0

assume you have a class that represented in your xml named "myClass",you can Deserialize it using this code

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(myClass));
            textReader = new StreamReader("the path of the xml file need to deserialize");
            objObjectToLoad = xmlSerializer.Deserialize(textReader);

now you have to care of exception that can be thrown so you can wrap it with try/catch/finaly

Amjad Abdelrahman
  • 3,372
  • 1
  • 13
  • 20
  • i know, but build type of myClass is so hard. Look: http://stackoverflow.com/questions/25192004/add-same-xmlelement-of-name-with-other-type/25192260?noredirect=1#comment39230183_25192260 – Marek Woźniak Aug 07 '14 at 21:37
0

the result could looks like:

    public class Phone
    {
        [XmlAttribute("type")]
        public string Type { get; set; }
        [XmlText]
        public string Value { get; set; }
    }

    public class Address
    {
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Country { get; set; }
    }

    public class Employee
    {
        [XmlElement("EmpId", Order = 1)]
        public int Id { get; set; }

        [XmlElement("Name", Order = 2)]
        public string Name { get; set; }

        [XmlElement("Sex", Order = 3)]
        public string Sex { get; set; }

        [XmlElement(ElementName = "Phone", Order = 5)]
        public Phone phone_home { get; set; }

        [XmlElement(ElementName = "Phone", Order = 6)]
        public Phone phone_work { get; set; }

        [XmlElement(ElementName = "Address", Order = 7)]
        public Address Address { get; set; }

        public Employee() { }
        public Employee(string home, string work)
        {
            phone_home = new Phone()
            {
                Type = "home",
                Value = home
            };
            phone_work = new Phone()
            {
                Type = "work",
                Value = work
            };
        }
}
Marek Woźniak
  • 1,766
  • 16
  • 34