I have used xsd.exe to generate .cs file from my XML response.
In my XML response there are few elements which are not required in app, I want to omit those, so I removed them from response xml file and regenerated the .cs file.
I use this .cs file to deserialize from my xml server response (which has those unnecessary elements), which causes System.InvalidOperationException.
Please let me know how can I parse or deserialize the server response to the class which has only those properties which are required.
I wok for Windows Phone app.
Thanks,
Kavit.
Try decorating the properties you no longer want with the XmlIgnoreAttribute attribute.
By default, all public fields and public read/write properties are
serialized by the XmlSerializer. That is, the value of each public
field or property is persisted as an XML element or XML attribute in
an XML-document instance.
To override the default serialization of a field or property, create
an XmlAttributes object, and set its XmlIgnore property to true. Add
the object to an XmlAttributeOverrides object and specify the type of
the object that contains the field or property to ignore, and the name
of the field or property to ignore.
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlignore.aspx
Related
I have a problem with Serialization and Deserialization of an object. I have an object Foo with different properties and some of the properties have the attribute NonSerialized. I have some cases when I want to serialize the fields and to read the serialized object with all the fields. Is this possible to do?
If you have control over the class Foo you could use the ShouldSerialize attribute to control whether the field should be serialized. For a idea how it works you can look here Xml serialization - Hide null values
I have an xml that contains certain information that was serialized in a certain version. Now in next version, I want that information to be deserialized, but dont serialize it back in the same property/element. That information will be handled in a child class and that will serialize it. I know I can use XmlIgnore but that will ignore both Serialization and deserialization.
P.S: I could make the string attributes null, but what about the doubles, int?
I have a dynamic XML structure. I mean, root name can be changed, element name can be changed and structure of xml can be changed.So How can I deserialize dynamic xml to objects?
Creating classes dynamically by xml element names, is this possible? If yes, then how can I do this?
So How can I deserialize this type xml to objects?
Well, you cannot deserialize a XML without a specific XSD schema to statically typed classes. You could use a XDocument or XmlReader classes to parse and extract information from it.
If you're using .Net 4, the ExpandoObject might be what you need:
Represents an object whose members can be dynamically added and removed at run time.
You want to look into the factory pattern. The basic idea is:
for each XML node, get the type name / name of node
ask factory for type with given name
create object of the given type
pass the XML node to object to allow it to load its data
XSD schemas help here to verify the XML is structurally correct although not necessary. I did try this once, creating objects from XML, but I never found a way to have multiple schema files for one XML (this would have allowed me to extend the schema without changing the root schema, if you see what I mean).
I have a use case where I am serializing objects over the wire via MSMQ (mostly strings). When I read the object off the queue I want to be able to tell if the user meant for the object to be a XML or a string. I was thinking a good way to do this would just be to check the type. If it's XmlElement than it becomes XML data otherwise it becomes string or CDATA. The reason I don't want to just check to see if the data is valid XML is that sometimes data will be provided that is supposed to be serialized as a string but is in fact valid XML. I want the caller to be able to control the de-serialization into string or XML.
Are there any types that are marked as serializable in the .NET Framework like XElement or XmlElement (both which are not marked serializable)?
Why don't you just add a property to the class of the serialized object that tells you what it is? I'd propose IsXml.
I have a object that has a number of properties that aren't present in the xsd file. When doing XmlDocument.Validate is there a way I can tell it to ignore properties that are not present in the xsd and instead just ensure that the properties required by xsd are present in the xml document?
I can get around this by adding [XmlIgnore] attributes all over my class but I would rather accomplish this by convention rather then explicitly add attributes all throughout my object model.
I doubt there is. Personally I would create a separate DTO, as it sounds like you're trying to make one object serve two jobs. Another option would be to use the XmlSerializer ctor that allows you to specify attribs at runtime, but this is much more work than [XmlIgnore].
So if you just want it to work: [XmlIgnore]. If you want it to be "pure", create a second DTO model and translate between them.