XmlSerializer::UnknownNode Event
.NET Framework 4.5
Occurs when the XmlSerializer encounters an XML node of unknown type during deserialization.
Namespace: System.Xml.Serialization
Assembly: System.Xml (in System.Xml.dll)
By default, after calling the Deserialize method, the XmlSerializer ignores XML nodes of unknown types. However, you can use this event to handle such node types.
The following example prints the type of any encountered unknown node.
#using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::IO; using namespace System::Xml; using namespace System::Xml::Serialization; public ref class Group { public: // Only the GroupName field will be known. String^ GroupName; }; public ref class Test { public: static void main() { Test^ t = gcnew Test; t->DeserializeObject( "UnknownNodes.xml" ); } private: void DeserializeObject( String^ filename ) { XmlSerializer^ mySerializer = gcnew XmlSerializer( Group::typeid ); FileStream^ fs = gcnew FileStream( filename,FileMode::Open ); mySerializer->UnknownNode += gcnew XmlNodeEventHandler( this, &Test::serializer_UnknownNode ); Group^ myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( fs )); fs->Close(); } private: 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 ); XmlNodeType myNodeType = e->NodeType; Console::WriteLine( "NodeType: {0}", myNodeType ); Group^ myGroup = dynamic_cast<Group^>(e->ObjectBeingDeserialized); Console::WriteLine( "GroupName: {0}", myGroup->GroupName ); Console::WriteLine(); } }; int main() { Test::main(); } /* Paste this XML into a file named UnknownNodes: <?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" xmlns:coho = "http://www.cohowinery.com" xmlns:cp="http://www.cpandl.com"> <coho:GroupName>MyGroup</coho:GroupName> <cp:GroupSize>Large</cp:GroupSize> <cp:GroupNumber>444</cp:GroupNumber> <coho:GroupBase>West</coho:GroupBase> <coho:ThingInfo> <Number>1</Number> <Name>Thing1</Name> <Elmo> <Glue>element</Glue> </Elmo> </coho:ThingInfo> </Group> */
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.