Beispiel #1
0
    // Encrypt the text in the specified XmlDocument.
    private static void ShowTransformProperties(XmlDocument xmlDoc)
    {
        //<Snippet1>
        XmlDsigC14NTransform xmlTransform =
            new XmlDsigC14NTransform(true);

        //</Snippet1>

        // Ensure the transform is using the appropriate algorithm.
        //<Snippet4>
        xmlTransform.Algorithm =
            SignedXml.XmlDsigExcC14NTransformUrl;
        //</Snippet4>

        // Retrieve the XML representation of the current transform.
        //<Snippet10>
        XmlElement xmlInTransform = xmlTransform.GetXml();

        //</Snippet10>

        Console.WriteLine("\nXml representation of the current transform: ");
        Console.WriteLine(xmlInTransform.OuterXml);

        // Retrieve the valid input types for the current transform.
        //<Snippet5>
        Type[] validInTypes = xmlTransform.InputTypes;
        //</Snippet5>

        // Verify the xmlTransform can accept the XMLDocument as an
        // input type.
        for (int i = 0; i < validInTypes.Length; i++)
        {
            if (validInTypes[i] == xmlDoc.GetType())
            {
                // Load the document into the transfrom.
                //<Snippet12>
                xmlTransform.LoadInput(xmlDoc);
                //</Snippet12>

                //<Snippet3>
                XmlDsigC14NTransform secondTransform =
                    new XmlDsigC14NTransform();
                //</Snippet3>

                //<Snippet13>
                string classDescription = secondTransform.ToString();
                //</Snippet13>

                // This call does not perform as expected.
                // This transform does not contain inner XML elements
                //<Snippet11>
                secondTransform.LoadInnerXml(xmlDoc.SelectNodes("//."));
                //</Snippet11>

                break;
            }
        }

        //<Snippet6>
        Type[] validOutTypes = xmlTransform.OutputTypes;
        //</Snippet6>

        for (int i = 0; i < validOutTypes.Length; i++)
        {
            if (validOutTypes[i] == typeof(System.IO.Stream))
            {
                try
                {
                    //<Snippet9>
                    Type         streamType   = typeof(System.IO.Stream);
                    MemoryStream outputStream = (MemoryStream)
                                                xmlTransform.GetOutput(streamType);
                    //</Snippet9>

                    // Read the CryptoStream into a stream reader.
                    StreamReader streamReader =
                        new StreamReader(outputStream);

                    // Read the stream into a string.
                    string outputMessage = streamReader.ReadToEnd();

                    // Close the streams.
                    outputStream.Close();
                    streamReader.Close();

                    // Display to the console the Xml before and after
                    // encryption.
                    Console.WriteLine("Encoding the following xml: " +
                                      xmlDoc.OuterXml);
                    Console.WriteLine("Message encoded: " + outputMessage);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unexpected exception caught: " +
                                      ex.ToString());
                }

                break;
            }
            else
            {
                //<Snippet8>
                object outputObject = xmlTransform.GetOutput();
                //</Snippet8>
            }
        }
    }