Here is a snippet of my XML file it contains person information and address information:
<?xml version="1.0" encoding="UTF-8"?><!--Sex Offender Data file Generated by FDLE --><sopde:RegisteredPersonDataExchange xsi:schemaLocation="http://fdle.gov/niem/SexualOffenderPredatorDataExchange/exchange/1.0 file:///C:/Workspaces/MyEclipse/SexPred_Exchange/src/resources/Exchange_Schema.xsd" xmlns:sc="http://fdle.gov/niem/SexualOffenderPredatorDataExchange/extensioncodelist/1.0" xmlns:se="http://fdle.gov/niem/SexualOffenderPredatorDataExchange/extension/1.0" xmlns:sopde="http://fdle.gov/niem/SexualOffenderPredatorDataExchange/exchange/1.0" xmlns:s="http://release.niem.gov/niem/structures/3.0/" xmlns:j="http://release.niem.gov/niem/domains/jxdm/5.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://release.niem.gov/niem/niem-core/3.0/"><j:RegisteredPerson s:id="SOP_NBR61553"><RoleOfPerson><PersonBirthDate><Date>1970-08-11</Date></PersonBirthDate><PersonName><PersonGivenName>TOMMY</PersonGivenName><PersonMiddleName/><PersonSurName>SMITH</PersonSurName><PersonNameSuffixText/></PersonName><sc:PersonRaceCode>B</sc:PersonRaceCode><j:PersonSexCode>M</j:PersonSexCode></RoleOfPerson> -<j:RegisteredOffenderIdentification><IdentificationID>61553</IdentificationID><sc:RegisteredPersonIdInfoCategory>SOPS_PNO</sc:RegisteredPersonIdInfoCategory></j:RegisteredOffenderIdentification></j:RegisteredPerson><j:RegisteredPerson s:id="SOP_NBR61304"><RoleOfPerson><PersonBirthDate><Date>1968-03-22</Date></PersonBirthDate><PersonName><PersonGivenName>Scott</PersonGivenName><PersonMiddleName/><PersonSurName>Gates</PersonSurName><PersonNameSuffixText/></PersonName><sc:PersonRaceCode>W</sc:PersonRaceCode><j:PersonSexCode>M</j:PersonSexCode></RoleOfPerson><j:RegisteredOffenderIdentification><IdentificationID>61304</IdentificationID><sc:RegisteredPersonIdInfoCategory>SOPS_PNO</sc:RegisteredPersonIdInfoCategory></j:RegisteredOffenderIdentification></j:RegisteredPerson><se:SopsOffenderAddressList><se:SopsOffenderAddress PersonRef="SOP_NBR61553"><LocationStreet><StreetFullText>535 APPLEYARD DRIVE</StreetFullText><StreetExtensionText>LEON COJAIL</StreetExtensionText></LocationStreet><LocationCityName>TALLAHASSEE</LocationCityName><sc:LocationNonFLCounty/><sc:LocationUSStateCode>FL</sc:LocationUSStateCode><LocationPostalCode>32304</LocationPostalCode><LocationPostalExtensionCode>3801</LocationPostalExtensionCode><sc:AddressCategoryCode>TEMP</sc:AddressCategoryCode><se:SOPSAddressIdentification><IdentificationID>644638</IdentificationID></se:SOPSAddressIdentification></se:SopsOffenderAddress>
As you can see I have several Name Spaces, such as j: s: and sc: I am trying to loop thru the data and build a CSV file using this code snippet:
StringBuilder sb1 = new StringBuilder(); string delimiter = ","; XDocument.Load(@"D:\FDLE_SO_DataPull\Sample.xml").Descendants(j+"RegisteredPerson") .ToList().ForEach(element => sb1.Append( element.Element(j + "RegisteredPerson").Attribute(s + "id").Value + delimiter + element.Element(j + "RegisteredPerson").Element("RoleOfPerson").Element("PersonBirthDate").Element("Date").Value + delimiter + element.Element("Registered").Element("RoleOfPerson").Element("PersonName").Element("PersonGivenName").Value + delimiter + element.Element("Registered").Element("RoleOfPerson").Element("PersonName").Element("PersonMiddleName").Value + delimiter + element.Element("Registered").Element("RoleOfPerson").Element("PersonName").Element("PersonSurName").Value + delimiter + element.Element("Registered").Element("RoleOfPerson").Element("PersonRaceCode").Value + delimiter + element.Element("Registered").Element("RoleOfPerson").Element("PersonSexCode").Value + "r/n")); StreamWriter sw = new StreamWriter(@"D:\FDLE_SO_DataPull\Result.csv"); sw.WriteLine(sb1.ToString()); sw.Close();I have stopped the code short without gathering the data in address node, but would also need to access this data and include in the csv. When I try to run the code I get an error stating {"Object reference not set to an instance of an object."} it is obvious I am not doing something right to get my data. Can someone explain what the problem may be?