コード例 #1
0
 private static XmlDocument GetXmlMessage(Message request)
 {
     using (XmlDocumentWriterHelper documentWriterHelper = new XmlDocumentWriterHelper())
     {
         request.WriteMessage(documentWriterHelper.CreateDocumentWriter());
         return(documentWriterHelper.ReadDocument());
     }
 }
コード例 #2
0
        private XmlElement SerializeToken(SecurityToken token)
        {
            using (XmlDocumentWriterHelper documentWriterHelper = new XmlDocumentWriterHelper())
            {
                _tokenSerializer.WriteToken(documentWriterHelper.CreateDocumentWriter(), token);
                XmlDocument xmlDocument = documentWriterHelper.ReadDocument();

                return(xmlDocument.DocumentElement);
            }
        }
コード例 #3
0
        /// <summary>
        /// Returns a SecurityTokenReference element which contains a reference
        /// by id to a client certificate that is located in the same XML document.
        /// </summary>
        private XmlElement GetKeyIdentifierClause(X509SecurityToken certToken)
        {
            var keyIdentifierClause =
                certToken.CreateKeyIdentifierClause <LocalIdKeyIdentifierClause>();

            Debug.Assert(keyIdentifierClause != null);

            using (XmlDocumentWriterHelper documentWriterHelper = new XmlDocumentWriterHelper())
            {
                _tokenSerializer.WriteKeyIdentifierClause(
                    documentWriterHelper.CreateDocumentWriter(), keyIdentifierClause);

                XmlDocument xmlDocument = documentWriterHelper.ReadDocument();

                return(xmlDocument.DocumentElement);
            }
        }
コード例 #4
0
      /// <summary>
      /// Converts the specified <paramref name="value"/> to <see cref="XmlElement"/>.
      /// </summary>
      ///
      /// <param name="value">
      /// Value to serialize. If the declaring type specifies an <see cref="XmlTypeAttribute.Namespace"/>
      /// attribute value, it is set as default namespace to the returned root XML element.
      /// </param>
      /// <param name="includeStandardNamespaceDeclarations">
      /// Specifies wether the returned root XML element will explicitly declare standard XML namespaces,
      /// such as the XML schema and instance namespaces.
      /// </param>
      public static XmlElement ToXmlElement(object value, bool includeStandardNamespaceDeclarations = true)
      {

         Type typeToSerialize = value.GetType();

         // Retrive the default XML namespace from the XmlType attribute: [XmlType(Namespace = ...)]
         string defaultNamespace =
            (typeToSerialize.GetCustomAttributes(typeof(XmlTypeAttribute), true)
               .FirstOrDefault() as XmlTypeAttribute)
               ?.Namespace;

         XmlSerializer serializer = (defaultNamespace != null)
            ? new XmlSerializer(typeToSerialize, defaultNamespace)
            : new XmlSerializer(typeToSerialize);

         using (XmlDocumentWriterHelper documentWriterHelper = new XmlDocumentWriterHelper())
         {
            XmlWriter xmlWriter = documentWriterHelper.CreateDocumentWriter();

            if (includeStandardNamespaceDeclarations)
            {
               serializer.Serialize(xmlWriter, value);

            }
            else
            {
               // Suppress adding the default XML instance and schema namespaces
               // by specifying the namespace table explicitly.
               var ns = new XmlSerializerNamespaces();

               // Append the default namespace to the namespace table
               if (defaultNamespace != null)
               {
                  ns.Add("", defaultNamespace);
               }

               serializer.Serialize(xmlWriter, value, ns);
            }

            return documentWriterHelper.ReadDocument().DocumentElement;
         }
      }