Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 801
Eoin Campbell Avatar asked Sep 05 '09 16:09

Eoin Campbell


1 Answers

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))

like image 153
manji Avatar answered Oct 25 '22 19:10

manji