【发布时间】:2017-06-26 02:48:31
【问题描述】:
我正在尝试反序列化在我们的数据库中存储为文本的 xml 报告。
我已经通过 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; }
}
我正在使用以下方法对 xml 进行反序列化,但我似乎无法将其返回,因此我可以将其放入我的 SSIS 输出缓冲区。
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;
}
这就是我尝试将反序列化的 xml 处理为输出缓冲区的方式。
public override void CreateNewOutputRows()
{
var xmlStanResponse = Variables.xmlresponse;
System.Windows.Forms.MessageBox.Show(Convert.ToString(xmlStanResponse));
DeserializeStanData(Convert.ToString(xmlStanResponse));
ResponseDataBuffer.AddRow();
ResponseDataBuffer.MatchName
}
这是基本的 XML 示例。
<?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>
【问题讨论】:
-
顺便说一句,在您的
DeserializeStanData方法中,使用string xmlStanResponseText = stanresponse;而不是string xmlStanResponseText = Convert.ToString(Variables.xmlresponse);不是更有意义吗? -
另外,根据您的 XML(也许一些示例 XML 会有所帮助),您可以尝试
XmlSerializer serializerStan = new XmlSerializer(typeof(List<BCAmessage>));,因为您的 XML 中可能有多个BCAmessage。 -
感谢@KeyurPATEL,您的第一点很公平。我会修改它。我还为 XML 添加了一个示例记录。
-
您创建的反序列化类似乎没问题,错误是什么(如果有)或者
dsResult是什么样的? -
return dsResult;的错误是 "名称 'dsResult' 在当前上下文中不存在
标签: c# xml ssis deserialization xml-deserialization