Search code examples
c#xmlssisdeserializationxml-deserialization

C# Deserialize XML to SSIS Output Buffer


I am trying to deserialize an xml report that is stored as text in our database.

I have declared the following classes for this via XML2CSharp.

[XmlRoot(ElementName = "asic-report")]
    public class Asicreport
    {
        [XmlElement(ElementName = "asic-name")]
        public string Asicname { get; set; }
        [XmlElement(ElementName = "asic-organisation-number")]
        public string Asicorganisationnumber { get; set; }
        [XmlElement(ElementName = "asic-a-b-n")]
        public string Asicabn { get; set; }
        [XmlElement(ElementName = "asic-organisation-number-heading")]
        public string Asicorganisationnumberheading { get; set; }
        [XmlElement(ElementName = "asic-type")]
        public string Asictype { get; set; }
        [XmlElement(ElementName = "asic-status")]
        public string Asicstatus { get; set; }
        [XmlElement(ElementName = "asic-jurisdiction")]
        public string Asicjurisdiction { get; set; }
        [XmlElement(ElementName = "asic-locality")]
        public string Asiclocality { get; set; }
        [XmlElement(ElementName = "asic-address-state")]
        public string Asicaddressstate { get; set; }
        [XmlElement(ElementName = "asic-postcode")]
        public string Asicpostcode { get; set; }
    }

    [XmlRoot(ElementName = "australian-business-register-report")]
    public class Australianbusinessregisterreport
    {
        [XmlElement(ElementName = "ASICNumber")]
        public string ASICNumber { get; set; }
        [XmlElement(ElementName = "ABN")]
        public string ABN { get; set; }
        [XmlElement(ElementName = "ABNStatus")]
        public string ABNStatus { get; set; }
        [XmlElement(ElementName = "ABNStatusFromDate")]
        public string ABNStatusFromDate { get; set; }
        [XmlElement(ElementName = "EntityTypeInd")]
        public string EntityTypeInd { get; set; }
        [XmlElement(ElementName = "Non-IndividualName")]
        public string NonIndividualName { get; set; }
        [XmlElement(ElementName = "BusinessAddressState")]
        public string BusinessAddressState { get; set; }
        [XmlElement(ElementName = "BusinessAddressPostcode")]
        public string BusinessAddressPostcode { get; set; }
        [XmlElement(ElementName = "GSTStatus")]
        public string GSTStatus { get; set; }
        [XmlElement(ElementName = "GSTStatusFromDate")]
        public string GSTStatusFromDate { get; set; }
        [XmlElement(ElementName = "TradingName")]
        public string TradingName { get; set; }
    }

    [XmlRoot(ElementName = "street-type")]
    public class Streettype
    {
        [XmlAttribute(AttributeName = "code")]
        public string Code { get; set; }
    }

    [XmlRoot(ElementName = "address")]
    public class Address
    {
        [XmlElement(ElementName = "unit-number")]
        public string Unitnumber { get; set; }
        [XmlElement(ElementName = "street-number")]
        public string Streetnumber { get; set; }
        [XmlElement(ElementName = "property")]
        public string Property { get; set; }
        [XmlElement(ElementName = "street-name")]
        public string Streetname { get; set; }
        [XmlElement(ElementName = "street-type")]
        public Streettype Streettype { get; set; }
        [XmlElement(ElementName = "suburb")]
        public string Suburb { get; set; }
        [XmlElement(ElementName = "state")]
        public string State { get; set; }
        [XmlElement(ElementName = "postcode")]
        public string Postcode { get; set; }
        [XmlAttribute(AttributeName = "first-reported")]
        public string Firstreported { get; set; }
        [XmlAttribute(AttributeName = "type")]
        public string Type { get; set; }
    }

    [XmlRoot(ElementName = "bureau-report")]
    public class Bureaureport
    {
        [XmlElement(ElementName = "address")]
        public Address Address { get; set; }
        [XmlElement(ElementName = "last-search-date")]
        public string Lastsearchdate { get; set; }
        [XmlElement(ElementName = "nature-of-business")]
        public string Natureofbusiness { get; set; }
        [XmlElement(ElementName = "ErrorMsg")]
        public string ErrorMsg { get; set; }
        [XmlElement(ElementName = "WarningMsg")]
        public string WarningMsg { get; set; }
        [XmlElement(ElementName = "bureau-reference")]
        public string Bureaureference { get; set; }
        [XmlElement(ElementName = "bca-company-type")]
        public string Bcacompanytype { get; set; }
        [XmlElement(ElementName = "bca-name")]
        public string Bcaname { get; set; }
    }

    [XmlRoot(ElementName = "org-id-result")]
    public class Orgidresult
    {
        [XmlElement(ElementName = "match-name")]
        public string Matchname { get; set; }
        [XmlElement(ElementName = "organisation-number")]
        public string Organisationnumber { get; set; }
        [XmlElement(ElementName = "organisation-type")]
        public string Organisationtype { get; set; }
        [XmlElement(ElementName = "australian-business-number")]
        public string Australianbusinessnumber { get; set; }
        [XmlElement(ElementName = "organisation-number-heading")]
        public string Organisationnumberheading { get; set; }
        [XmlElement(ElementName = "state")]
        public string State { get; set; }
        [XmlElement(ElementName = "asic-report")]
        public Asicreport Asicreport { get; set; }
        [XmlElement(ElementName = "australian-business-register-report")]
        public Australianbusinessregisterreport Australianbusinessregisterreport { get; set; }
        [XmlElement(ElementName = "bureau-report")]
        public Bureaureport Bureaureport { get; set; }
    }

    [XmlRoot(ElementName = "response")]
    public class Response
    {
        [XmlElement(ElementName = "org-id-result")]
        public Orgidresult Orgidresult { get; set; }
        [XmlAttribute(AttributeName = "version")]
        public string Version { get; set; }
    }

    [XmlRoot(ElementName = "BCAservice-data")]
    public class BCAservicedata
    {
        [XmlElement(ElementName = "response")]
        public Response Response { get; set; }
    }

    [XmlRoot(ElementName = "BCAservice")]
    public class BCAservice
    {
        [XmlElement(ElementName = "BCAservice-code")]
        public string BCAservicecode { get; set; }
        [XmlElement(ElementName = "BCAservice-code-version")]
        public string BCAservicecodeversion { get; set; }
        [XmlElement(ElementName = "BCAservice-client-ref")]
        public string BCAserviceclientref { get; set; }
        [XmlElement(ElementName = "BCAservice-data")]
        public BCAservicedata BCAservicedata { get; set; }
    }

    [XmlRoot(ElementName = "BCAservices")]
    public class BCAservices
    {
        [XmlElement(ElementName = "BCAservice")]
        public BCAservice BCAservice { get; set; }
    }

    [XmlRoot(ElementName = "BCAmessage")]
    public class BCAmessage
    {
        [XmlElement(ElementName = "BCAservices")]
        public BCAservices BCAservices { get; set; }
        [XmlAttribute(AttributeName = "service-request-id")]
        public string Servicerequestid { get; set; }
        [XmlAttribute(AttributeName = "type")]
        public string Type { get; set; }
    }

