XmlNodeEventHandler 委托
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示处理 XmlSerializer 的 UnknownNode 事件的方法。
public delegate void XmlNodeEventHandler(System::Object ^ sender, XmlNodeEventArgs ^ e);
public delegate void XmlNodeEventHandler(object? sender, XmlNodeEventArgs e);
public delegate void XmlNodeEventHandler(object sender, XmlNodeEventArgs e);
type XmlNodeEventHandler = delegate of obj * XmlNodeEventArgs -> unit
Public Delegate Sub XmlNodeEventHandler(sender As Object, e As XmlNodeEventArgs)
参数
- sender
- Object
事件源。
包含事件数据的 XmlNodeEventArgs。
示例
以下示例创建 , XmlSerializer为 UnknownNode 事件添加事件处理程序,并反序列化 对象。
private:
void DeserializeItem( String^ filename )
{
XmlSerializer^ mySerializer = gcnew XmlSerializer( ObjectToDeserialize::typeid );
// Add an instance of the delegate to the event.
mySerializer->UnknownNode += gcnew XmlNodeEventHandler( this, &Class1::Serializer_UnknownNode );
// A FileStream is needed to read the file to deserialize.
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
ObjectToDeserialize^ o = dynamic_cast<ObjectToDeserialize^>(mySerializer->Deserialize( fs ));
}
void Serializer_UnknownNode( Object^ sender, XmlNodeEventArgs^ e )
{
Console::WriteLine( "UnknownNode Name: {0}", e->Name );
Console::WriteLine( "UnknownNode LocalName: {0}", e->LocalName );
Console::WriteLine( "UnknownNode Namespace URI: {0}", e->NamespaceURI );
Console::WriteLine( "UnknownNode Text: {0}", e->Text );
Object^ o = e->ObjectBeingDeserialized;
Console::WriteLine( "Object being deserialized {0}", o );
XmlNodeType myNodeType = e->NodeType;
Console::WriteLine( myNodeType );
}
private void DeserializeItem(string filename) {
XmlSerializer mySerializer =
new XmlSerializer(typeof(ObjectToDeserialize));
// Add an instance of the delegate to the event.
mySerializer.UnknownNode += new XmlNodeEventHandler
(Serializer_UnknownNode);
// A FileStream is needed to read the file to deserialize.
FileStream fs = new FileStream(filename, FileMode.Open);
ObjectToDeserialize o = (ObjectToDeserialize)mySerializer.Deserialize(fs);
}
private void Serializer_UnknownNode
(object sender, XmlNodeEventArgs e) {
Console.WriteLine("UnknownNode Name: "
+ e.Name);
Console.WriteLine("UnknownNode LocalName: "
+ e.LocalName);
Console.WriteLine("UnknownNode Namespace URI: "
+ e.NamespaceURI);
Console.WriteLine("UnknownNode Text: "
+ e.Text);
object o = e.ObjectBeingDeserialized;
Console.WriteLine("Object being deserialized "
+ o.ToString());
XmlNodeType myNodeType = e.NodeType;
Console.WriteLine(myNodeType);
}
Private Sub DeserializeItem(ByVal filename As String)
Dim mySerializer As New XmlSerializer(GetType(ObjectToDeserialize))
' Add an instance of the delegate to the event.
AddHandler mySerializer.UnknownNode, AddressOf Serializer_UnknownNode
' A FileStream is needed to read the file to deserialize.
Dim fs As New FileStream(filename, FileMode.Open)
Dim o As ObjectToDeserialize = _
CType(mySerializer.Deserialize(fs), ObjectToDeserialize)
End Sub
Private Sub Serializer_UnknownNode _
(ByVal sender As Object, _
ByVal e As XmlNodeEventArgs)
Console.WriteLine("UnknownNode Name: " & e.Name)
Console.WriteLine("UnknownNode LocalName: " & e.LocalName)
Console.WriteLine("UnknownNode Namespace URI: " & e.NamespaceURI)
Console.WriteLine("UnknownNode Text: " & e.Text)
Dim o As Object = e.ObjectBeingDeserialized
Console.WriteLine("Object being deserialized " & o.ToString())
Dim myNodeType As XmlNodeType = e.NodeType
Console.WriteLine(myNodeType)
End Sub
注解
创建 XmlNodeEventHandler 委托时,需要标识处理事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件。
仅 UnknownNode 当调用 方法时, Deserialize 才会发生 该事件。
扩展方法
GetMethodInfo(Delegate) |
获取指示指定委托表示的方法的对象。 |