0

I am reading xml coming from https://www.usaspending.gov/fpds/fpds.php?detail=c&fiscal_year=2015&stateCode=TX&max_records=10 using the xsd from https://data.usaspending.gov/content/xsd/contracts/PrimeBasicContracts.xsd.

I have converted this to a class using xsd.exe. When I read the object using this code

XmlSerializer ser = new XmlSerializer(typeof(usaspendingSearchResults));

var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var spendingResults = ser.Deserialize(fs) as usaspendingSearchResults;
fs.Dispose();

It will read all top level elements but all nested properties are null. How can I get the Deserialize to populate all elements?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jim
  • 808
  • 2
  • 11
  • 28

1 Answers1

1

Your problem is that the properties of the doc element specified by the XSD file do not remotely match those actually in the XML file. You can check this if you upload your XML and XSD to an online validator, such as http://www.freeformatter.com/xml-validator-xsd.html. If you do, you will see a bunch of errors like:

Cvc-complex-type.2.4.a: Invalid Content Was Found Starting With Element 'obligatedAmount'. One Of '{"http://www.usaspending.gov/schemas/":record_count}' Is Expected.. Line '1', Column '612'.
Cvc-complex-type.2.4.a: Invalid Content Was Found Starting With Element 'obligatedAmount'. One Of '{"http://www.usaspending.gov/schemas/":record_count}' Is Expected.. Line '1', Column '10492'.
Cvc-complex-type.2.4.a: Invalid Content Was Found Starting With Element 'obligatedAmount'. One Of '{"http://www.usaspending.gov/schemas/":record_count}' Is Expected.. Line '1', Column '20216'.
... Remainder snipped for brevity.

The errors arise because, according to the XSD, the <doc> element should have the following child elements:

    <xs:element ref="schemas:record_count"/>
    <xs:element ref="schemas:UniqueTransactionID" minOccurs="0"/>
    <xs:element ref="schemas:TransactionStatus" minOccurs="0"/>
    <xs:element ref="schemas:AwardType"/>
    <xs:element ref="schemas:ContractPricing"/>
    <xs:element ref="schemas:ContractingAgency"/>
    <xs:element ref="schemas:DUNSNumber"/>
    <xs:element ref="schemas:parentDUNSNumber"/>
    <xs:element ref="schemas:DateSigned"/>
    <xs:element ref="schemas:ContractDescription"/>
    <xs:element ref="schemas:ReasonForModification"/>
    <xs:element ref="schemas:DollarsObligated"/>
    <xs:element ref="schemas:ExtentCompeted"/>
    <xs:element ref="schemas:FiscalYear"/>
    <xs:element ref="schemas:TransactionNumber"/>
    <xs:element ref="schemas:AgencyID" minOccurs="0"/>
    <xs:element ref="schemas:FundingAgency"/>
    <xs:element ref="schemas:IDVAgency" minOccurs="0"/>
    <xs:element ref="schemas:IDVProcurementInstrumentID"/>
    <xs:element ref="schemas:MajorAgency"/>
    <xs:element ref="schemas:ContractingAgencyCode"/>
    <xs:element ref="schemas:MajorFundingAgency"/>
    <xs:element ref="schemas:ModificationNumber"/>
    <xs:element ref="schemas:PSCCategoryCode"/>
    <xs:element ref="schemas:ParentRecipientOrCompanyName"/>
    <xs:element ref="schemas:PlaceofPerformanceCongDistrict"/>
    <xs:element ref="schemas:PlaceofPerformanceState"/>
    <xs:element ref="schemas:PlaceofPerformanceZipCode"/>
    <xs:element ref="schemas:PrincipalNAICSCode"/>
    <xs:element ref="schemas:ProcurementInstrumentID"/>
    <xs:element ref="schemas:PrincipalPlaceCountyOrCity"/>
    <xs:element ref="schemas:ProductorServiceCode"/>
    <xs:element ref="schemas:ProgramSource"/>
    <xs:element ref="schemas:ProgramSourceAccountCode"/>
    <xs:element ref="schemas:ProgramSourceAgencyCode"/>
    <xs:element ref="schemas:ProgramSourceDescription"/>
    <xs:element ref="schemas:RecipientAddressLine123"/>
    <xs:element ref="schemas:RecipientCity"/>
    <xs:element ref="schemas:RecipientCongressionalDistrict"/>
    <xs:element ref="schemas:RecipientCountyName"/>
    <xs:element ref="schemas:RecipientName"/>
    <xs:element ref="schemas:RecipientOrContractorName"/>
    <xs:element ref="schemas:RecipientState"/>
    <xs:element ref="schemas:RecipientZipCode"/>
    <xs:element ref="schemas:TypeofSpending"/>
    <xs:element ref="schemas:TypeofTransaction"/>
    <xs:element ref="schemas:VendorName" minOccurs="0"/>

In fact, the <doc> elements in the XML have a different and far larger set of children, including

<doc spendingCategory="Contracts">
  <obligatedAmount>169335580</obligatedAmount>
  <baseAndExercisedOptionsValue>169335580.00</baseAndExercisedOptionsValue>
  <baseAndAllOptionsValue>169335580.00</baseAndAllOptionsValue>
  <maj_agency_cat>9700</maj_agency_cat>
  <mod_agency>1700</mod_agency>
  <maj_fund_agency_cat>9700: DEPT OF DEFENSE</maj_fund_agency_cat>
  <contractingOfficeAgencyID>1700: DEPT OF THE NAVY</contractingOfficeAgencyID>
  <contractingOfficeID>N00019:_x0020_NAVAL_x0020_AIR_x0020_SYSTEMS_x0020_COMMAND</contractingOfficeID>
  <fundingRequestingAgencyID>1700</fundingRequestingAgencyID>
  <fundingRequestingOfficeID>N00019</fundingRequestingOfficeID>
  <signedDate>2014-12-12T00:00:00.000</signedDate>
  <effectiveDate>2014-12-12T00:00:00.000</effectiveDate>
  <currentCompletionDate>2021-12-31T00:00:00.000</currentCompletionDate>
  <ultimateCompletionDate>2021-12-31T00:00:00.000</ultimateCompletionDate>
  ... Remainder snipped for brevity.

You should locate the correct XSD for your XML. If you can't but need to proceed anyway, you could try to generate your classes directly from the XML.

dbc
  • 104,963
  • 20
  • 228
  • 340