简体   繁体   English

将 XML 转换为 C# object

[英]Convert XML to C# object

I am having difficulty trying to convert my XML to a C# object.我在尝试将 XML 转换为 C# object 时遇到了困难。 This needs to be done dynamically.这需要动态完成。 I think the problem exists somewhere in the c# object class.我认为问题存在于 c# object class 的某处。

The XML 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:我的 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.目前我没有收到任何错误,只是一个空数组。如果您需要任何进一步的信息,请告诉我。

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 Address data, otherwise declare it the same way you did with Address and add it to the PostCodeValidatedSite class. PS:我没有添加Coordinates声明,因为您似乎只对Address数据感兴趣,否则以与Address相同的方式声明它并将其添加到PostCodeValidatedSite class。

Now a days there are many free online tool by which you can easily convert any XML to C sharp object online this can save much time also现在有很多免费的在线工具,您可以通过它轻松地将任何 XML 转换为 C 尖锐 object 在线这也可以节省很多时间

http://devsupertools.com/api/convert_xml_to_csharp try this URL and it will automatically convert any xml to c sharp http://devsupertools.com/api/convert_xml_to_csharp try this URL and it will automatically convert any xml to c sharp

Here is another approach to extract and deserialize just address nodes, using Cinchoo ETL - an open source library这是另一种提取和反序列化address节点的方法,使用Cinchoo ETL - 一个开源库

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

Working sample fiddle: https://dotnetfiddle.net/Jg1lUv工作样本小提琴: https://dotnetfiddle.net/Jg1lUv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM