簡體   English   中英

即使后代在C#中的XDocument中也沒有找到

[英]Descendants not found even if they exist in XDocument in C#

我在獲取具有特定名稱的后代時遇到問題。 我的XML基本上由很多這樣的元素組成:

<?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>

我正在嘗試使用名稱:Entity,后來又想遍歷所有元素,並從名稱為“ Reason”的子孫中獲取值。 但是以下行找不到任何Entity元素:

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"

但是沒有成功。 有人看到我做錯了嗎?

您可以使用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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM