Edit

Share via


SignedXml.XmlDsigXPathTransformUrl Field

Definition

Represents the Uniform Resource Identifier (URI) for the XML Path Language (XPath). This field is constant.

public: System::String ^ XmlDsigXPathTransformUrl;
public const string XmlDsigXPathTransformUrl;
val mutable XmlDsigXPathTransformUrl : string
Public Const XmlDsigXPathTransformUrl As String 

Field Value

Examples

The following code example demonstrates how to call members of the XmlDsigXPathTransform class.

using System;
using System.IO;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;

class Class1
{
    [STAThread]
    static void Main(string[] args)
    {
        // Encrypt a sample XML string.
        XmlDocument productsXml = LoadProducts();
        ShowTransformProperties(productsXml);

        // Encrypt an XPath Xml string.
        XmlDocument transformXml = LoadTransformByXml();
        ShowTransformProperties(transformXml);

        // Use XmlDsigXPathTransform to resolve a Uri.
        Uri baseUri = new Uri("http://www.contoso.com");
        string relativeUri = "xml";
        Uri absoluteUri = ResolveUris(baseUri, relativeUri);

        Console.WriteLine("This sample completed successfully; " +
            "press Enter to exit.");
        Console.ReadLine();
    }

    // Encrypt the text in the specified XmlDocument.
    private static void ShowTransformProperties(XmlDocument xmlDoc)
    {

        // Create a new XMLDocument object.
        XmlDocument doc = new XmlDocument();

        // Create a new XmlElement.
        XmlElement xPathElem = doc.CreateElement("XPath");

        // Set the element text to the value
        // of the XPath string.
        xPathElem.InnerText = "ancestor-or-self::PRODUCTS";

        // Create a new XmlDsigXPathTransform object.
        XmlDsigXPathTransform xmlTransform = new XmlDsigXPathTransform();

        // Load the XPath XML from the element. 
        xmlTransform.LoadInnerXml(xPathElem.SelectNodes("."));

        // Ensure the transform is using the proper algorithm.
        xmlTransform.Algorithm =
            SignedXml.XmlDsigXPathTransformUrl;

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

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

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

        // 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.
                xmlTransform.LoadInput(xmlDoc);

                try
                {
                    // This transform is created for demonstration purposes.
                    XmlDsigXPathTransform secondTransform =
                        new XmlDsigXPathTransform();

                    string classDescription = secondTransform.ToString();

                    xmlTransform.LoadInnerXml(xPathElem.SelectNodes(".")); ;
                }
                catch (CryptographicException)
                {
                    Console.WriteLine("Caught exception while trying to " +
                        "load the specified Xml document. The document " +
                        "requires an XPath element to be valid.");
                }
                break;
            }
        }

        Type[] validOutTypes = xmlTransform.OutputTypes;

        for (int i = validOutTypes.Length - 1; i >= 0; i--)
        {
            if (validOutTypes[i] == typeof(System.Xml.XmlDocument))
            {
                try
                {
                    Type xmlDocumentType = typeof(System.Xml.XmlDocument);
                    XmlDocument xmlDocumentOutput = (XmlDocument)
                        xmlTransform.GetOutput(xmlDocumentType);

                    // Display to the console the Xml before and after
                    // encryption.
                    Console.WriteLine("Result of the GetOutput method call" +
                        " from the current transform: " +
                        xmlDocumentOutput.OuterXml);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unexpected exception caught: " +
                        ex.ToString());
                }

                break;
            }
            else if (validOutTypes[i] == typeof(System.Xml.XmlNodeList))
            {
                try
                {
                    Type xmlNodeListType = typeof(System.Xml.XmlNodeList);
                    XmlNodeList xmlNodes = (XmlNodeList)
                        xmlTransform.GetOutput(xmlNodeListType);

                    // Display to the console the Xml before and after
                    // encryption.
                    Console.WriteLine("Encoding the following message: " +
                        xmlDoc.InnerText);

                    Console.WriteLine("Nodes of the XmlNodeList retrieved " +
                        "from GetOutput:");
                    for (int j = 0; j < xmlNodes.Count; j++)
                    {
                        Console.WriteLine("Node " + j +
                            " has the following name: " +
                            xmlNodes.Item(j).Name +
                            " and the following InnerXml: " +
                            xmlNodes.Item(j).InnerXml);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unexpected exception caught: " +
                        ex.ToString());
                }

                break;
            }
            else
            {
                object outputObject = xmlTransform.GetOutput();
            }
        }
    }

    // Create an XML document for the dsig namespace.
    private static XmlDocument LoadTransformByXml()
    {
        XmlDocument xmlDoc = new XmlDocument();

        string transformXml = "<Signature><Reference URI=''><Transforms>";
        transformXml += "<Transform><XPath ";
        transformXml += "xmlns:dsig='http://www.w3.org/2000/09/xmldsig#'>";
        transformXml += "not(ancestor-or-self::dsig:Signature)";
        transformXml += "</XPath></Transform>";
        transformXml += "</Transforms></Reference></Signature>";

        xmlDoc.LoadXml(transformXml);
        return xmlDoc;
    }

    // Create an XML document describing various products.
    private static XmlDocument LoadProducts()
    {
        XmlDocument xmlDoc = new XmlDocument();

        string contosoProducts = "<PRODUCTS>";
        contosoProducts += "<PRODUCT><ID>123</ID>";
        contosoProducts += "<DESCRIPTION>Router</DESCRIPTION></PRODUCT>";
        contosoProducts += "<PRODUCT><ID>456</ID>";
        contosoProducts += "<DESCRIPTION>Keyboard</DESCRIPTION></PRODUCT>";
        contosoProducts += "<PRODUCT><ID>789</ID>";
        contosoProducts += "<DESCRIPTION>Monitor</DESCRIPTION></PRODUCT>";
        contosoProducts += "</PRODUCTS>";

        xmlDoc.LoadXml(contosoProducts);
        return xmlDoc;
    }

    // Resolve the specified base and relative Uri's .
    private static Uri ResolveUris(Uri baseUri, string relativeUri)
    {
        XmlUrlResolver xmlResolver = new XmlUrlResolver();
        xmlResolver.Credentials =
            System.Net.CredentialCache.DefaultCredentials;

        XmlDsigXPathTransform xmlTransform =
            new XmlDsigXPathTransform();
        xmlTransform.Resolver = xmlResolver;

        Uri absoluteUri = xmlResolver.ResolveUri(baseUri, relativeUri);

        if (absoluteUri != null)
        {
            Console.WriteLine(
                "\nResolved the base Uri and relative Uri to the following:");
            Console.WriteLine(absoluteUri.ToString());
        }
        else
        {
            Console.WriteLine(
                "Unable to resolve the base Uri and relative Uri");
        }
        return absoluteUri;
    }
}
Imports System.IO
Imports System.Xml
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml



