2016-05-26 10 views
0

特定の名前の子孫を取得する際に問題が発生しています。C#のXDocumentにも子孫が見つかりません

<?xml version="1.0" encoding="utf-8"?> 
<Search_Results xmlns="https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.5/OutputFile.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.5/OutputFile.xsd https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.5/OutputFile.xsd"> 
<Entity Record="28" ResultID="12460985"> 
    <GeneralInfo> 
     <EntityType>Individual</EntityType> 
     <Name>Jón Jónsson</Name> 
     <DOB>01/01/0001</DOB> 
     <DOBParsed /> 
     <AccountID>ABS-ASSOC-10-109</AccountID> 
     <IDLabel>Account ID</IDLabel> 
     <IDNumber>ABS-ASSOC-10-109</IDNumber> 
     <AddressType>Current</AddressType> 
     <PostalCode>Somalia</PostalCode> 
    </GeneralInfo> 
    <RecordDetailInfo> 
     <EntityType>Individual</EntityType> 
     <SearchDate>2016-05-13 09:53:50Z</SearchDate> 
     <Origin>Automatic Batch</Origin> 
     <FirstName>Jón</FirstName> 
     <LastName>Jónsson</LastName> 
     <FullName>Jón Jónsson</FullName> 
     <AdditionalInfo> 
      <Type>Date of Birth</Type> 
      <Information>01/01/0001</Information> 
     </AdditionalInfo> 
     <Addresses> 
      <Type>Current</Type> 
      <PostalCode>Somalia</PostalCode> 
     </Addresses> 
     <Identifications> 
      <Type>Account ID</Type> 
      <Number>ABS10-109</Number> 
     </Identifications> 
    </RecordDetailInfo> 
    <WatchList> 
     <Match ID="1"> 
      <EntityName>Jonsson</EntityName> 
      <EntityScore>96</EntityScore> 
      <BestName>Jonsson, Jon Orn</BestName> 
      <BestNameScore>96</BestNameScore> 
      <FileName>WorldCompliance - Full.BDF</FileName> 
      <SourceDate>2016-05-11 05:01:00Z</SourceDate> 
      <DistributionDate>2016-05-12 14:59:39Z</DistributionDate> 
      <ResultDate>2016-05-13 09:53:50Z</ResultDate> 
      <EntityUniqueID>WX0003219444</EntityUniqueID> 
      <MatchDetails> 
       <Entity Type="2"> 
        <Number>3219444</Number> 
        <Date>9/3/2012</Date> 
        <Reason>International</Reason> 
        <CheckSum>69185</CheckSum> 
        <Gender>Male</Gender> 
        <Name> 
         <First>Jon Orn</First> 
         <Last>Jonsson</Last> 
         <Full>Jon Orn Jonsson</Full> 
        </Name> 
        <Notes>Source.</Notes> 
        <Addresses> 
         <Address ID="1" Type="4"> 
          <Country>Iceland</Country> 
         </Address> 
        </Addresses> 
        <IDs> 
         <ID ID="1" Type="27"> 
          <Number>3219444</Number> 
         </ID> 
        </IDs> 
        <Descriptions> 
         <Description ID="1" Type="10"> 
          <Value>Honorary Consul of Iceland in Saskatchewan, Canada</Value> 
          <Notes>Starting 2002 Ending 2014</Notes> 
         </Description> 
         <Description ID="2" Type="22"> 
          <Value>Link to WorldCompliance Online Database</Value> 
          <Notes>Jonsson, Jon Orn | https://members.worldcompliance.com/metawatch2.aspx?id=e0399c29-7c5e-4674-874c-f36fdb19052e</Notes> 
         </Description> 
         <Description ID="3" Type="22"> 
          <Value>Sources of Record Information</Value> 
          <Notes>http://brunnur.mfa.is/interpro/utanr/HBvefur.nsf/Pages/IslSendiradIsl?OpenDocument&amp;amp | CountryNr=1(Canada)&amp;amp | Lang=44') | http://www.international.gc.ca/protocol-protocole/assets/pdfs/Diplomatic_List.pdf | http://www.inlofna.org/Elfros/newsletter%20January%202010.pdf | http://publications.gc.ca/collections/Collection/E12-3-2002E.pdf | http://www.onlygolfnews.com/golf-canada-saskatchewan/saskatchewan-golf-first-fort-lacrosse-ted-brandon-over-new-last-snow.htm | http://www.ops.gov.sk.ca/Consular-Officers</Notes> 
         </Description> 
        </Descriptions> 
       </Entity> 
      </MatchDetails> 
     </Match> 
    </WatchList> 
