2016-05-26 55 views
0

我在獲取具有特定名稱的後代時遇到問題。我有一個基本是由大量的這種元素的休XML:即使它們存在於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> 

我試圖達到與名稱的所有元素:實體,後來我想通過所有的人,從他們的名稱爲後代得到的值「原因」。 但實體要素的非發現這一行:

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是否正確進入,或者以後沒有其他事情導致您丟失數據。運行該方法的其餘部分確實會導致一個原因('國際')。 – Chris

+0

您需要提供[mcve]。根據上面的兩條評論,這是有效的,所以沒有太多的答案。順便說一句,你檢查null的東西都不會是空的。你的代碼可以簡化爲'var reasons = xmlDoc.Descendants(nameSpace +「Reason」)。選擇(x => x.Value).ToList()'。 –

回答

0

您可以使用LINQ用的localName進行過濾:

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); 
} 
相關問題