即使它们存在于C#中的XDocument中,也没有找到后代

时间:2016-05-26 10:47:18

标签: c# xml

我遇到了使用特定名称获得后代的问题。我有一些基本上由这些元素组成的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>

我正在尝试使用名称来访问所有元素:实体以及稍后我想要浏览所有这些元素并从其后代获取名称为&#34; Reason&#34;的值。 但是在这一行中找不到实体元素:

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"

但没有成功。 有人看到我做错了什么吗?

1 个答案:

答案 0 :(得分: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);
}