How to access Overridden Attributes in a IXmlSerializable object

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Ive searched for answers, most are to setup the override process, but I havent found any that try to access the overridden attributes within the WriteXml method of the IXmlSerializable object.
I have an IXmlSerializable object and I have marked fields and properties with an [XmlIgnore] attribute. Understandably I can access these attributes using GetCustomAttributes. However this collection does not include any attributes that were added to the
XmlAttributeOverrides collection that was used to create the XmlSerializer used to serialize the object.
Is there a method to access the added attributes within the WriteXml method?
My attempt: This is dummy object just presented as an example
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; class Person : IXmlSerializable
{
<span style="color:Blue; public <span style="color:Blue; string Name1;

<span style="color:Blue; public <span style="color:Blue; string Name2;

[XmlIgnore]
<span style="color:Blue; public <span style="color:Blue; string Name3;

<span style="color:Blue; public Person()
{
}

<span style="color:Blue; public Person(<span style="color:Blue; string first, <span style="color:Blue; string second, <span style="color:Blue; string third)
{
Name1 = first;
Name2 = second;
Name3 = third;
}

<span style="color:Blue; public XmlSchema GetSchema()
{
<span style="color:Blue; return <span style="color:Blue; null;
}

<span style="color:Blue; public <span style="color:Blue; void ReadXml(XmlReader reader)
{
<span style="color:Green; // ....
}

<span style="color:Blue; public <span style="color:Blue; void WriteXml(XmlWriter writer)
{
FieldInfo[] finfo = <span style="color:Blue; this.GetType().GetFields();

<span style="color:Blue; foreach (FieldInfo finf <span style="color:Blue; in finfo)
{
<span style="color:Blue; object[] atts = finf.GetCustomAttributes(<span style="color:Blue; true);

<span style="color:Blue; if (atts.Length == 0)
{
<span style="color:Green; // fields without attributes appear here
writer.WriteElementString(finf.Name, (<span style="color:Blue; string)finf.GetValue(<span style="color:Blue; this));
}
<span style="color:Blue; else
{
<span style="color:Green; // only fields with compiler added attributes appear here.
<span style="color:Green; // Any fields with XmlAttributeOverrides do NOT appear here...
}
}
}
}

[/code]
And the code used to create the override:
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 <font size="2" color="#008000 <font size="2" color="#008000
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // parent app...

<span style="color:Blue; public XmlSerializer CreateOverrider()
{
XmlAttributeOverrides xOver = <span style="color:Blue; new XmlAttributeOverrides();
XmlAttributes attrs = <span style="color:Blue; new XmlAttributes();

attrs.XmlIgnore = <span style="color:Blue; true;
xOver.Add(<span style="color:Blue; typeof(Person), <span style="color:#A31515; "Name2", attrs);

XmlSerializer xSer = <span style="color:Blue; new XmlSerializer(<span style="color:Blue; typeof(Person), xOver);
<span style="color:Blue; return xSer;
}

<span style="color:Blue; private <span style="color:Blue; void button2_Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Green; // Custom XmlSerialize
Person pson = <span style="color:Blue; new Person(<span style="color:#A31515; "First", <span style="color:#A31515; "Second", <span style="color:#A31515; "Third");

XmlSerializer serializer = CreateOverrider();
TextWriter writer = <span style="color:Blue; new StreamWriter(<span style="color:#A31515; "PersonOverride.xml");

serializer.Serialize(writer, pson);
writer.Close();
}

<span style="color:Green; // etc...

[/code]
</font></font><font size="2" color="#008000 </font>
I understand that the this.GetType().GetCustomAttributes() is probably doing the right thing, returning the compiler added attributes of the Person class. So how do I access the XmlAttributeOverrides collection passed to the XmlSerializer?
Any advice greatfully welcome.

View the full article
 
Back
Top