xmlserializer

XmlSerializer - Ignoring inherited unserializable member

﹥>﹥吖頭↗ 提交于 2020-01-21 14:57:09
问题 Assume you have two classes, one inherits the other and the child needs to be serialized / deserialized with XmlSerializer. However, the parent contains a member that is not serializeable, say a dictionary. public class Parent { public Dictionary<string, int> dictionary; } The parent class is a library used for many other scripts. It cannot be modified. Now the child class contains only serializable members: public class Child : Parent { [XmlElement] public int foo; } When trying to call the

Is XmlRootAttribute inheritable?

。_饼干妹妹 提交于 2020-01-21 10:39:31
问题 I have a class I am serializing with C#'s XmlSerializer. It is marked with the XmlRoot attribute, and I would like to inherit this attribute in a derived class. Looking at the documentation it does not say that XmlRoot sets Inherit to false with AttributeUsageAttribute (Inherit is supposed to default to true), but I get an error when trying to deserialize my inherited class without an XmlRoot attribute ("<rootNode xmlns=''> was not expected."). This currently works: [Serializable()] [XmlRoot(

Deserialize XML with multiple types

谁都会走 提交于 2020-01-15 09:48:28
问题 I'm trying to deserialize XML where some same name tags have different xsi types: <user-defined-data-row> <field name="entity"> <field-value xsi:type="field-text-valueType"> <value>Test</value> </field-value> </field> <field name="expiry_date"> <field-value xsi:type="field-date-valueType"> <value>2001-10-07</value> </field-value> </field> </user-defined-data-row> This is easily achieved by deserializing the xml into this model: [XmlRoot(ElementName = "field-value", Namespace = "http://www

Why is XmlSerializer's Deserialize() spitting out a child object which is a XmlNode[]?

五迷三道 提交于 2020-01-14 04:32:06
问题 I'm using XmlSerializer to serialize and then deserialize a simple object. When I deserialize the object to my surprise I find a child object was not properly deserialized but instead turned into XmlNode[] . Here is very nearly the structure I've got: // This line I put in here as a way of sneaking into the XML the // root node's C# namespace, since it's not the same as the // deserializing code and the deserializing code seemed unable to // deserialize properly without knowing the Type (see

XMLSerializer warns about unknown nodes/attributes when deserializing derived types

眉间皱痕 提交于 2020-01-13 20:40:14
问题 I recently registered event handlers for unknown nodes, elements and attributes with the XMLSerializer I use to deserialize complex types from a type hierarchy. I did this because some of the XML I receive is from third parties; I am interested in data format changes which could cause trouble on my end. In the XML the XMLSerializer produces it uses the standard XML attribute xsi:type="somederivedtypename" to identify the actual derived type represented by an XML element. I was surprised to

Implementing IXmlSerializable Requires Collection Property to Have Setter

情到浓时终转凉″ 提交于 2020-01-11 09:46:12
问题 I have a collection property that is of a custom type which inherits from BindingList. Currently, this property gets serialized via XmlSerializer even though it has no Setter. I now am trying to implement IXmlSerializable on this custom collection class and see that the WriteXml() and ReadXml() interface methods only get called if my collection property has a Setter. Why does serialization ignore this property now unless there is a Setter when before it serialized correctly without one. To

XML Deserialization of collection property with code defaults

孤人 提交于 2020-01-08 17:20:12
问题 For application configuration, I frequently will create a configuration class with configuration values for the application that I then deserialize into an object to utilize. The configuration object is usually databound to a user interface control so that the configuration can be changed and persisted by the user. The configuration class typically has default values assigned to the properties so that there is always a default configuration. This has worked well. I recently had a situation

IXmlSerializable and Root Element Prefix

為{幸葍}努か 提交于 2020-01-06 18:32:31
问题 I have looked everywhere and not a single response is valid or the question is just slightly off to the point of not getting me the answer I need. Given all the searches I have looked for there seems to be one MASSIVE flaw in .Net's implementation of xml serialization. Default: [XmlRoot("root", Namespace="http://myclass.org/")] public class MyClass { } void Main() { XmlSerializer ser = new XmlSerializer(typeof(MyClass)); XmlSerializerNamespaces xsn = new XmlSerializerNamespaces(); xsn.Add("mc

validate xml during serialization

痞子三分冷 提交于 2020-01-06 07:13:06
问题 This post https://stackoverflow.com/a/1708614/5333340 gives a solution on how to validate an xml during deserialization. It also says that similar code can be written for serialization, but I was not able to figure it out. Can someone give a hint? I want to do the validation during serialization, so that, if the validation fails at some point, the serialization stops immediately. Based on the linked answer my deserialization code, where validation takes place, looks like this: private static

validate xml during serialization

旧街凉风 提交于 2020-01-06 07:13:06
问题 This post https://stackoverflow.com/a/1708614/5333340 gives a solution on how to validate an xml during deserialization. It also says that similar code can be written for serialization, but I was not able to figure it out. Can someone give a hint? I want to do the validation during serialization, so that, if the validation fails at some point, the serialization stops immediately. Based on the linked answer my deserialization code, where validation takes place, looks like this: private static