在C#中反序列化文件XML

时间:2014-08-06 15:28:23

标签: xml visual-studio-2012 linq-to-xml

我有XML文件(HERE)。 现在我想从员工到我的班级创建反序列化。应该使用什么类以及使用什么函数来自动执行此操作?请注意,有两种类型的数字。

3 个答案:

答案 0 :(得分:1)

我会实现类似下面的内容

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

答案 1 :(得分:0)

假设您有一个在您的xml中表示的名为&#34; myClass&#34;的类,您可以使用此代码对其进行反序列化

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

现在你必须关注可抛出的异常,以便用try / catch / finaly包装它

答案 2 :(得分:0)

结果可能如下:

    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
            };
        }
}