ashley g
ashley g

Reputation: 885

Convert XML to C# object

I am having difficulty trying to convert my XML to a C# object. This needs to be done dynamically. I think the problem exists somewhere in the c# object class.

The 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>

The serialisation code:

 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;

My object Class:

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

}

At the moment I do not receive any errors just an empty array.Please let me know if you require any further information.

Upvotes: 0

Views: 403

Answers (2)

Cinchoo
Cinchoo

Reputation: 6322

Here is another approach to extract and deserialize just address nodes, using Cinchoo ETL - an open source library

using (var r = ChoXmlReader<Address>.LoadText(xml).WithXPath("//Address"))
{
    r.Print();
}

Working sample fiddle: https://dotnetfiddle.net/Jg1lUv

Upvotes: 1

Innat3
Innat3

Reputation: 3576

Add the following classes

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

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

Then deserialize it this way

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

PS: I didn't add the Coordinates declaration since you only seemed interested in the Addressdata, otherwise declare it the same way you did with Address and add it to the PostCodeValidatedSite class.

Upvotes: 2

Related Questions