WebMethod returning an XmlDocument generating a compile error in the client.

  • Thread starter Thread starter Thirsty Traveler
  • Start date Start date
T

Thirsty Traveler

I have a WebMethod as follows:

[WebMethod]
public XmlDocument OrderContract(XmlDocument doc)
{
return OrderBLL.OrderContract(doc);
}

However, the client is generating a compile error when the following lines
are used:

OrderWS.ContractServices txn = new OrderWS.ContractServices ();
XmlDocument retxml = txn.OrderContractCert(xml);

However, this compiles clean:

OrderWS.ContractServices txn = new OrderWS.ContractServices ();
XmlNode retxml = txn.OrderContractCert(xml);

I have no idea why the client is expecting an XmlNode.
 
Thirsty Traveler wrote:

However, the client is generating a compile error when the following lines
are used:

OrderWS.ContractServices txn = new OrderWS.ContractServices ();
XmlDocument retxml = txn.OrderContractCert(xml);

However, this compiles clean:

OrderWS.ContractServices txn = new OrderWS.ContractServices ();
XmlNode retxml = txn.OrderContractCert(xml);

I have no idea why the client is expecting an XmlNode.

Does it work if you cast the result? E.g.
XmlDocument retxml = txn.OrderContractCert(xml) as XmlDocument;
if (retxml != null) {
// now use retxml here
}
 
IIRC the cast won't work; it returns a node, not a document.

If you need a document reference, then create a new XmlDocument and
call ImportNode on it to clone that node into the new document.
Alternatively, see what OwnerDocument (on the XmlNode) says.

Marc
 
I am still unclear on why it is returning a node rather than a document when
the webmethod specifies XmlDocument as the return type.
 
Marc said:
IIRC the cast won't work; it returns a node, not a document.

XmlNode is an astract class, somehow an instance of a derived class
needs to be returned, and XmlDocument is a derived class. But you are
right that the cast to XmlDocument does not work, a test here shows that
NodeType gives Element node so you could cast to XmlElement but not
XmlDocument.
Alternatively, see what OwnerDocument (on the XmlNode) says.

The OwnerDocument of the node returned is an XmlDocument instance but in
my test here (with .NET 1.1) does not even contain the node returned.
 
Thirsty said:
I am still unclear on why it is returning a node rather than a document when
the webmethod specifies XmlDocument as the return type.

Web services communicate using SOAP and it seems any XmlNode sub type is
simply serialized in the SOAP message so it is kind of hard to decide on
the client side what concrete type the XmlNode had.
 
Try this:
OrderWS.ContractServices txn = new OrderWS.ContractServices ();
XmlDocument retxml = (XmlDocument) txn.OrderContractCert(xml);

Does it work for you?

chanmm
 
Unfortunately, that is one of the first things I tried and it did not work.

chanmm said:
Try this:
OrderWS.ContractServices txn = new OrderWS.ContractServices ();
XmlDocument retxml = (XmlDocument) txn.OrderContractCert(xml);

Does it work for you?

chanmm

Thirsty Traveler said:
I have a WebMethod as follows:

[WebMethod]
public XmlDocument OrderContract(XmlDocument doc)
{
return OrderBLL.OrderContract(doc);
}

However, the client is generating a compile error when the following
lines are used:

OrderWS.ContractServices txn = new OrderWS.ContractServices ();
XmlDocument retxml = txn.OrderContractCert(xml);

However, this compiles clean:

OrderWS.ContractServices txn = new OrderWS.ContractServices ();
XmlNode retxml = txn.OrderContractCert(xml);

I have no idea why the client is expecting an XmlNode.
 
Thirsty said:
Unfortunately, that is one of the first things I tried and it did not work.

It seems (at least with .NET 1.1 from tests) that you get an XmlElement
instead of an XmlDocument so you could use e.g.
XmlNode retxml = txn.OrderContractCert(xml);
XmlDocument doc = retxml.OwnerDocument;
doc.AppendChild(retxml);
and then you can use doc as an XmlDocument.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top