我正在尝试使用SoapCore在asp.net核心中托管asmx类型的Web服务。
我有以下一种网络方法。
[OperationContract]
XmlDocument testProperties();
及其具体实现如下。
public XmlDocument testProperties()
{
XmlDocument testProperties = new XmlDocument();
testProperties.LoadXml("<test>abc</test>");
return testProperties;
}
[当我尝试访问此Web方法时,它失败并显示以下错误。
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 ''.
注意:仅当方法的返回类型为XmlDocument时,才会出现此问题。如果它是字符串或其他简单数据类型,则一切正常。
任何线索都会有所帮助。
编辑1
我已经尝试使用TestPropertiesResult重命名测试,但这也无法解决任何问题。仅错误消息更改为
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 ''.
编辑2
[在SoapCore中进行了进一步分析后,我可以缩小根本原因。当通过创建为
的XmlSerializer对XmlDocument进行序列化时,将出现此问题XmlSerializer ser = new XmlSerializer(typeof(XmlDocument), null, new Type[0], new XmlRootAttribute("dummynode"), "testnamespace");
我尝试将其更改为以下内容
XmlSerializer ser = new XmlSerializer(typeof(XmlDocument));
然后它可以工作,但是根元素丢失。我不能改变我只能改变XmlDocument
。
请参阅此文档。
What can be serialized using XMLSerializer ?
根据文档,将XMLDocument类包装为公共成员。
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 };
}
我希望这可以解决您的问题。