简体   繁体   English

XmlSerializer.Deserialize忽略不必要的元素

[英]XmlSerializer.Deserialize ignore unnecessary elements

I have used xsd.exe to generate .cs file from my XML response. 我已经使用xsd.exe从XML响应中生成.cs文件。

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. 在我的XML响应中,应用程序中不需要一些元素,我想省略这些元素,因此我从响应xml文件中将其删除,然后重新生成了.cs文件。

I use this .cs file to deserialize from my xml server response (which has those unnecessary elements), which causes System.InvalidOperationException. 我使用此.cs文件从xml服务器响应(包含那些不必要的元素)中反序列化,这导致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. 我为Windows Phone应用启动了。

Thanks, Kavit. 谢谢,卡维特。

Try decorating the properties you no longer want with the XmlIgnoreAttribute attribute. 尝试使用XmlIgnoreAttribute属性装饰不再需要的属性。

By default, all public fields and public read/write properties are serialized by the XmlSerializer. 默认情况下,所有公共字段和公共读/写属性都由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. 即,每个公共字段或属性的值都作为XML元素或XML属性保留在XML文档实例中。

To override the default serialization of a field or property, create an XmlAttributes object, and set its XmlIgnore property to true. 若要覆盖字段或属性的默认序列化,请创建XmlAttributes对象,并将其XmlIgnore属性设置为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. 将对象添加到XmlAttributeOverrides对象,并指定包含要忽略的字段或属性的对象的类型,以及要忽略的字段或属性的名称。

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlignore.aspx http://msdn.microsoft.com/zh-CN/library/system.xml.serialization.xmlattributes.xmlignore.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM