共用方式為


XmlNodeEventArgs 類別

定義

提供 UnknownNode 事件的資料。

public ref class XmlNodeEventArgs : EventArgs
public class XmlNodeEventArgs : EventArgs
type XmlNodeEventArgs = class
    inherit EventArgs
Public Class XmlNodeEventArgs
Inherits EventArgs
繼承
XmlNodeEventArgs

範例

下列範例會 UnknownNode 使用 的 XmlSerializer 事件來列印呼叫 XmlSerializer 類別 Deserialize 方法時所遇到的未知 XML 節點的各種屬性。

#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>
*/
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>
*/
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>
'         <Glue>element</Glue>
'  </coho:ThingInfo>
'/Group>

備註

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

只有在 UnknownNode 您呼叫 Deserialize 方法時,才會發生此事件。

屬性

LineNumber

取得未知 XML 節點的行號。

LinePosition

取得未知 XML 節點在行中的位置。

LocalName

取得要還原序列化的 XML 節點的 XML 區域名稱。

Name

取得要還原序列化的 XML 節點名稱。

NamespaceURI

取得已與要還原序列化之 XML 節點建立關聯的命名空間 URI。

NodeType

取得要還原序列化的 XML 節點類型。

ObjectBeingDeserialized

取得要還原序列化的物件。

Text

取得要還原序列化的 XML 節點文字。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