简体   繁体   中英

linq to xml navigate through xml c#

I have some XML and need to be able to read the data within.

A sample of the XML is

 <?xml version="1.0" ?>
<ConsumeLeadRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LeadType>Mortgage</LeadType>
  <LeadXml>
    <ns1:LeadAssigned xmlns:ns1="http://YaddaYadda" xmlns:ns0="http://YaddaYadda" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <ns0:Lead>
        <Reference>1234</Reference>
        <Product>
          <Mnemonic>Mortgage</Mnemonic>
          <Description>Mortgage Leads</Description>
          <SubType>Other</SubType>
        </Product>
        <ExtendedInfo>
          <Mortgage>
            <MortgageValue>75000</MortgageValue>
            <MortgageValueLowerBound>1</MortgageValueLowerBound>
            <MortgageValueUpperBound>500</MortgageValueUpperBound>
            <PropertyValue>100000</PropertyValue>
            <PropertyValueLowerBound>1</PropertyValueLowerBound>
            <PropertyValueUpperBound>500</PropertyValueUpperBound>
            <Adverse>false</Adverse>
            <FirstTime>false</FirstTime>
          </Mortgage>
        </ExtendedInfo>
        <Applicants>
          <Applicant1>
            <Title>Mr</Title>
            <Forename>SampleForename</Forename>
            <Surname>SampleSurname</Surname>
            <DateOfBirth>1903-02-01</DateOfBirth>
            <Smoker>false</Smoker>
            <TelephoneNumbers>
              <TelephoneNumber>
                <Number>01244123456</Number>
                <Type>Mobile</Type>
              </TelephoneNumber>
            </TelephoneNumbers>
            <EmailAddresses>
              <EmailAddress>
                <Email>test@moneysupermarket.com</Email>
                <Type>Business</Type>
              </EmailAddress>
            </EmailAddresses>
            <Addresses>
              <Address>
                <Street>Sample Street</Street>
                <District>Sample District</District>
                <Town>Sample Town</Town>
                <County>Sample County</County>
                <Postcode>CH53UZ</Postcode>
                <Type>Home</Type>
              </Address>
            </Addresses>
          </Applicant1>
        </Applicants>
      </ns0:Lead>
      <Assignment>
        <Price>20</Price>
        <AssignmentDateTime>2010-02-01T00:00:00</AssignmentDateTime>
        <Subscription>
          <Reference>1234</Reference>
          <Subscriber>
            <Title>Mr</Title>
            <Forename>SampleForename</Forename>
            <Surname>SampleSurname</Surname>
          </Subscriber>
        </Subscription>
        <Account>
          <Reference>1234</Reference>
          <CompanyName>Sample Company</CompanyName>
        </Account>
        <LeadType>SampleLeadType</LeadType>
        <TerritoryName>UNITED KINGDOM</TerritoryName>
      </Assignment>
    </ns1:LeadAssigned>
  </LeadXml>
  <AuthenticationUsername>Username</AuthenticationUsername>
  <AuthenticationPassword>Password</AuthenticationPassword>
</ConsumeLeadRequest>

Using Linq to XML how do i navigate to the items?

Thanks Sp

I have tried a few things like

 XDocument Leads = XDocument.Load(@"C:\Users\Steven.Pentleton\AppData\Local\Temporary Projects\PAALeadImport\PAAExmple.xml");
        var Lead = (from L in Leads.Descendants("Lead")
                    select new { LeadType = (string)L.Element("Reference") }).ToList();
        var S = Lead.First();
        string T = S.LeadType;

Are you looking for XDcoument or XElement in linq

Namespace: using System.Xml.Linq ;

我猜您正在寻找Linq To Xml指南

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM