XmlAnyElementAttribute.Name 属性

定义

获取或设置 XML 元素名。

public:
 property System::String ^ Name { System::String ^ get(); void set(System::String ^ value); };
public string Name { get; set; }
member this.Name : string with get, set
Public Property Name As String

属性值

XML 元素的名称。

例外

数组成员的元素名称与 Name 属性指定的元素名称不匹配。

示例

#using <system.dll>
#using <system.xml.dll>

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

[XmlRoot(Namespace="http://www.cohowinery.com")]
public ref class Group
{
public:
   String^ GroupName;

   // This is for serializing Employee elements.

   [XmlAnyElement(Name="Employee")]
   array<XmlElement^>^UnknownEmployees;

   // This is for serializing City elements.   

   [XmlAnyElement
   (Name="City",
   Namespace="http://www.cpandl.com")]
   array<XmlElement^>^UnknownCity;

   // This one is for all other unknown elements.

   [XmlAnyElement]
   array<XmlElement^>^UnknownElements;
};

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

   // Create an XmlNamespaces to use.
   XmlSerializerNamespaces^ namespaces = gcnew XmlSerializerNamespaces;
   namespaces->Add( "c", "http://www.cohowinery.com" );
   namespaces->Add( "i", "http://www.cpandl.com" );
   Group^ myGroup = gcnew Group;

   // Create arrays of arbitrary XmlElement objects.
   // First create an XmlDocument, used to create the 
   // XmlElement objects.
   XmlDocument^ xDoc = gcnew XmlDocument;

   // Create an array of Employee XmlElement objects.
   XmlElement^ El1 = xDoc->CreateElement( "Employee", "http://www.cohowinery.com" );
   El1->InnerText = "John";
   XmlElement^ El2 = xDoc->CreateElement( "Employee", "http://www.cohowinery.com" );
   El2->InnerText = "Joan";
   XmlElement^ El3 = xDoc->CreateElement( "Employee", "http://www.cohowinery.com" );
   El3->InnerText = "Jim";
   array<XmlElement^>^employees = {El1,El2,El3};
   myGroup->UnknownEmployees = employees;

   // Create an array of City XmlElement objects.
   XmlElement^ inf1 = xDoc->CreateElement( "City", "http://www.cpandl.com" );
   inf1->InnerText = "Tokyo";
   XmlElement^ inf2 = xDoc->CreateElement( "City", "http://www.cpandl.com" );
   inf2->InnerText = "New York";
   XmlElement^ inf3 = xDoc->CreateElement( "City", "http://www.cpandl.com" );
   inf3->InnerText = "Rome";
   array<XmlElement^>^cities = {inf1,inf2,inf3};
   myGroup->UnknownCity = cities;
   XmlElement^ xEl1 = xDoc->CreateElement( "bld" );
   xEl1->InnerText = "42";
   XmlElement^ xEl2 = xDoc->CreateElement( "Region" );
   xEl2->InnerText = "West";
   XmlElement^ xEl3 = xDoc->CreateElement( "type" );
   xEl3->InnerText = "Technical";
   array<XmlElement^>^elements = {xEl1,xEl2,xEl3};
   myGroup->UnknownElements = elements;

   // Serialize the class, and close the TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );
   ser->Serialize( writer, myGroup, namespaces );
   writer->Close();
}

void DeserializeObject( String^ filename )
{
   XmlSerializer^ ser = gcnew XmlSerializer( Group::typeid );
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Group^ myGroup;
   myGroup = safe_cast<Group^>(ser->Deserialize( fs ));
   fs->Close();
   for ( int i = 0; i < myGroup->UnknownEmployees->Length; ++i )
   {
      XmlElement^ xEmp = myGroup->UnknownEmployees[ i ];
      Console::WriteLine( "{0}: {1}", xEmp->LocalName, xEmp->InnerText );
   }
   for ( int i = 0; i < myGroup->UnknownCity->Length; ++i )
   {
      XmlElement^ xCity = myGroup->UnknownCity[ i ];
      Console::WriteLine( "{0}: {1}", xCity->LocalName, xCity->InnerText );
   }
   for ( int i = 0; i < myGroup->UnknownElements->Length; ++i )
   {
      XmlElement^ xEmp = myGroup->UnknownElements[ i ];
      Console::WriteLine( "{0}: {1}", xEmp->LocalName, xEmp->InnerText );
   }
}

int main()
{
   SerializeObject(  "AnyElementArray.xml" );
   DeserializeObject(  "AnyElementArray.xml" );
   Console::WriteLine(  "Done" );
}
using System;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;

