gpt4 book ai didi

c# - 在 C# 中使用 Linq 从 XML 获取属性和属性

转载 作者:行者123 更新时间:2023-12-04 16:58:48 25 4
gpt4 key购买 nike

我有一个生成的 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”这样的BooleanValue。为了获得商店中所有证书的列表。
但它们都导致 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}");
}
}
输出
IENUMERABLE: { propAtt = trustAnchor, propBool = true }

关于c# - 在 C# 中使用 Linq 从 XML 获取属性和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69411326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com