views:

1008

answers:

1

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);
+5  A: 

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

najmeddine
Have tried that but no joy. Any other suggestions would be appreciated. I've added the code to the original question in case I'm doing something silly.
Eoin Campbell
LargeUrl does not have a setter, so even without the XmlIgnore attribute, it won't be serialized.
najmeddine
ah right. I've gotten around it by creating a proxy class to store the values in and then serializing that seperately.
Eoin Campbell
is my answer correct?
najmeddine