I try to use Linq to parse a XML in C#.
this is the XML I am parsing:
<Credit>
<LoanApp>
<LoanAppRq PaymentCall="True" Personal="True" Type="Finance">
<Applicant>
<Personal>
<Individuals>
<Individual Type="Applicant">
<GivenName>
<FirstName>test</FirstName>
<LastName>tester</LastName>
</GivenName>
<ContactInfo>
<Address Type="Current">
<StreetNumber>6</StreetNumber>
<StreetName>alton AVE</StreetName>
<City>PHILADELPHIA</City>
<State>PA</State>
<Zip>19142</Zip>
<TimeAtLocation>
<Year>6</Year>
<Month>0</Month>
</TimeAtLocation>
</Address>
<Address Type="Previous">
<StreetNumber>83</StreetNumber>
<StreetName>Main Street</StreetName>
<StreetExtra>12</StreetExtra>
<City>Irvine</City>
<State>CA</State>
<Zip>92695</Zip>
<Country>USA</Country>
<TimeAtLocation>
<Year/>
<Month>3</Month>
</TimeAtLocation>
</Address>
</ContactInfo>
and this is my code to parse it:
parsed_xml.LoadXml(dto.xml);
XElement xelement = XElement.Load(stream);
IEnumerable<XElement> Credit = xelement.Elements();
foreach (var item in Credit)
{
dt.BORROWERFIRSTNAME = item.Element("LoanApp").Element("LoanAppRq").Element("Applicant").Element("Personal").Element("Individuals").Element("Individual").Element("GivenName").Element("FirstName").Value;
dt.BORROWERLASTNAME= item.Element("LoanApp").Element("LoanAppRq").Element("Applicant").Element("Personal").Element("Individuals").Element("Individual").Element("GivenName").Element("LastName").Value;
}
this code give me the Firstname and lastname.
I have used this website as a reference for learning.
For deep XML hierarchies like yours without complex namespaces, I prefer XPathSelectElements
in the System.Xml.XPath
namespace.
Assuming that your xelement
element has exactly the XML shown in your question, you can do:
foreach (var individual in xelement.XPathSelectElements("LoanApp/LoanAppRq/Applicant/Personal/Individuals/Individual"))
{
// Get the first and last name.
var BORROWERFIRSTNAME = (string)individual.XPathSelectElement("GivenName/FirstName");
var BORROWERLASTNAME = (string)individual.XPathSelectElement("GivenName/LastName");
// Get the XElement for the current address.
var currentAddress = individual.XPathSelectElements("ContactInfo/Address[@Type='Current']")
.FirstOrDefault();
// Extract its properties, checking for a missing current address if necessary.
var currentZip = (currentAddress == null ? null : (string)currentAddress.Element("Zip"));
// Get the XElement for the previous address.
var previousAddress = individual.XPathSelectElements("ContactInfo/Address[@Type='Previous']")
.FirstOrDefault();
// Extract its properties, checking for a missing previous address if necessary.
var previousZip = (previousAddress == null ? null : (string)previousAddress.Element("Zip"));
// Process the borrower names and addresses as required.
}
The equivalent in pure Linq to XML is:
foreach (var individual in xelement.Elements("LoanApp")
.Elements("LoanAppRq")
.Elements("Applicant")
.Elements("Personal")
.Elements("Individuals")
.Elements("Individual"))
{
// Get the first and last name.
var BORROWERFIRSTNAME = (string)individual.Elements("GivenName")
.Elements("FirstName")
.FirstOrDefault();
var BORROWERLASTNAME = (string)individual.Elements("GivenName")
.Elements("LastName")
.FirstOrDefault();
// Get the XElement for the current address.
var currentAddress = individual.Elements("ContactInfo").Elements("Address").Where(e => (string)e.Attribute("Type") == "Current").FirstOrDefault();
// Extract its properties, checking for a missing current address if necessary.
var currentZip = (currentAddress == null ? null : (string)currentAddress.Element("Zip"));
// Get the XElement for the previous address.
var previousAddress = individual.Elements("ContactInfo").Elements("Address").Where(e => (string)e.Attribute("Type") == "Previous").FirstOrDefault();
// Extract its properties, checking for a missing previous address if necessary.
var previousZip = (previousAddress == null ? null : (string)previousAddress.Element("Zip"));
// Process the borrower names and addresses as required.
}
As you can see, it looks a bit more complicated.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments