public class XmlSerializerOutputFormatterNamespace : XmlSerializerOutputFormatter
{
protected override void Serialize(XmlSerializer xmlSerializer, XmlWriter xmlWriter, object value)
{
//applying "empty" namespace will produce no namespaces
var emptyNamespaces = new XmlSerializerNamespaces();
emptyNamespaces.Add("", "any-non-empty-string");
xmlSerializer.Serialize(xmlWriter, value, emptyNamespaces);
}
}
And here is the code of controller:
[HttpPost]
[Produces("application/xml")]
public async Task<BaseMsg> mp([FromBody]TextMsg XmlData)
{
TextMsg ReturnXmlData = new TextMsg() { ToUserName = XmlData.FromUserName, FromUserName = XmlData.ToUserName ,CreateTime= XmlData.CreateTime, MsgType= "text", Content ="你好呀"};
return ReturnXmlData;
}
[XmlInclude(typeof(BaseMsg))]
[XmlRoot("xml")]
public class BaseMsg {
public string ToUserName { get; set; }
public string FromUserName { get; set; }
public string CreateTime { get; set; }
public string MsgType { get; set; }
public string MsgId { get; set; }
}
[XmlInclude(typeof(TextMsg))]
[XmlRoot("xml")]
public class TextMsg:BaseMsg {
public string Content { get; set; }
}
After I ran the program and send a post, Visual Studio reports an error in the override Serialize:
System.InvalidOperationException: 'There was an error generating the XML document.'
InvalidOperationException: The type Demo.Controllers.CallbackController+TextMsg was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
It seems reported this error for I did not use the XmlInclude. However, as you see I added it yet and why I did not work yet?
I tried some ways and found if I don't inherit the BaseMsg class, it won't report this anymore.
Why it turns out to be this? Would you please help me? Thank you.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
53 Points
246 Posts
Why the XmlInclude does not work in my project?
Sep 22, 2019 08:10 AM|mywatermelon|LINK
I override the Serialize for removing the prefix of XML:
Here is the startup.cs:
And here is the code of override Serialize:
And here is the code of controller:
After I ran the program and send a post, Visual Studio reports an error in the override Serialize:
System.InvalidOperationException: 'There was an error generating the XML document.'
InvalidOperationException: The type Demo.Controllers.CallbackController+TextMsg was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
It seems reported this error for I did not use the XmlInclude. However, as you see I added it yet and why I did not work yet?
I tried some ways and found if I don't inherit the BaseMsg class, it won't report this anymore.
Why it turns out to be this? Would you please help me? Thank you.
Contributor
2690 Points
874 Posts
Re: Why the XmlInclude does not work in my project?
Sep 23, 2019 02:25 AM|Rena Ni|LINK
Hi mywatermelon,
You need to remove the following attribute in the TextMsg class:
And then modify the attribute in BaseMsg class like below:
[XmlInclude(typeof(TextMsg))] [XmlRoot("xml")] public class BaseMsg { public string ToUserName { get; set; } public string FromUserName { get; set; } public string CreateTime { get; set; } public string MsgType { get; set; } public string MsgId { get; set; } }
Reference: http://www1.cs.columbia.edu/~lok/csharp/refdocs/System.Xml.Serialization/types/XmlIncludeAttribute.html
Best Regards,
Rena
Member
53 Points
246 Posts
Re: Why the XmlInclude does not work in my project?
Sep 23, 2019 02:26 AM|mywatermelon|LINK
Thanks a lot.