RockAndRoll
RockAndRoll

Reputation: 2287

Web method with XmlDocument as return type fails in SoapCore in asp.net core

I am trying to host asmx type web service in asp.net core using SoapCore.

I have one web method as follows.

[OperationContract]
XmlDocument testProperties();

and concrete implementation for this as follows.

public XmlDocument testProperties()
{
    XmlDocument testProperties = new XmlDocument(); 
    testProperties.LoadXml("<test>abc</test>");
    return testProperties;
}

When I am trying to access this web method, it fails with following error.

System.InvalidOperationException: There was an error generating the XML document.
 ---> System.InvalidOperationException: This element was named 'test' from namespace '' but should have been named 'TestPropertiesResult' from namespace ''.

Note: This issue only comes when method has return type as XmlDocument. If it is string or other simple data type, everything works perfectly.

Any lead would be helpful.

Edit 1

I have tried renaming test with TestPropertiesResult but that also does not solve anything. Only error message changes to

System.InvalidOperationException: There was an error generating the XML document.
 ---> System.InvalidOperationException: This element was named 'TestPropertiesResult' from namespace '' but should have been named 'TestPropertiesResult' from namespace ''.

Edit 2

After further analysis in SoapCore, I could narrow down the root cause. This issue is coming when serialization happens of XmlDocument by XmlSerializer created as

XmlSerializer ser = new XmlSerializer(typeof(XmlDocument), null, new Type[0], new XmlRootAttribute("dummynode"), "testnamespace");

I tried changing it to following

XmlSerializer ser = new XmlSerializer(typeof(XmlDocument));

then it works but root element is missing. I can't change it. I can only change XmlDocument.

Upvotes: 2

Views: 1003

Answers (1)

SRIDHARAN
SRIDHARAN

Reputation: 1222

Please refer this documentation.

What can be serialized using XMLSerializer ?

As per the documentation wrap the XMLDocument class as public member.

 public class Wrapper
 {
    public XmlDocument XmlDocument   { get; set; }
 }
 public static Wrapper testProperties()
 {
        XmlDocument testProperties = new XmlDocument();
        testProperties.LoadXml("<test>abc</test>");
        return new Wrapper { XmlDocument = testProperties };
 }

I hope this solves your problem.

Upvotes: 2

Related Questions