Class Class1

    <STAThread()> _
    Shared Sub Main(ByVal args() As String)
        ' Encrypt a sample XML string.
        Dim productsXml As XmlDocument = LoadProducts()
        ShowTransformProperties(productsXml)

        ' Encrypt an XPath Xml string.
        Dim transformXml As XmlDocument = LoadTransformByXml()
        ShowTransformProperties(transformXml)

        ' Use XmlDsigXPathTransform to resolve a Uri.
        Dim baseUri As New Uri("http://www.contoso.com")
        Dim relativeUri As String = "xml"
        Dim absoluteUri As Uri = ResolveUris(baseUri, relativeUri)

        Console.WriteLine("This sample completed successfully; " + "press Enter to exit.")
        Console.ReadLine()

    End Sub


    ' Encrypt the text in the specified XmlDocument.
    Private Shared Sub ShowTransformProperties(ByVal xmlDoc As XmlDocument)
        ' Create a new XMLDocument object.
        Dim doc As New XmlDocument()

        ' Create a new XmlElement.
        Dim xPathElem As XmlElement = doc.CreateElement("XPath")

        ' Set the element text to the value
        ' of the XPath string.
        xPathElem.InnerText = "ancestor-or-self::PRODUCTS"

        ' Create a new XmlDsigXPathTransform object.
        Dim xmlTransform As New XmlDsigXPathTransform()

        ' Load the XPath XML from the element. 
        xmlTransform.LoadInnerXml(xPathElem.SelectNodes("."))
        ' Ensure the transform is using the proper algorithm.
        xmlTransform.Algorithm = SignedXml.XmlDsigXPathTransformUrl
        ' Retrieve the XML representation of the current transform.
        Dim xmlInTransform As XmlElement = xmlTransform.GetXml()
        Console.WriteLine(vbLf + "Xml representation of the current transform: ")
        Console.WriteLine(xmlInTransform.OuterXml)

        ' Retrieve the valid input types for the current transform.
        Dim validInTypes As Type() = xmlTransform.InputTypes
        ' Verify the xmlTransform can accept the XMLDocument as an
        ' input type.
        Dim i As Integer
        For i = 0 To validInTypes.Length
            If validInTypes(i).Equals(xmlDoc.GetType()) Then
                ' Load the document into the transfrom.
                xmlTransform.LoadInput(xmlDoc)
                Try
                    ' This transform is created for demonstration purposes.
                    Dim secondTransform As New XmlDsigXPathTransform()

                    Dim classDescription As String = secondTransform.ToString()
                    xmlTransform.LoadInnerXml(xPathElem.SelectNodes("."))
                Catch
                    Console.WriteLine("Caught exception while trying to " + "load the specified Xml document. The document " + "requires an XPath element to be valid.")
                End Try
                Exit For
            End If
        Next i

        Dim validOutTypes As Type() = xmlTransform.OutputTypes
        For i = validOutTypes.Length - 1 To 0 Step -1
            If validOutTypes(i).Equals(GetType(System.Xml.XmlDocument)) Then
                Try
                    Dim xmlDocumentType As Type = GetType(System.Xml.XmlDocument)
                    Dim xmlDocumentOutput As XmlDocument = CType(xmlTransform.GetOutput(xmlDocumentType), XmlDocument)
                    ' Display to the console the Xml before and after
                    ' encryption.
                    Console.WriteLine("Result of the GetOutput method call" + " from the current transform: " + xmlDocumentOutput.OuterXml)
                Catch ex As Exception
                    Console.WriteLine("Unexpected exception caught: " + ex.ToString())
                End Try

                Exit For
            ElseIf validOutTypes(i).Equals(GetType(System.Xml.XmlNodeList)) Then
                Try
                    Dim xmlNodeListType As Type = GetType(System.Xml.XmlNodeList)
                    Dim xmlNodes As XmlNodeList = CType(xmlTransform.GetOutput(xmlNodeListType), XmlNodeList)

                    ' Display to the console the Xml before and after
                    ' encryption.
                    Console.WriteLine("Encoding the following message: " + xmlDoc.InnerText)

                    Console.WriteLine("Nodes of the XmlNodeList retrieved " + "from GetOutput:")
                    For j As Integer = 0 To xmlNodes.Count - 1
                        Console.WriteLine("Node " + j + " has the following name: " + xmlNodes.Item(j).Name + " and the following InnerXml: " + xmlNodes.Item(j).InnerXml)
                    Next
                Catch ex As Exception
                    Console.WriteLine("Unexpected exception caught: " + ex.ToString())
                End Try

                Exit For
            Else
                Dim outputObject As Object = xmlTransform.GetOutput()
            End If 
        Next i

    End Sub


    ' Create an XML document for the dsig namespace.
    Private Shared Function LoadTransformByXml() As XmlDocument
        Dim xmlDoc As New XmlDocument()

        Dim transformXml As String = "<Signature><Reference URI=''><Transforms>"
        transformXml += "<Transform><XPath "
        transformXml += "xmlns:dsig='http://www.w3.org/2000/09/xmldsig#'>"
        transformXml += "not(ancestor-or-self::dsig:Signature)"
        transformXml += "</XPath></Transform>"
        transformXml += "</Transforms></Reference></Signature>"

        xmlDoc.LoadXml(transformXml)
        Return xmlDoc

    End Function 'LoadTransformByXml


    ' Create an XML document describing various products.
    Private Shared Function LoadProducts() As XmlDocument
        Dim xmlDoc As New XmlDocument()

        Dim contosoProducts As String = "<PRODUCTS>"
        contosoProducts += "<PRODUCT><ID>123</ID>"
        contosoProducts += "<DESCRIPTION>Router</DESCRIPTION></PRODUCT>"
        contosoProducts += "<PRODUCT><ID>456</ID>"
        contosoProducts += "<DESCRIPTION>Keyboard</DESCRIPTION></PRODUCT>"
        contosoProducts += "<PRODUCT><ID>789</ID>"
        contosoProducts += "<DESCRIPTION>Monitor</DESCRIPTION></PRODUCT>"
        contosoProducts += "</PRODUCTS>"

        xmlDoc.LoadXml(contosoProducts)
        Return xmlDoc

    End Function 'LoadProducts


    ' Resolve the specified base and relative Uri's .
    Private Shared Function ResolveUris(ByVal baseUri As Uri, ByVal relativeUri As String) As Uri
        Dim xmlResolver As New XmlUrlResolver()
        xmlResolver.Credentials = System.Net.CredentialCache.DefaultCredentials

        Dim xmlTransform As New XmlDsigXPathTransform()
        xmlTransform.Resolver = xmlResolver
        Dim absoluteUri As Uri = xmlResolver.ResolveUri(baseUri, relativeUri)

        If Not (absoluteUri Is Nothing) Then
            Console.WriteLine(vbLf + "Resolved the base Uri and relative Uri to the following:")
            Console.WriteLine(absoluteUri.ToString())
        Else
            Console.WriteLine("Unable to resolve the base Uri and relative Uri")
        End If
        Return absoluteUri

    End Function
End Class

Remarks

The value of the XmlDsigXPathTransformUrl field is http://www.w3.org/TR/1999/REC-xpath-19991116.

Use this field to conveniently supply a value to one of the URI attributes of an element used for XMLDSIG.

The XmlDsigXPathTransform class implements the transform described by the XmlDsigXPathTransformUrl field.

For more information, see the World Wide Web Consortium (W3C) specification.

Applies to