如何在c#中对XML文件进行正确签名?

如何在c#中对XML文件进行正确签名?,c#,xml,sign,C#,Xml,Sign,我有如下的源文件: <LicenseFile xmlns=""> <Object Id="Settings"> <ProductID xmlns="">P2</ProductID> <FirstName xmlns="">John</FirstName> <LastName xmlns="">Jackson</LastName> </Object&

我有如下的源文件:

<LicenseFile xmlns="">
<Object Id="Settings">
<ProductID xmlns="">P2</ProductID>
            <FirstName xmlns="">John</FirstName>
            <LastName xmlns="">Jackson</LastName>
</Object>
</LicenseFile>
    <LicenseFile xmlns="">
     <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
...
       <Object Id="Settings">
       <ProductID xmlns="">P2</ProductID>
                <FirstName xmlns="">John</FirstName>
                <LastName xmlns="">Jackson</LastName>
       </Object>
    </Signature>
    </LicenseFile>
    <LicenseFile xmlns="">
     <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
...
    </Signature>
    <Object Id="Settings">
       <ProductID xmlns="">P2</ProductID>
                <FirstName xmlns="">John</FirstName>
                <LastName xmlns="">Jackson</LastName>
    </Object>
    </LicenseFile>
什么是不正确的?
Thnx.

对象节点必须是签名的子节点,且不在同一级别

XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = false;
doc.Load(new XmlTextReader(FileName));
SignedXml signedXml = new SignedXml(doc);
signedXml.SigningKey = Key;
Signature XMLSignature = signedXml.Signature;
Reference reference = new Reference("#Settings");
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
XMLSignature.SignedInfo.AddReference(reference);
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new RSAKeyValue((RSA)Key));
XMLSignature.KeyInfo = keyInfo;

// Compute the signature.
signedXml.ComputeSignature();

XmlElement xmlDigitalSignature = signedXml.GetXml();

// Append the element to the XML document.
doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
if (doc.FirstChild is XmlDeclaration)
{
    doc.RemoveChild(doc.FirstChild);
}

// Save the signed XML document to a file specified
// using the passed string.
XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false));
doc.WriteTo(xmltw);
xmltw.Close();