簡體   English   中英

在 C# 中使用 Linq 從 XML 獲取屬性和特性

[英]get properties and attributes from XML using Linq in C#

我有一個生成的 XML 並且需要提取屬性屬性和 BooleanValues

{<l7:Resource xmlns:l7="http:not shown for security reasons">
  <l7:TrustedCertificate id="not shown for security reasons" version="0">
    <l7:Name>not shown for security reasons</l7:Name>
    <l7:CertificateData>
      <l7:IssuerName>not shown for security reasons</l7:IssuerName>
      <l7:SerialNumber>not shown for security reasons</l7:SerialNumber>
      <l7:SubjectName>not shown for security reasons</l7:SubjectName>
      <l7:Encoded>not shown for security reasons</l7:Encoded>
    </l7:CertificateData>
    <l7:Properties>
      <l7:Property key="revocationCheckingEnabled">
        <l7:BooleanValue>true</l7:BooleanValue>
      </l7:Property>
      <l7:Property key="trustAnchor">
        <l7:BooleanValue>true</l7:BooleanValue>
      </l7:Property>
      <l7:Property key="trustedAsSamlAttestingEntity">
        <l7:BooleanValue>false</l7:BooleanValue>
      </l7:Property>
      <l7:Property key="trustedAsSamlIssuer">
        <l7:BooleanValue>false</l7:BooleanValue>
      </l7:Property>
      <l7:Property key="trustedForSigningClientCerts">
        <l7:BooleanValue>true</l7:BooleanValue>
      </l7:Property>
      <l7:Property key="trustedForSigningServerCerts">
        <l7:BooleanValue>false</l7:BooleanValue>
      </l7:Property>
      <l7:Property key="trustedForSsl">
        <l7:BooleanValue>false</l7:BooleanValue>
      </l7:Property>
      <l7:Property key="verifyHostname">
        <l7:BooleanValue>false</l7:BooleanValue>
      </l7:Property>
    </l7:Properties>
  </l7:TrustedCertificate>
</l7:Resource>}  

我嘗試了很多解決方案,比如


            public static void GetPropertiesWithAttributes(XElement certlist, XNamespace ns, IEnumerable<XElement> certProperties)
        {


            var propellor = from prop in certlist.Elements(ns + "Properties").Take(10)

                            select new
                            {
                                propAtt = (string)prop.Elements(ns + "Property").SingleOrDefault(PropertyElement => PropertyElement.Attribute(ns + "Key").Value == "trustAnchor"),

                                propBool = prop.Element(ns + "BooleanValue").Value
                            };

            foreach (var value in propellor)
            {
                Console.WriteLine($"IENUMERABLE: {value}");
            }
        }

所以我需要提取“trustAnchor”之類的屬性和“true”之類的布爾值。 為了獲得商店中所有證書的列表。 但它們的結果都是 null。所以我在所有情況下都犯了同樣的錯誤。 任何想法如何使這項工作?

請嘗試以下解決方案。

c#

void Main()
{
    XDocument doc = XDocument.Parse(@"<l7:Resource xmlns:l7='http:not shown for security reasons'>
            <l7:TrustedCertificate id='not shown for security reasons' version='0'>
                <l7:Name>not shown for security reasons</l7:Name>
                <l7:CertificateData>
                    <l7:IssuerName>not shown for security reasons</l7:IssuerName>
                    <l7:SerialNumber>not shown for security reasons</l7:SerialNumber>
                    <l7:SubjectName>not shown for security reasons</l7:SubjectName>
                    <l7:Encoded>not shown for security reasons</l7:Encoded>
                </l7:CertificateData>
                <l7:Properties>
                    <l7:Property key='revocationCheckingEnabled'>
                        <l7:BooleanValue>true</l7:BooleanValue>
                    </l7:Property>
                    <l7:Property key='trustAnchor'>
                        <l7:BooleanValue>true</l7:BooleanValue>
                    </l7:Property>
                    <l7:Property key='trustedAsSamlAttestingEntity'>
                        <l7:BooleanValue>false</l7:BooleanValue>
                    </l7:Property>
                    <l7:Property key='trustedAsSamlIssuer'>
                        <l7:BooleanValue>false</l7:BooleanValue>
                    </l7:Property>
                    <l7:Property key='trustedForSigningClientCerts'>
                        <l7:BooleanValue>true</l7:BooleanValue>
                    </l7:Property>
                    <l7:Property key='trustedForSigningServerCerts'>
                        <l7:BooleanValue>false</l7:BooleanValue>
                    </l7:Property>
                    <l7:Property key='trustedForSsl'>
                        <l7:BooleanValue>false</l7:BooleanValue>
                    </l7:Property>
                    <l7:Property key='verifyHostname'>
                        <l7:BooleanValue>false</l7:BooleanValue>
                    </l7:Property>
                </l7:Properties>
            </l7:TrustedCertificate>
        </l7:Resource>");

    XNamespace ns = doc.Root.GetNamespaceOfPrefix("l7");

    var propellor = doc.Descendants(ns + "Property")
        .Where(d => d.Attribute("key").Value.Equals("trustAnchor"))
        .Take(10)
        .Select(prop => new
        {
            propAtt = (string)prop.Attribute("key").Value,
            propBool = prop.Element(ns + "BooleanValue").Value
        });

    foreach (var value in propellor)
    {
        Console.WriteLine($"IENUMERABLE: {value}");
    }
}

Output

IENUMERABLE: { propAtt = trustAnchor, propBool = true }

示例中的屬性不在命名空間中,因此PropertyElement.Attribute(ns + "Key")肯定是錯誤的,如果那是你也用於 select 元素節點的ns ,它應該只是PropertyElement.Attribute("Key").Value == "trustAnchor"(string)PropertyElement.Attribute("key") == "trustAnchor"

暫無
暫無

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

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