Yes,  you can override the attributes by passing an XmlAttributeOverrides
object to the constructor if the XmlSerializer when you serialize the
object:

using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

public class Test
{
    [XmlAttribute("attribute1")]
    public string attribute1;

    public string foo = "bar";
    public Test(){}
}

public class App
{
  public static void Main()
  {
    XmlAttributes attrs = new XmlAttributes();
    attrs.XmlIgnore = true;

    XmlAttributeOverrides overrides = new XmlAttributeOverrides();
    overrides.Add( typeof(Test), attrs );

    XmlSerializer ser = new XmlSerializer( typeof(Test), overrides );
    XmlTextWriter writer = new XmlTextWriter( "ignore.xml", Encoding.UTF8 );
    ser.Serialize( writer, new Test() );
    writer.Close();
  }
}


Note that you do not want to pass the XmlAttributeOverrides to the
XmlSerializer that dedeserializes the XML doc or attribute1 will be ignored.

HTH,
Christoph


----- Original Message -----
From: "Łukasz Wielek" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, May 15, 2002 6:09 AM
Subject: [DOTNET] Dynamic XmlAttributes


I have class :
public Test
{
    [XmlAttribute("attribute1")]
    public string attribute1;
    public Test(){}
    /* .... */
}

but attribute attribute1 makes sense only during deserialization of class
Test.
When I serialize this class I don't want this attribute to appear.

Is it possible to dynamically change [XmlAttribute("attribute1")] to
[XmlIgnore] ???


--
WielO
mailto:[EMAIL PROTECTED]
-----------------------------------------------------

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to