Home > Back-end >  get properties and attributes from XML using Linq in C#
get properties and attributes from XML using Linq in C#

Time:10-02

I have a generated XML and need to extract properties attributes and 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>}  

I tried many solutions like


            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}");
            }
        }

So I need to extract the properties like "trustAnchor" and the BooleanValue like "true". In order to get a list of those for all certificates in the store. But they all result in null. So I am making the same mistake in all cases. Any ideas how to make this work?

CodePudding user response:

Please try the following solution.

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 }

CodePudding user response:

The attributes in your sample are not in a namespace so PropertyElement.Attribute(ns "Key") is certainly wrong if that is the ns you also use to select the element nodes, it should simply be PropertyElement.Attribute("Key").Value == "trustAnchor" or (string)PropertyElement.Attribute("key") == "trustAnchor".

  • Related