I am using the following method to deserialize the xml however I cant seem to return it so I can out put it to my SSIS out put buffers.

 private void DeserializeStanData(string stanresponse)
    {

        string xmlStanResponseText = Convert.ToString(Variables.xmlresponse);

        XmlSerializer serializerStan = new XmlSerializer(typeof(BCAmessage));



        using (TextReader xmlResponseText = new StringReader(xmlStanResponseText))
        {
            var dsResult = serializerStan.Deserialize(xmlResponseText);

        }

        return dsResult;


    }

This is how I am trying to process the deserialized xml to output buffer.

public override void CreateNewOutputRows()
{
    var xmlStanResponse = Variables.xmlresponse;
    System.Windows.Forms.MessageBox.Show(Convert.ToString(xmlStanResponse));

    DeserializeStanData(Convert.ToString(xmlStanResponse));

    ResponseDataBuffer.AddRow();
    ResponseDataBuffer.MatchName

}

This is the base XML example.

<?xml version="1.0"?> 
<BCAmessage service-request-id="11140285673" type="RESPONSE">
    <BCAservices>
        <BCAservice>
            <BCAservice-code>BCA020</BCAservice-code>
            <BCAservice-code-version>V00</BCAservice-code-version>
            <BCAservice-client-ref/>
            <BCAservice-data>
                <response version="1-36-1">
                    <org-id-result>
                        <match-name>PATHWAYS TECHNOLOGY CONSULTING PTY LTD</match-name>
                        <organisation-number>103944976</organisation-number>
                        <organisation-type>P/L</organisation-type>
                        <australian-business-number>64103944976</australian-business-number>
                        <organisation-number-heading>ACN</organisation-number-heading>
                        <state>QLD</state>
                        <asic-report>
                            <asic-name>PATHWAYS TECHNOLOGY CONSULTING PTY LTD</asic-name>
                            <asic-organisation-number>103944976</asic-organisation-number>
                            <asic-a-b-n>64103944976</asic-a-b-n>
                            <asic-organisation-number-heading>ACN</asic-organisation-number-heading>
                            <asic-type>APTY</asic-type>
                            <asic-status>REGD</asic-status>
                            <asic-jurisdiction>ASIC</asic-jurisdiction>
                            asic-locality>FORTITUDE VALLEY</asic-locality>
                            <asic-address-state>QLD</asic-address-state>
                            <asic-postcode>4006</asic-postcode>
                        </asic-report>
                        <australian-business-register-report>
                            <ASICNumber>103944976</ASICNumber>
                            <ABN>64103944976</ABN>
                            <ABNStatus>ACT</ABNStatus>
                            <ABNStatusFromDate>2003-03-04</ABNStatusFromDate>
                            <EntityTypeInd>PRV</EntityTypeInd>
                            <Non-IndividualName>PATHWAYS TECHNOLOGY CONSULTING PTY LTD</Non-IndividualName>
                            <BusinessAddressState>QLD</BusinessAddressState>
                            <BusinessAddressPostcode>4006</BusinessAddressPostcode>
                            <GSTStatus>ACT</GSTStatus>
                            <GSTStatusFromDate>2003-03-04</GSTStatusFromDate>
                            <TradingName>INTELLIGENT PATH WAYS</TradingName>
                        </australian-business-register-report>
                        <bureau-report>
                            <address first-reported="2011-04-05" type="trading-address">
                                <unit-number>9</unit-number>
                                <street-number>515</street-number>
                                <property>GREEN SQUARE NORTH TOWER</property>
                                <street-name>ST PAULS</street-name>
                                <street-type code="TCE"/>
                                <suburb>FORTITUDE VALLEY</suburb>
                                <state>QLD</state>
                                <postcode>4006</postcode>
                            </address>
                            <last-search-date>2011-05-24</last-search-date>
                            <nature-of-business/>
                            <ErrorMsg/>
                            <WarningMsg/>
                            <bureau-reference>611723538</bureau-reference>
                            <bca-company-type>P/L</bca-company-type>
                            <bca-name>PATHWAYS TECHNOLOGY CONSULTING</bca-name>
                        </bureau-report>
                    </org-id-result>
                </response>
            </BCAservice-data>
        </BCAservice>
    </BCAservices>
</BCAmessage>

Solution

  • Based on your error, it seems the problem is with your function DeserializeStanData.

    The return type is void but you return dsResult. Also, the scope of the dsResult variable is the using statement only. You can't refer to it outside the using statement, because it's declared in there.

    You could try changing your function as follows:

    private BCAmessage DeserializeStanData(string stanresponse)
    {
        string xmlStanResponseText = Convert.ToString(Variables.xmlresponse);
        XmlSerializer serializerStan = new XmlSerializer(typeof(BCAmessage));
        BCAmessage dsResult;
        using (TextReader xmlResponseText = new StringReader(xmlStanResponseText))
        {
            dsResult = serializerStan.Deserialize(xmlResponseText);
        }
    
        return dsResult;
    }