XDocument loaded = XDocument.Parse(mainXML);
var ContactInfo =
from Contact in loaded.Descendants("Contacts")
select new
{
ContactID = Contact.Attribute("Id").Value,
Displayed = Contact.Attribute("Displayed").Value,
AddressList = Contact.Descendants("AddressList"),
CellPhone = Contact.Element("CellPhone").Value,
Citation = Contact.Element("Citation").Value,
DrivingLicense = Contact.Element("DrivingLicense").Value,
Fax = Contact.Element("Fax").Value,
HomePhone = Contact.Element("HomePhone").Value,
};
У меня есть некоторые другие имена элементов в mainXML
, такие как WorkPhone
, которые существуют не во всех элементах. Если я добавлю эти свойства и запущу его, он получит NullPointerException
s из-за этого.
<Contacts Id="firstcontact" Displayed="False">
<IsCaller />
<Role />
<RelationToRole />
<PersonInfo>
<DBA />
<DateOfBirth />
<FirstName>TRinya</FirstName>
<LastName />
<MiddleName />
</PersonInfo>
<AddressList>
<AddressLine1 />
<City />
<Email />
<EmailPreferred />
<State />
<ZipCode />
<pyCountry>USA</pyCountry>
</AddressList>
<Citation />
<DrivingLicense />
<Language />
<InterpreterNeeded />
<Pedestrian />
<PersonalInjury>
<InjuredYesNoUnk />
<TypeOfInjury />
<InjuredAffectedArea />
<InjuryDescription />
</PersonalInjury>
<PositionInVehicle />
<HomePhone />
<CellPhone />
<pyWorkPhone />
<Fax />
<PrimaryPhone />
<PrimaryPhoneType />
</Contacts>