tag:blogger.com,1999:blog-2441850399540300710.post2961802940607519445..comments2024-02-17T03:47:06.818-08:00Comments on CraigBlog: XmlInclude - Not Quite as Useless as I ThoughtCraig Anderahttp://www.blogger.com/profile/17084199593129216563noreply@blogger.comBlogger29125tag:blogger.com,1999:blog-2441850399540300710.post-9895111623573551082010-05-28T11:21:10.000-07:002010-05-28T11:21:10.000-07:00Have you guys tried with JSon serialization? - Thi...Have you guys tried with JSon serialization? - This might not be an issue if you use this serialization mechanism instead of XML. As for myself, I have always dealt with this issue in XML serialization the same way (anticipating all the derivate types in the base class attributes is certainly not very elegant). but I've never verified if JSON has the same issue. That's just an idea... <br /><br />Regards.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-12842162560122236002009-08-09T19:08:12.000-07:002009-08-09T19:08:12.000-07:00Hey Craig, funny I was just googling this XMLSeria...Hey Craig, funny I was just googling this XMLSerializer error and your page turned up in the first page of results. Small world. <br /><br />I'm running across this issue just dumping an object Graph to XML. Unfortunately, neither annotating the base class nor a method will help me. Martin's solution is the only one available to me. Unfortunately something in the obnoxious business model library I'm forced to use is overriding the behavior. As it's just a piece of my test harness, it's not worth too large a time investment. <br /><br />In the troubleshooting process however, I discovered the reference below that describes how to turn on preservation of temporary serialization assemblies, and debug through them. Might help somebody else that ends up here. <br /><br />Cheers, <br />Rob <br /><br />http://msdn.microsoft.com/en-us/library/aa302290.aspx<br /><br /><a href="http://msdn.microsoft.com/en-us/library/aa302290.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa302290.aspx</a>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-12230310008496823772009-02-27T06:15:28.000-08:002009-02-27T06:15:28.000-08:00Sorry, I was wrong. This doesn't work... It is...Sorry, I was wrong. This doesn't work... It is usefull for another types of attributes. however you can pass all the derived classes (that you probably don't know when you compile the base class) on the constructor of the class XmlSerializer.<br /><br />For example:<br />XmlSerializer serializador = new XmlSerializer(typeof(OneClass), null, new Type[] { typeof(DerivedClass1), typeof(DerivedClass2) }, null, null);Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-80542349469805580062009-02-27T04:54:26.000-08:002009-02-27T04:54:26.000-08:00I think there is an other way to do this:
1) you c...I think there is an other way to do this:<br />1) you create a new XmlIncludeAttribute:<br />XmlIncludeAttribute incAttribute = new XmlIncludeAttribute(typeof(DerivedClass));<br />2) you create a new XmlAttributeOverrides:<br />XmlAttributeOverrides ovsAttributes = new XmlAttributeOverrides();<br />3) you add the IncludeAttribute to the attributes in ovsAttributes<br />ovsAttributes.Add(typeof(BaseClass), incAttribute);<br />4) XmlSerializer ser = new XmlSerializer(typeof(classToBeSerialized), ovsAttributes);Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-51529631264683648422009-01-13T22:22:48.000-08:002009-01-13T22:22:48.000-08:00I intend to use the same solution. When I was sear...I intend to use the same solution. When I was searching for one I didn't realise you can actually put <_.XmlInclude..> infront of a class. My solution incorporates it on the WebMethod itself.<br /><br />With all that being solved - did anyone measure the performance (a comment from VisualCron above)?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-25383464267231660522009-01-09T10:56:43.000-08:002009-01-09T10:56:43.000-08:00I've had no many problems trying to XmlSeriali...I've had no many problems trying to XmlSerialize a List<_ collection="collection" from="from" derive="derive" usually="usually" i="i" that="that"> instead.craig-anderanoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-69108973693392543472009-01-09T09:31:30.000-08:002009-01-09T09:31:30.000-08:00This does not work if "Animal" Class inh...This does not work if "Animal" Class inherits a collectionbase or List<br /><br />any idea if that was the case?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-33218502359081776562009-01-09T02:30:29.000-08:002009-01-09T02:30:29.000-08:00Thanks for this, and for the caption. When I saw i...Thanks for this, and for the caption. When I saw in the google results 'not quite as useless as i thought' I immediately knew it was addresses the problem I'd just come against.<br /><br />cheers,<br /><br />Ian.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-41886115350208169502008-12-03T13:23:10.000-08:002008-12-03T13:23:10.000-08:00You can milk anything with nipples.You can milk anything with nipples.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-24490388890946528992008-03-31T21:23:00.000-07:002008-03-31T21:23:00.000-07:00Nipples thats funny....Nipples thats funny....Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-89352104727009319442007-05-27T17:48:00.000-07:002007-05-27T17:48:00.000-07:00I don't know if anyone reads this, but I ran i...I don't know if anyone reads this, but I ran into problems with xmlinclude so here is my little type hack:<br /><br><br /><br>//serialize data<br /><br> string str = Serialize(content, types);<br /><br><br /><br>//create xml document<br /><br> XmlDocument doc = new XmlDocument();<br /><br>//load xml document from string data<br /><br> doc.LoadXml(str);<br /><br> if (doc.DocumentElement.Name != content.GetType().Name)<br /><br> {<br /><br> //create attribute <br /><br>doc.DocumentElement.SetAttributeNode("type", "http://www.w3.org/2001/XMLSchema-instance");<br /><br>//add xsi prefix <br /><br> doc.DocumentElement.Attributes["type"].Prefix = "xsi";<br /><br>//set type value (content is the object we are serializing)<br /><br> doc.DocumentElement.Attributes["xsi:type"].Value = content.GetType().Name;<br /><br> }<br /><br><br /><br>and you end up with xsi:type="myobjtype" in your xml headerAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-28325251643042508372006-10-31T17:25:00.000-08:002006-10-31T17:25:00.000-08:00I agree that decorating a base class with XMLInclu...I agree that decorating a base class with XMLInclude for all classes that inherit from it defeats the purpose of inheritance.<br /><br><br /><br>Turns out you can use the XMLInclude attribte to decorate the WebService class, that way you don't have to decorate each method. Also, if you have a webmethod that either returns the base type, or accepts it as a parameter, you don't even have to mess with XMLInclude.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-24013167853891814982006-07-05T05:31:00.000-07:002006-07-05T05:31:00.000-07:00The biggest problem I can see with this is that it...The biggest problem I can see with this is that it screws up schema generation. If you're writing a WebMethod, and you want to be able to generate code at the other end that will deserialize as either a Mammal or an Animal, then the signature of the WebMethod has to include information about what other types can be returned. Which means annotating the base type.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-87593124103675198202006-07-05T04:22:00.000-07:002006-07-05T04:22:00.000-07:00It strikes me that it would be far more useful to ...It strikes me that it would be far more useful to be able to annotate the derived class as opposed to the base class - the whole idea of a base class is for it to be unaware of its subclasses. <br /><br><br /><br>A better syntax / concept would be:<br /><br><br /><br>public class Mammal : Animal {<br /><br> [System.Xml.Serialization.XmlInclude(typeof(Animal)<br /><br> public int nipples; <br /><br>}<br /><br><br /><br>Then the serializer would be able to check and see if this subclass was allowed to be serialized in the context of serializing the superclass.<br /><br><br /><br><br /><br><br /><br>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-64193011987831118202006-04-20T06:50:00.000-07:002006-04-20T06:50:00.000-07:00The two likely causes are
1) A misconfiguration ...The two likely causes are <br /><br><br /><br>1) A misconfiguration of your web server. Be sure that you can hit it with a browser from the machine where you're running the windows app. <br /><br>2) A problem with the serialization of MyClass. You can test this by using XmlSerializer directly against an instance of MyClass, without going through web services. I think Chris Sells has a tool that will help you do this.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-50568408253153629062006-04-20T06:04:00.000-07:002006-04-20T06:04:00.000-07:00Hi, im passing a parameter to a webservice functio...Hi, im passing a parameter to a webservice function which is like this<br /><br><br /><br>[WebMethod]<br /><br>public void myfunc(MyClass abc)<br /><br>{<br /><br>.......<br /><br>}<br /><br><br /><br>Now, when i create an instance of "MyClass" and try to call to the myfunc method from a windows app (after adding a reference to the webservice), i get the error saying "there was an error generating the xml document". I tried all the methods u have mentioned, but i still get the error. What should i doAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-71444569015978950962006-02-17T09:47:00.000-08:002006-02-17T09:47:00.000-08:00Brilliant, thank you so much. I've been worki...Brilliant, thank you so much. I've been working on this for awhiel now.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-72904758724690682132005-12-12T03:26:00.000-08:002005-12-12T03:26:00.000-08:00Yes, XmlSerializer works by instantiating an insta...Yes, XmlSerializer works by instantiating an instance of your class and then calling the property getters and setters. So if no setter exists, then XmlSerializer (and therefore WebServices) won't work. <br /><br><br /><br>There is a way around this, however. You can implement an interface called IXmlSerializable, and take care of the serialization yourself. Depending on how complex the XML and the class are, this may be easy or difficult.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-49816415217156489492005-12-12T02:26:00.000-08:002005-12-12T02:26:00.000-08:00Oh...my...god. The problem, it appears, isn't...Oh...my...god. The problem, it appears, isn't that it is in an external assembly. Rather, that the entire set of properites on the class is ReadOnly (only getters, no setters). I am guessing that since there is no support for denoting an attribute as read only in XSD, it doesn't translate well to web services. Mystery solved. Thanks for all your help.<br /><br><br /><br>-Chris<br /><br>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-22513000871388152372005-12-12T02:00:00.000-08:002005-12-12T02:00:00.000-08:00Craig,
Thank goodness this may be something you ...Craig,<br /><br> Thank goodness this may be something you do all the time! I'm sorry I haven't been able to get back to this blog, the weekend had me buried in snow. I'm going to write a quick program that will take an instance of the class and generate the serialized XML and I'll report back my findings.<br /><br><br /><br>-Chris<br /><br>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-83632756563796320092005-12-08T11:03:00.000-08:002005-12-08T11:03:00.000-08:00If I understand what you're trying to do, it&#...If I understand what you're trying to do, it's something I do all the time.<br /><br><br /><br>Have you tried writing a simple test program that uses XmlSerializer to simply turn an instance of that class into XML? What happens when you do?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-32315381533297974802005-12-08T10:50:00.000-08:002005-12-08T10:50:00.000-08:00Can someone explain how I can get a class that is ...Can someone explain how I can get a class that is defined in an external assembly to be generated in the WSDL? I was experiencing a problem where none of the data encapsulated in this external class was being serialized down to the client. Looking at the WSDL shows an empty complexType node with the name of the external class. I've tried a combination of things to get it to serialize properly: Added an XmlType attribute to the external class to match the same namespace as the webservice; edited the asmx to make a reference to the assembly; used XmlInclude on the webservice method to try to force it to spit out the schema for the external type...NOTHING! Anyone have any pointers?<br /><br><br /><br>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-41202498076719152522005-11-01T07:27:00.000-08:002005-11-01T07:27:00.000-08:00Performance questions are always answered the same...Performance questions are always answered the same way: it depends. :) <br /><br><br /><br>In this case, it depends on how often you're using it, since there's extensive caching in the XmlSerializer infrastructure, and high setup costs. <br /><br><br /><br>I'm not aware of any specific issues with XmlInclude, although I've never measured it. I'd be a bit surprised if it added significantly to the cost of using XmlSerializer.<br /><br><br /><br>But there's no way around simply measuring the performance in your particular scenario. You just have to do it. Unless you're seeing massive slowdowns and are trying to figure out if this is the problem?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-58477173583696965852005-11-01T07:23:00.000-08:002005-11-01T07:23:00.000-08:00Do you know if it takes a lot of performance using...Do you know if it takes a lot of performance using a long row of XmlInclude in a class?<br /><br><br /><br>regards,<br /><br><br /><br>HenrikAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2441850399540300710.post-90203391096542057862004-07-09T02:27:00.000-07:002004-07-09T02:27:00.000-07:00One reason to use this technique is if you don'...One reason to use this technique is if you don't have access to the base type. That is, you want to take someone else's type and extend it, then write a WebMethod that can return either the base type or your extended type. If you don't have access to the base type source you can't add the [XmlInclude] attribute to it. But you can add it to your WebMethod.Anonymousnoreply@blogger.com