[XmlRoot(Namespace = "http://www.cohowinery.com")]
public class Group{
   public string GroupName;

   // This is for serializing Employee elements.
   [XmlAnyElement(Name = "Employee")]
   public XmlElement[] UnknownEmployees;

   // This is for serializing City elements.   
   [XmlAnyElement
   (Name = "City", 
   Namespace = "http://www.cpandl.com")]
   public XmlElement[] UnknownCity;

    // This one is for all other unknown elements.
   [XmlAnyElement]
   public XmlElement[] UnknownElements;
}

public class Test{
   static void Main(){
      Test t = new Test();
      t.SerializeObject("AnyElementArray.xml");
      t.DeserializeObject("AnyElementArray.xml");
      Console.WriteLine("Done");
   }

   private void SerializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      // Create an XmlNamespaces to use.
      XmlSerializerNamespaces namespaces =
      new XmlSerializerNamespaces();
      namespaces.Add("c", "http://www.cohowinery.com");
      namespaces.Add("i", "http://www.cpandl.com");
      Group myGroup = new Group();
      // Create arrays of arbitrary XmlElement objects.
      // First create an XmlDocument, used to create the 
      // XmlElement objects.
      XmlDocument xDoc = new XmlDocument();

      // Create an array of Employee XmlElement objects.
      XmlElement El1 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El1.InnerText = "John";
      XmlElement El2 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El2.InnerText = "Joan";
      XmlElement El3 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El3.InnerText = "Jim";
      myGroup.UnknownEmployees= new XmlElement[]{El1, El2, El3};     
    
      // Create an array of City XmlElement objects.
      XmlElement inf1 = xDoc.CreateElement("City", "http://www.cpandl.com");
      inf1.InnerText = "Tokyo";
      XmlElement inf2 = xDoc.CreateElement("City", "http://www.cpandl.com");     
      inf2.InnerText = "New York";
      XmlElement inf3 = xDoc.CreateElement("City", "http://www.cpandl.com");     
      inf3.InnerText = "Rome";

      myGroup.UnknownCity = new XmlElement[]{inf1, inf2, inf3};

      XmlElement xEl1 = xDoc.CreateElement("bld");
      xEl1.InnerText = "42";
      XmlElement xEl2 = xDoc.CreateElement("Region");
      xEl2.InnerText = "West";
      XmlElement xEl3 = xDoc.CreateElement("type");
      xEl3.InnerText = "Technical";
      myGroup.UnknownElements = 
        new XmlElement[]{xEl1,xEl2,xEl3};
      // Serialize the class, and close the TextWriter.
      TextWriter writer = new StreamWriter(filename);
      ser.Serialize(writer, myGroup, namespaces);
      writer.Close();
   }

   private void DeserializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup;
      myGroup = (Group)ser.Deserialize(fs);
      fs.Close();
      foreach(XmlElement xEmp in myGroup.UnknownEmployees){
         Console.WriteLine(xEmp.LocalName + ": " + xEmp.InnerText);}
      foreach(XmlElement xCity in myGroup.UnknownCity){
         Console.WriteLine(xCity.LocalName + ": " + xCity.InnerText);}
      foreach(XmlElement xEl in myGroup.UnknownElements){
         Console.WriteLine(xEl.LocalName + ": " + xEl.InnerText);}
   }
 }
Imports System.Text
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Xml.Schema


<XmlRoot(Namespace:= "http://www.cohowinery.com")> _
Public Class Group
   Public GroupName As String 

   ' This is for serializing Employee elements.
   <XmlAnyElement(Name:="Employee")> _
   Public  UnknownEmployees() As XmlElement

   ' This is for serializing City elements.   
   <XmlAnyElement _
   (Name:="City", _
   Namespace:="http://www.cpandl.com")> _
   Public UnknownCity() As XmlElement 

    ' This one is for all other unknown elements.
   <XmlAnyElement> _
   Public UnknownElements() As XmlElement 
End Class

