다음을 통해 공유


XmlSerializer.UnknownNode 이벤트

XmlSerializer가 deserialization을 수행하는 동안 알 수 없는 형식의 XML 노드가 나타날 때 발생합니다.

네임스페이스: System.Xml.Serialization
어셈블리: System.Xml(system.xml.dll)

구문

‘선언
Public Event UnknownNode As XmlNodeEventHandler
‘사용 방법
Dim instance As XmlSerializer
Dim handler As XmlNodeEventHandler

AddHandler instance.UnknownNode, handler
public event XmlNodeEventHandler UnknownNode
public:
event XmlNodeEventHandler^ UnknownNode {
    void add (XmlNodeEventHandler^ value);
    void remove (XmlNodeEventHandler^ value);
}
/** @event */
public void add_UnknownNode (XmlNodeEventHandler value)

/** @event */
public void remove_UnknownNode (XmlNodeEventHandler value)
JScript에서는 이벤트를 사용할 수 있지만 새로 선언할 수는 없습니다.

설명

기본적으로, Deserialize 메서드가 호출된 뒤에는, XmlSerializer는 알 수 없는 형식의 XML 노드를 무시합니다. 그러나, 이 이벤트를 사용하여 이러한 노드 형식을 처리할 수 있습니다.

예제

다음 예제에서는 발견되는 모든 알 수 없는 노드의 형식을 인쇄합니다.

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

Public Class Group
   ' Only the GroupName field will be known.
   Public GroupName As String
End Class

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

   Private Sub DeserializeObject(filename As String )
      Dim mySerializer As XmlSerializer  = New XmlSerializer(GetType(Group))
      Dim fs As FileStream = New FileStream(filename, FileMode.Open)
      AddHandler mySerializer.UnknownNode, _
      AddressOf serializer_UnknownNode
      Dim myGroup As Group = _
      CType(mySerializer.Deserialize(fs), Group)
      fs.Close()
   End Sub
   
   Private Sub serializer_UnknownNode _
   (sender As object , e As XmlNodeEventArgs )
      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)

      Dim myNodeType As XmlNodeType = e.NodeType
      Console.WriteLine("NodeType: {0}", myNodeType)
 
      Dim myGroup As Group = CType(e.ObjectBeingDeserialized, Group)
      Console.WriteLine("GroupName: {0}", myGroup.GroupName)
      Console.WriteLine()
   End Sub
End Class
' 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>
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Group{
   // Only the GroupName field will be known.
   public string GroupName;
}

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

   private void DeserializeObject(string filename){
      XmlSerializer mySerializer = new XmlSerializer(typeof(Group));
      FileStream fs = new FileStream(filename, FileMode.Open);
      mySerializer.UnknownNode += new 
      XmlNodeEventHandler(serializer_UnknownNode);
      Group myGroup = (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 = (Group) e.ObjectBeingDeserialized;
      Console.WriteLine("GroupName: {0}", myGroup.GroupName);
      Console.WriteLine();
   }
}
/* 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>
*/   
#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>
*/
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

public class Group
{
    // Only the GroupName field will be known.
    public String groupName;
} //Group

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.DeserializeObject("UnknownNodes.xml");
    } //main

    private void DeserializeObject(String filename)
    {
        XmlSerializer mySerializer = new XmlSerializer(Group.class.ToType());
        FileStream fs = new FileStream(filename, FileMode.Open);
        mySerializer.add_UnknownNode(new XmlNodeEventHandler
            (SerializerUnknownNode));
        Group myGroup = (Group)mySerializer.Deserialize(fs);
        fs.Close();
    } //DeserializeObject

    private void SerializerUnknownNode(Object sender, XmlNodeEventArgs e)
    {
        Console.WriteLine("UnknownNode Name: {0}", e.get_Name());
        Console.WriteLine("UnknownNode LocalName: {0}", e.get_LocalName());
        Console.WriteLine("UnknownNode Namespace URI: {0}",
            e.get_NamespaceURI());
        Console.WriteLine("UnknownNode Text: {0}", e.get_Text());

        XmlNodeType myNodeType = e.get_NodeType();
        Console.WriteLine("NodeType: {0}", myNodeType);

        Group myGroup = (Group)e.get_ObjectBeingDeserialized();
        Console.WriteLine("GroupName: {0}", myGroup.groupName);
        Console.WriteLine();
    } //SerializerUnknownNode
} //Test
/* 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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0에서 지원

참고 항목

참조

XmlSerializer 클래스
XmlSerializer 멤버
System.Xml.Serialization 네임스페이스
XmlAnyElementAttribute 클래스
XmlAnyAttributeAttribute 클래스
CanDeserialize
Deserialize
XmlSerializer.UnknownAttribute 이벤트
XmlAttributes 클래스

기타 리소스

XML Serialization 소개
방법: XML 스트림의 대체 요소 이름 지정
특성을 사용하여 XML Serialization 제어
XML Serialization 예
XML 스키마 정의 도구(Xsd.exe)