The sample code in Listing 22.37 shows how to sign XML data and produce an envelope for it via the RSA algorithm.
Listing 22.37: SignXML1.cs, Compute Signature for XML Data
using System;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
public class DigitalSignSample
{
public static void Main()
{
// generate XML data
XmlDocument document = new XmlDocument();
XmlNode node = document.CreateNode(XmlNodeType.Element, "", @"Visual Studio .NET", "sign xml samples");
node.InnerText = @"C# wimps the lama's bass...";
document.AppendChild(node);
Console.WriteLine("OriginalXML data:\r\n" + document.OuterXml + "\r\n");
// create signedxml variable
RSA rsa = System.Security.Cryptography.RSA.Create();
SignedXml signedXml = new SignedXml();
signedXml.SigningKey = rsa;
// create dataobject
DataObject dataObject = new System.Security.Cryptography.Xml.DataObject();
dataObject.Data = document.ChildNodes;
dataObject.Id = "goo";
// add dataobject and reference
signedXml.AddObject(dataObject);
signedXml.AddReference(new Reference("#goo"));
Read more: C# Corner