将XML转换为C#对象

问题描述 投票:0回答:1

我在尝试将XML转换为C#对象时遇到困难。这需要动态完成。我认为问题存在于C#对象类中的某处。

XML

           <Sites>
                <PostCodeValidatedSite>
                    <Address>
                        <ALK>A00067262524</ALK>
                        <BuildingName>1 The Pavilions</BuildingName>
                        <CSSDistrictCode>CM</CSSDistrictCode>
                        <ExchangeCode>SOL</ExchangeCode>
                        <IsPostCodeValid>true</IsPostCodeValid>
                        <Locality>Shirley</Locality>
                        <PostCode>B90 4SB</PostCode>
                        <PostTown>Solihull</PostTown>
                        <Qualifier>Gold</Qualifier>
                        <Street>Cranmore Drive</Street>
                        <Technologies>
                            <Technology>
                                <IsAssociated>true</IsAssociated>
                                <IsRestricted>false</IsRestricted>
                                <Name>Copper</Name>
                            </Technology>
                            <Technology>
                                <IsAssociated>true</IsAssociated>
                                <IsRestricted>false</IsRestricted>
                                <Name>PointToPointFibre</Name>
                            </Technology>
                            <Technology>
                                <IsAssociated>false</IsAssociated>
                                <IsRestricted>false</IsRestricted>
                                <Name>FTTPBrownfield</Name>
                            </Technology>
                            <Technology>
                                <IsAssociated>false</IsAssociated>
                                <IsRestricted>false</IsRestricted>
                                <Name>FTTPGreenfield</Name>
                            </Technology>
                        </Technologies>
                    </Address>
                    <Coordinates>
                        <Easting>413358</Easting>
                        <Latitude>52.39657</Latitude>
                        <Longitude>-1.79875</Longitude>
                        <Northing>278082</Northing>
                    </Coordinates>
                </PostCodeValidatedSite>


                <PostCodeValidatedSite>
                    <Address>
                        <ALK>A15100427347</ALK>
                        <BuildingName>1 The Pavilions</BuildingName>
                        <CSSDistrictCode>CM</CSSDistrictCode>
                        <ExchangeCode>SOL</ExchangeCode>
                        <IsPostCodeValid>true</IsPostCodeValid>
                        <Locality>Shirley</Locality>
                        <PostCode>B90 4SB</PostCode>
                        <PostTown>Solihull</PostTown>
                        <Qualifier>Gold</Qualifier>
                        <Street>Cranmore Drive</Street>
                        <SubBuilding>Floor 001-Room Comm</SubBuilding>
                        <Technologies>
                            <Technology>
                                <IsAssociated>false</IsAssociated>
                                <IsRestricted>false</IsRestricted>
                                <Name>Copper</Name>
                            </Technology>
                            <Technology>
                                <IsAssociated>true</IsAssociated>
                                <IsRestricted>false</IsRestricted>
                                <Name>PointToPointFibre</Name>
                            </Technology>
                            <Technology>
                                <IsAssociated>false</IsAssociated>
                                <IsRestricted>false</IsRestricted>
                                <Name>FTTPBrownfield</Name>
                            </Technology>
                            <Technology>
                                <IsAssociated>false</IsAssociated>
                                <IsRestricted>false</IsRestricted>
                                <Name>FTTPGreenfield</Name>
                            </Technology></Technologies>
                    </Address>
                    <Coordinates>
                        <Easting>413358</Easting>
                        <Latitude>52.39657</Latitude>
                        <Longitude>-1.79875</Longitude>
                        <Northing>278082</Northing>
                    </Coordinates>
                </PostCodeValidatedSite>
            </Sites>

序列号:

 string Outerxml = xmlToFormat.FirstChild.FirstChild.FirstChild.FirstChild.LastChild.FirstChild.FirstChild.OuterXml;
        string formatedXml = XmlFormatter.RemoveXmlns(Outerxml);

        List<Address> result = new List<Address>(); ;

        // Deserialises xlm into an object 
        XmlSerializer serializer = new XmlSerializer(typeof(List<Address>), new XmlRootAttribute("Address"));
        using (TextReader reader = new StringReader(formatedXml))
        {
            result = (List<Address>)serializer.Deserialize(reader);
        }

        return result;

我的对象类:

public class Address
{
    [XmlElement("ALK")]
    public string ALK { get; set; }

    [XmlElement("BuildingName")]
    public string BuildingName { get; set; }

    [XmlElement("CSSDistrictCode")]
    public string CSSDistrictCode { get; set; }

    [XmlElement("IsPostCodeValid")]
    public Boolean IsPostCodeValid { get; set; }

    [XmlElement("Locality")]
    public string Locality { get; set; }

    [XmlElement("PostCode")]
    public string PostCode { get; set; }

    [XmlElement("PostTown")]
    public string PostTown { get; set; }

    [XmlElement("Qualifier")]
    public string Qualifier { get; set; }

    [XmlElement("Street")]
    public string Street { get; set; }
}

}

目前我还没有收到任何错误,只是一个空数组。如果您需要任何其他信息,请告诉我。

c# xml object soap soap-client
1个回答
1
投票

添加以下课程

[Serializable, XmlRoot("Sites")]
public class Sites
{
    [XmlElement("PostCodeValidatedSite")]
    public List<PostCodeValidatedSite> PostCodeValidatedSites { get; set; }
}

public class PostCodeValidatedSite
{
    public Address Address { get; set; }
}

然后以这种方式反序列化

XmlSerializer serializer = new XmlSerializer(typeof(Sites));
using (TextReader reader = new StringReader(formatedXml))
{
    var result = (Sites)serializer.Deserialize(reader);
}

PS:我没有添加Coordinates声明,因为您似乎只对Address数据感兴趣,否则以与Address相同的方式声明它,并将其添加到PostCodeValidatedSite类中。

© www.soinside.com 2019 - 2024. All rights reserved.