Help convert C# code relating to signing xml


To summarize:

In VC 2.3 for VS, we need to digitally sign portions of an XML document in SOAP envelope.  We are using classes in System.Security.Cryptography.Xml, in particular the class SignedXml

We are having trouble signing xml when we add a Reference based on an Id attribute.  We are getting "Malformed Reference Element" exception when ComputeSignature is invoked.

After much searhing on the internet I made have found the answer in stackoverflow, but I need to convert C# code below:


public class SignedXmlWithId : SignedXml
public SignedXmlWithId(XmlDocument xml) : base(xml)

public SignedXmlWithId(XmlElement xmlElement)
: base(xmlElement)

public override XmlElement GetIdElement(XmlDocument doc, string id)
// check to see if it's a standard ID reference
XmlElement idElem = base.GetIdElement(doc, id);

if (idElem == null)
XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("wsu", "");

idElem = doc.SelectSingleNode("//*[@wsu:Id=\"" id "\"]", nsManager) as XmlElement;

return idElem;


Converting the above code may be the answer, otherwise I may need more help.

Many thanks


  • Verified Answer

    Something like this:


          $set ilusing"System.Security.Cryptography.Xml"
          $set ilusing"System.Xml"
           class-id testsignxml.SignedXmlWithId inherits type SignedXml.
           working-storage section.
           method-id New public.
           procedure division using myxml as type XmlDocument.
              invoke super::New(myxml)
           end method.
           method-id New public.
           procedure division using xmlElement as type XmlElement.
              invoke super::New(xmlElement)
           end method.
           method-id GetIdElement override.
           procedure division using by value doc as type XmlDocument #id as string
               returning return-item as type XmlElement.
               declare idElem as type XmlElement = super::GetIdElement(doc, #id)
               if idElem = null
                  declare nsManager as type XmlNamespaceManager = new XmlNamespaceManager(doc::NameTable)
                  invoke nsManager::AddNamespace("wsu", "")
                  set idElem = doc::SelectSingleNode("//*[@wsu:Id=\" & #id & "\]", nsManager) as type XmlElement
               set return-item to idElem
           end method.
           end class.
  • Hi Chris

    Many thanks, using that code may well have done the trick regarding the signature.

    Perhaps I should create a new post, but now we have the problem of sending the SOAP message, which we use System.Xml, System.Security.Cryptography..., etc to create.

    What I have tried so far is the below:

    method-id SEND-SOAP.

    01 Client type HttpClient.

    01 Request type HttpRequestMessage.

    01 Response type HttpResponseMessage.

    *01 Uri String.
    01 Uri type Uri.

    procedure division.

    set Uri to new Uri("https://???.ie/paye-employers/v1/soap").  *>> actual URI replaced here

    set Client to new HttpClient().

    set Request to new HttpRequestMessage(type HttpMethod::Post, Uri).

    set Request::Content to new StringContent(theMsg, type Encoding::UTF8, "application/soap xml").  *>> theMsg is the SOAP envelope

    * set Response to Client::SendAsync(Request) as type HttpResponseMessage.
    set Response to Client::SendAsync(Request)::Result.

    * set Response to Client::PostAsync(Uri,Request::Content)::Result.

    exit method.

    end method.


    This is returning Http status code 500 "Internal Server Error", which is not very helpful. I may try things like sending the data as a stream.

    Using System.Net.Http is probably not the best way to go about it.  I have looked at some WCF examples but I do not understand them.

    We have been provided with the .wsdl's, endpoints, and sample code written in Java.

    Any help would be appreciated.


    Many thanks


  • Hi Brendan,

    Please create a new thread for the new problem so that we can close out the other one. It really makes it more difficult to search for particular problems and solutions in the future if their are multiple problems are questions asked within a single thread.

  • Hi Chris

    Ok thanks, thought I should probably do that. [:)]  New post "How to send digitally-signed SOAP message" created.