Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;格式错误的参考元素“;对XML文件进行签名时出现异常_C#_Xml_Digital Signature - Fatal编程技术网

C# &引用;格式错误的参考元素“;对XML文件进行签名时出现异常

C# &引用;格式错误的参考元素“;对XML文件进行签名时出现异常,c#,xml,digital-signature,C#,Xml,Digital Signature,我试图用对KeyInfo节点的引用对XML文档进行签名,但在调用方法“ComputeSignature”后,我得到了“格式错误的引用元素”异常 这是我的代码: signedXml.SigningKey = certificate.PrivateKey; if (!signParameters.IncludeCertificateInSignature) return; var certificateKeyInfo = new KeyInfo();

我试图用对KeyInfo节点的引用对XML文档进行签名,但在调用方法“ComputeSignature”后,我得到了“格式错误的引用元素”异常

这是我的代码:

        signedXml.SigningKey = certificate.PrivateKey;

        if (!signParameters.IncludeCertificateInSignature) return;
        var certificateKeyInfo = new KeyInfo();
        certificateKeyInfo.AddClause(new KeyInfoX509Data(certificate));
        signedXml.KeyInfo = certificateKeyInfo;
        signedXml.KeyInfo.Id = "xmldsig-keyinfo";

        signedXml.AddReference(new Reference("#xmldsig-keyinfo"));

如果我删除了“#xmldsig keyinfo”,它可以工作,但是我得到了整个文档的引用,我需要带有keyinfo标记的引用。

我最终找到了答案,我无法添加KeyInfoId引用,因为XMLDoc中还没有创建xmlement;因此,我实现了SigneXML类并直接从keyInfo返回XML

public override XmlElement GetIdElement(XmlDocument doc, string id)
{
    if (String.Compare(id, this.KeyInfo.Id, StringComparison.OrdinalIgnoreCase) == 0)
        return this.KeyInfo.GetXml();
    else
        return base.GetIdElement(doc, id);
}

我希望有帮助

就我而言,在被困数天之后。这是Reference.uri。我把它保存为“#1”。XML是一种新技术

<getTest>
<item ID="1">
<Seed>123</Seed>
</item>
</getTest>
<getTest>
<item ID="test">
<Seed>123</Seed>
</item>
</getTest>
KB3140745
KB3140768
KB3140743
KB3073930