XmlSerializer.UnknownElement 事件


XmlSerializer 在反序列化过程中遇到未知类型的 XML 元素时发生。Occurs when the XmlSerializer encounters an XML element of unknown type during deserialization.

 event System::Xml::Serialization::XmlElementEventHandler ^ UnknownElement;
public event System.Xml.Serialization.XmlElementEventHandler UnknownElement;
member this.UnknownElement : System.Xml.Serialization.XmlElementEventHandler 
Public Custom Event UnknownElement As XmlElementEventHandler 


下面的示例从名为 UnknownElements 的文件中对名为 Group 的类进行反序列化。The following example deserializes a class named Group from a file named UnknownElements.xml. 只要在文件中找到了类中没有相应成员的元素,就会发生 UnknownElement 事件。Whenever an element is found in the file that has no corresponding member in the class, the UnknownElement event occurs. 若要尝试该示例,请将以下 XML 代码粘贴到名为 UnknownElements 的文件中。To try the example, paste the following XML code into a file named UnknownElements.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">  
#using <System.Xml.dll>
#using <System.dll>

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

public ref class Group
   String^ GroupName;

public ref class Test
   void Serializer_UnknownElement( Object^ sender, XmlElementEventArgs^ e )
      Console::WriteLine( "Unknown Element" );
      Console::Write( "\t {0}", e->Element->Name );
      Console::WriteLine( " {0}", e->Element->InnerXml );
      Console::WriteLine( "\t LineNumber: {0}", e->LineNumber );
      Console::WriteLine( "\t LinePosition: {0}", e->LinePosition );
      Group^ x = dynamic_cast<Group^>(e->ObjectBeingDeserialized);
      Console::WriteLine( x->GroupName );
      Console::WriteLine( sender );

   void DeserializeObject( String^ filename )
      XmlSerializer^ ser = gcnew XmlSerializer( Group::typeid );

      // Add a delegate to handle unknown element events.
      ser->UnknownElement += gcnew XmlElementEventHandler( this, &Test::Serializer_UnknownElement );

      // A FileStream is needed to read the XML document.
      FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
      Group^ g = dynamic_cast<Group^>(ser->Deserialize( fs ));

int main()
   Test^ t = gcnew Test;

   // Deserialize the file containing unknown elements.
   t->DeserializeObject( "UnknownElements.xml" );
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;

public class Group{
   public string GroupName;

public class Test{
   static void Main(){
      Test t = new Test();
      // Deserialize the file containing unknown elements.
   private void Serializer_UnknownElement(object sender, XmlElementEventArgs e){
      Console.WriteLine("Unknown Element");
      Console.WriteLine("\t" + e.Element.Name + " " + e.Element.InnerXml);
      Console.WriteLine("\t LineNumber: " + e.LineNumber);
      Console.WriteLine("\t LinePosition: " + e.LinePosition);
      Group x  = (Group) e.ObjectBeingDeserialized;
      Console.WriteLine (x.GroupName);
      Console.WriteLine (sender.ToString());
   private void DeserializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      // Add a delegate to handle unknown element events.
      ser.UnknownElement+=new XmlElementEventHandler(Serializer_UnknownElement);
      // A FileStream is needed to read the XML document.
     FileStream fs = new FileStream(filename, FileMode.Open);
     Group g = (Group) ser.Deserialize(fs);
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Xml.Schema

Public Class Group
   Public GroupName As String 
End Class

Public Class Test
   Shared Sub Main()
      Dim t As Test = new Test()
      ' Deserialize the file containing unknown elements.
   End Sub

   Private Sub Serializer_UnknownElement _
   (sender As Object , e As XmlElementEventArgs)
      Console.WriteLine("Unknown Element")
      Console.WriteLine(ControlChars.Tab & e.Element.Name + " " & e.Element.InnerXml)
      Console.WriteLine(ControlChars.Tab & e.LineNumber & ":"  & e.LineNumber)
      Console.WriteLine(ControlChars.Tab & e.LinePosition & ":"   & e.LinePosition)
      Dim x As Group = CType( e.ObjectBeingDeserialized, Group)
      Console.WriteLine (x.GroupName)
      Console.WriteLine (sender.ToString())
   End Sub
   Private Sub DeserializeObject(filename As String)
      Dim ser As XmlSerializer = new XmlSerializer(GetType(Group))
      ' Add a delegate to handle unknown element events.
      AddHandler ser.UnknownElement, _
      AddressOf Serializer_UnknownElement 
      ' 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)
   End Sub
End Class


默认情况下,在调用 Deserialize 方法后,XmlSerializer 忽略未知类型的 XML 属性。By default, after calling the Deserialize method, the XmlSerializer ignores XML attributes of unknown types. 但是,可以使用此事件来处理此类节点类型。However, you can use this event to handle such node types.


如果 XmlAnyElementAttribute 应用于返回 XmlElement 对象的数组的字段,则在数组中收集所有未知元素。If the XmlAnyElementAttribute is applied to a field that returns an array of XmlElement objects, all unknown elements are collected in the array. 在这种情况下,不会发生 UnknownElement 事件。In that case, the UnknownElement event does not occur.