Επεξεργασία

Κοινή χρήση μέσω


XmlAnyAttributeAttribute Constructor

Definition

Constructs a new instance of the XmlAnyAttributeAttribute class.

public:
 XmlAnyAttributeAttribute();
public XmlAnyAttributeAttribute ();
Public Sub New ()

Examples

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 that contains 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>  
#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 ref class Group
{
public:
   String^ GroupName;

   // The Things array will be used to collect all unknown
   // attributes found when deserializing.
   array<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 = gcnew FileStream( filename,FileMode::Open );
   Group^ g = safe_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 = safe_cast<XmlAttribute^>(e->Current);
      Console::WriteLine( "{0}: {1}", xAtt->Name, xAtt->InnerXml );
   }
}

XmlSerializer^ CreateOverrideSerializer()
{
   // Override the Things field to capture all
   // unknown XML attributes.
   XmlAnyAttributeAttribute^ myAnyAttribute = gcnew XmlAnyAttributeAttribute;
   XmlAttributeOverrides^ xOverride = gcnew XmlAttributeOverrides;
   XmlAttributes^ xAtts = gcnew XmlAttributes;
   xAtts->XmlAnyAttribute = myAnyAttribute;
   xOverride->Add( Group::typeid, "Things", xAtts );
   return gcnew XmlSerializer( Group::typeid,xOverride );
}

int main()
{
   DeserializeObject(  "UnknownAttributes.xml" );
}
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);
   }
}
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

Applies to