</Entity> 
</Search_Results> 

私は名前を持つすべての要素に到達しようとしています::エンティティと後で、私はそれらのすべてを通過し、名前とその子孫から値を取得したい私は基本的に、この要素の多くで作られているヒューXMLを持っています"理由"。 しかし、エンティティ要素の非は、このラインで発見された:

var entityList = xmlDoc.Descendants(nameSpace + "Entity").ToList(); 

これは、私が使用しています全体の方法です:

public static void GetIBANAndBicValuesFromXML(XDocument xmlDoc) 
    { 
     var reasons = new List<string>(); 
     XNamespace nameSpace = 
      "https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.5/"; 

     var entityList = xmlDoc.Descendants(nameSpace + "Entity").ToList(); 

     if (entityList != null) 
     { 
      foreach (var reason in entityList.Select(entity => entity.Elements(nameSpace + "Reason")) 
       .Where(reasonsList => reasonsList != null).SelectMany(reasonsList => reasonsList)) 
      { 
       string reasonValue = reason.Value; 
       reasons.Add(reasonValue); 
      } 
     } 
    } 

をそしてこれは、このメソッドの呼び出しです:

private static void Main(string[] args) 
{ 
    var xmlFile = "C:\\temp\\indi2.xml"; 
    var x = XDocument.Load("C:\\temp\\Individuals.xml"); 

    XMLParse.GetIBANAndBicValuesFromXML(x); 
} 

私もこのような名前空間を試しました:

"https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.5/OutputFile.xsd" 

しかし成功はありません。 誰かが私が間違ってやっていることを見ていますか?

+0

ないこのことができますかどうかわからが、ちょうど(https://support.bridgerinsight.lexisnexis.com/downloads/xsd/を使用していることを確認したかったです4.5/OutputFile.xsd)は、これをテストするときに期待通りに2つのエンティティノードを返します! – abrown

+0

与えられたXMLをドキュメントに入れ、正しい名前空間( 'https:// support.bridgerinsight.lexisnexis.com/downloads/xsd/4.5/OutputFile.xsd')を使うと、entityListに2つの項目があります。あなたは、あなたのXMLが正しく来ていることを確認する必要がありますか、それは後であなたがデータを失う原因となるものではありません。その方法の残りの部分を実行すると、一つの理由( 'International')が引き出されます。 – Chris

+0

あなたは[mcve]を提供する必要があります。上記の2つのコメントによれば、これはうまくいくので、答えはたくさんありません。さて、あなたがnullをチェックしているもののどれもが*常にnullになることはありません。コードを 'var reasons = xmlDoc.Descendants(nameSpace +" Reason ")に減らすことができます。(x => x.Value).ToList()'を選択します。 –

答えて

0

あなたがたLocalNameをフィルタリングするためのLINQを使用することができます。

string fileName = "1.txt"; 
var xDoc = XDocument.Load(fileName); 
var neededElements = xDoc.Descendants().Where(x => x.Name.LocalName == "Entity"); 
Console.WriteLine("Found {0} Entitys", neededElements.Count()); 
foreach(var el in neededElements) 
{ 
    Console.WriteLine(el); 
} 
関連する問題