This documentation is archived and is not being maintained.

XmlAnyAttributeAttribute Constructor

Constructs a new instance of the XmlAnyAttributeAttribute class.

[Visual Basic]
Public Sub New()
[C#]
public XmlAnyAttributeAttribute();
[C++]
public: XmlAnyAttributeAttribute();
[JScript]
public function XmlAnyAttributeAttribute();

Example

[Visual Basic, C#, C++] The following example constructs an XmlAnyAttributeAttribute that is used to override the deserialization of an object. To try the example, create a file named UnknownAttributes.xml containing the following XML:

<?xml version="1.0" encoding="utf-8"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" GroupType = 'Technical' GroupNumber = '42' GroupBase = 'Red'>
  <GroupName>MyGroup</GroupName>
</Group>
[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml

Public Class Group
   ' The Things array will be used to collect all unknown
   ' attributes found when deserializing.
   public GroupName As String 
   public Things() As XmlAttribute
End Class

Public Class Test
   Shared Sub Main()
      Dim t As Test = New Test()
      t.DeserializeObject("UnknownAttributes.xml")
   End Sub

   Private Sub SerializeObject(filename As String)
      Dim ser As XmlSerializer = New XmlSerializer(GetType (Group))
      Dim writer As TextWriter = New StreamWriter(filename)
      Dim g As Group = New Group()
      g.GroupName = "MyGroup"
      
      ser.Serialize(writer, g)
      writer.Close()
   End Sub

   
   Private Sub DeserializeObject(filename As String)
      ' Use the CreateOverrideSerializer to return an instance
      ' of the XmlSerializer customized for overrides.
      Dim ser As XmlSerializer = CreateOverrideSerializer()
      ' A FileStream is needed to read the XML document.
      Dim fs As FileStream = New FileStream(filename, FileMode.Open)
     Dim g As Group = CType( _
         ser.Deserialize(fs), Group)
     fs.Close()
     Console.WriteLine(g.GroupName)
     Console.WriteLine(g.Things.Length)
     Dim xAtt As XmlAttribute
     for each xAtt in g.Things
        Console.WriteLine(xAtt.Name & ": " & xAtt.InnerXml)
     Next
   End Sub

   Private Function CreateOverrideSerializer() As XmlSerializer 
      ' Override the Things field to capture all
      ' unknown XML attributes.
      Dim myAnyAttribute As XmlAnyAttributeAttribute = _
      New XmlAnyAttributeAttribute()
      Dim xOverride As XmlAttributeOverrides = _
      New XmlAttributeOverrides()
      Dim xAtts As XmlAttributes = New XmlAttributes()
      xAtts.XmlAnyAttribute=myAnyAttribute
      xOverride.Add(GetType(Group), "Things", xAtts)
      return New XmlSerializer(GetType(Group) , xOverride)
   End Function
End Class

[C#] 
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

public class Group{
   public string GroupName;
   // The Things array will be used to collect all unknown
   // attributes found when deserializing.
   public XmlAttribute[]Things;
}

public class Test{
   static void Main(){
      Test t = new Test();
      t.DeserializeObject("UnknownAttributes.xml");
   }

   private void DeserializeObject(string filename){
      // Use the CreateOverrideSerializer to return an instance
      // of the XmlSerializer customized for overrides.
      XmlSerializer ser = CreateOverrideSerializer();
      // A FileStream is needed to read the XML document.
      FileStream fs = new FileStream(filename, FileMode.Open);
     Group g = (Group)
         ser.Deserialize(fs);
     fs.Close();
     Console.WriteLine(g.GroupName);
     Console.WriteLine(g.Things.Length);
     foreach(XmlAttribute xAtt in g.Things){
        Console.WriteLine(xAtt.Name + ": " + xAtt.InnerXml);
        }
   }

   private XmlSerializer CreateOverrideSerializer(){
      // Override the Things field to capture all
      // unknown XML attributes.
      XmlAnyAttributeAttribute myAnyAttribute = 
      new XmlAnyAttributeAttribute();
      XmlAttributeOverrides xOverride = 
      new XmlAttributeOverrides();
      XmlAttributes xAtts = new XmlAttributes();
      xAtts.XmlAnyAttribute=myAnyAttribute;
      xOverride.Add(typeof(Group), "Things", xAtts);
       
      return new XmlSerializer(typeof(Group) , xOverride);
   }
}

[C++] 

#using <mscorlib.dll>
#using <System.dll>
#using <System.XML.dll>

using namespace System;
using namespace System::Collections;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Xml;

public __gc class Group
{
public:
   String* GroupName;
   // The Things array will be used to collect all unknown
   // attributes found when deserializing.
   XmlAttribute*    Things[];
};

XmlSerializer* CreateOverrideSerializer();

void DeserializeObject( String* filename )
{
   // Use the CreateOverrideSerializer to return an instance
   // of the XmlSerializer customized for overrides.
   XmlSerializer* ser = CreateOverrideSerializer();
   // A FileStream is needed to read the XML document.
   FileStream* fs = new FileStream(filename, FileMode::Open);

   Group* g = __try_cast<Group*>( ser->Deserialize(fs));
   fs->Close();

   Console::WriteLine(g->GroupName);
   Console::WriteLine(g->Things->Length);

   for ( IEnumerator* e = g->Things->GetEnumerator(); e->MoveNext(); )
   {
      XmlAttribute* xAtt = __try_cast<XmlAttribute*>( e->Current );
      Console::WriteLine( S"{0}: {1}", xAtt->Name, xAtt->InnerXml);
   }
}

XmlSerializer* CreateOverrideSerializer()
{
   // Override the Things field to capture all
   // unknown XML attributes.
   XmlAnyAttributeAttribute* myAnyAttribute = 
      new XmlAnyAttributeAttribute();
   XmlAttributeOverrides* xOverride = 
      new XmlAttributeOverrides();
   XmlAttributes* xAtts = new XmlAttributes();
   xAtts->XmlAnyAttribute = myAnyAttribute;
   xOverride->Add(__typeof(Group), S"Things", xAtts);

   return new XmlSerializer(__typeof(Group) , xOverride);
}

int main()
{
   DeserializeObject("UnknownAttributes.xml");
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlAnyAttributeAttribute Class | XmlAnyAttributeAttribute Members | System.Xml.Serialization Namespace

Show: