简体   繁体   中英

How do I ignore an [XMLIgnore] Attribute

I'm trying to serialize some objects obtained from a 3rd Party .NET Lib to an XML File.

When I Go To Definition for the object, some of the Properties of that object are marked as [XMLIgnore]

Is there any way to tell my System.Xml.Serialization.XmlSerializer to ignore the fact that some properties have that attribute and that it should serialize everything in the object.

I could probably obtain the source and recompile it without the XMLIgnore attributes but it'd be nice if XmlSerializer had some nice override property like

XmlSerializer xmls = new XmlSerializer(
   typeof(MyObject),
   Settings.DoNotApplyXMLAttributeRules
);

Thanks in advance


EDIT

Have tried the XmlAttributeOverrides as suggested but not having much joy. Here's the object definition (it's from the FlickrAPI for a Photo)

[Serializable]
public class Photo
{
    //Some code omitted
    [XmlIgnore]
    public string LargeUrl { get; }

}

And heres the serializer code I've written... still doesn't work...

XmlWriter xtw = XmlWriter.Create( Server.MapPath("~/App_Data/Data.xml") );

XmlAttributes photoAttributes = new XmlAttributes();
photoAttributes.XmlIgnore = false;

XmlAttributeOverrides photoOverrides = new XmlAttributeOverrides();
photoOverrides.Add(typeof(Photo), "LargeUrl", photoAttributes);

XmlSerializer xmlphoto = new XmlSerializer(typeof(Photo), photoOverrides);

use:

XmlAttributeOverrides

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

Edit: (Following the question EDIT)

the property must be public and have a getter and setter to be serialized.

http://msdn.microsoft.com/en-us/library/182eeyhh%28VS.85%29.aspx

((see first Note))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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