Public Class Test
   Shared Sub Main()
      Dim t  As Test = New Test()
      t.SerializeObject("AnyElementArray.xml")
      t.DeserializeObject("AnyElementArray.xml")
      Console.WriteLine("Done")
   End Sub

   Private Sub SerializeObject(filename As String)
      Dim ser As XmlSerializer  = _
      New XmlSerializer(GetType(Group))
      ' Create an XmlNamespaces to use.
      Dim namespaces As XmlSerializerNamespaces = _
      New XmlSerializerNamespaces()
      namespaces.Add("c", "http://www.cohowinery.com")
      namespaces.Add("i", "http://www.cpandl.com")
      Dim myGroup As Group = New Group()
      ' Create arrays of arbitrary XmlElement objects.
      ' First create an XmlDocument, used to create the 
      ' XmlElement objects.
      Dim xDoc As XmlDocument = New XmlDocument()

      ' Create an array of Employee XmlElement objects.
      Dim El1 As XmlElement = xDoc.CreateElement("Employee", "http://www.cohowinery.com")
      El1.InnerText = "John"
      Dim El2 As XmlElement = xDoc.CreateElement("Employee", "http://www.cohowinery.com")
      El2.InnerText = "Joan"
      Dim El3 As XmlElement = xDoc.CreateElement("Employee", "http://www.cohowinery.com")
      El3.InnerText = "Jim"
      myGroup.UnknownEmployees= New XmlElement(){El1, El2, El3}     
    
      ' Create an array of City XmlElement objects.
      Dim inf1 As XmlElement = xDoc.CreateElement("City", "http://www.cpandl.com")
      inf1.InnerText = "Tokyo"
      Dim inf2 As XmlElement = xDoc.CreateElement("City", "http://www.cpandl.com")     
      inf2.InnerText = "New York"
      Dim inf3 As XmlElement = xDoc.CreateElement("City", "http://www.cpandl.com")     
      inf3.InnerText = "Rome"

      myGroup.UnknownCity = New XmlElement(){inf1, inf2, inf3}

      Dim xEl1 As XmlElement = xDoc.CreateElement("bld")
      xEl1.InnerText = "42"
      Dim xEl2 As XmlElement = xDoc.CreateElement("Region")
      xEl2.InnerText = "West"
      Dim xEl3 As XmlElement = xDoc.CreateElement("type")
      xEl3.InnerText = "Technical"
      myGroup.UnknownElements = _
      New XmlElement(){xEl1,xEl2,xEl3}
      ' Serialize the class, and close the TextWriter.
      Dim writer As TextWriter = New StreamWriter(filename)
      ser.Serialize(writer, myGroup, namespaces)
      writer.Close()

   End Sub

   Private Sub DeserializeObject(filename As String)
      Dim ser As XmlSerializer = _
      New XmlSerializer(GetType(Group))
      Dim fs As FileStream = New FileStream(filename, FileMode.Open)
      Dim myGroup As Group 
      myGroup = CType(ser.Deserialize(fs), Group)
      fs.Close()
      Dim xEmp As XmlElement
      for each xEmp in myGroup.UnknownEmployees
         Console.WriteLine(xEmp.LocalName & ": " & xEmp.InnerText)
      Next 
      
      Dim xCity As XmlElement
      for each xCity in myGroup.UnknownCity
         Console.WriteLine(xCity.LocalName & ": " & xCity.InnerText)
      Next
      
      Dim xEl As XmlElement
      for each  xEl in myGroup.UnknownElements
         Console.WriteLine(xEl.LocalName & ": " & xEl.InnerText)
      Next
   End Sub
 End Class

注解

如果在应用特性时指定 Name 属性值,则插入到数组中的所有 XmlElementXmlNode 对象必须具有相同的元素名称和默认命名空间,否则将引发异常。 如果设置 Namespace 属性值,还必须设置 Name 属性,并且 XmlElementXmlNode 对象还必须具有相同的名称和命名空间值。 如果未指定任何 Name 值,则 XmlElementXmlNode 对象可以具有任何元素名称。

调用 Deserialize 类的 XmlSerializer 方法时,所有在反序列化的对象中没有对应成员的属性都会收集到 数组中。 如果指定值 Name ,则数组仅包含具有该名称的 XML 元素。 如果未指定 Name 值,则数组包含类中没有相应成员的所有元素。 如果类包含多个应用了特性的字段,请使用 NameNamespace 属性来区分数组的内容。 如果此类 (具有多个字段) 还包含一个没有设置 (的区分属性值的字段, Name 并且 Namespace) 反序列化期间,数组包含其他数组中尚未包含的任何 XML 元素。 如果添加的多个字段未设置区分 NameNamespace 值,类中的最后一个字段将包含其他数组中尚未包含的所有未知元素,并且任何其他字段都设置为 null

可以将 的 XmlAnyElementAttribute 多个实例应用于一个类成员,但每个实例必须具有不同的 Name 属性值。 或者,如果为每个实例设置了相同的 Name 属性,则必须为每个实例设置不同的 Namespace 属性值。

适用于