XmlAttributes.XmlArray 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个对象,该对象指定 XmlSerializer 串行化返回数组的公共字段或读/写属性的方式。
public:
property System::Xml::Serialization::XmlArrayAttribute ^ XmlArray { System::Xml::Serialization::XmlArrayAttribute ^ get(); void set(System::Xml::Serialization::XmlArrayAttribute ^ value); };
public System.Xml.Serialization.XmlArrayAttribute XmlArray { get; set; }
public System.Xml.Serialization.XmlArrayAttribute? XmlArray { get; set; }
member this.XmlArray : System.Xml.Serialization.XmlArrayAttribute with get, set
Public Property XmlArray As XmlArrayAttribute
属性值
一个 XmlArrayAttribute,指定 XmlSerializer 序列化如何返回数组的公共字段或读/写属性。
示例
以下示例序列化一个类,该类包含一个名为返回对象数组的字段 Members
。 用于 XmlArrayAttribute 重写字段的序列化,并将元素名称重命名为 Staff
。
#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 Member
{
public:
String^ MemberName;
};
// This is the class that will be serialized.
public ref class Group
{
public:
// This field will be overridden.
array<Member^>^Members;
};
// Return an XmlSerializer used for overriding.
XmlSerializer^ CreateOverrider()
{
// Creating XmlAttributeOverrides and XmlAttributes objects.
XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
XmlAttributes^ xAttrs = gcnew XmlAttributes;
// Add an override for the XmlArray.
XmlArrayAttribute^ xArray = gcnew XmlArrayAttribute( "Staff" );
xArray->Namespace = "http://www.cpandl.com";
xAttrs->XmlArray = xArray;
xOver->Add( Group::typeid, "Members", xAttrs );
// Create the XmlSerializer and return it.
return gcnew XmlSerializer( Group::typeid,xOver );
}
void SerializeObject( String^ filename )
{
// Create an instance of the XmlSerializer class.
XmlSerializer^ mySerializer = CreateOverrider();
// Writing the file requires a TextWriter.
TextWriter^ writer = gcnew StreamWriter( filename );
// Create an instance of the class that will be serialized.
Group^ myGroup = gcnew Group;
// Set the object properties.
Member^ m = gcnew Member;
m->MemberName = "Paul";
array<Member^>^temp = {m};
myGroup->Members = temp;
// Serialize the class, and close the TextWriter.
mySerializer->Serialize( writer, myGroup );
writer->Close();
}
void DeserializeObject( String^ filename )
{
XmlSerializer^ mySerializer = CreateOverrider();
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
Group^ myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( fs ));
System::Collections::IEnumerator^ myEnum = myGroup->Members->GetEnumerator();
while ( myEnum->MoveNext() )
{
Member^ m = safe_cast<Member^>(myEnum->Current);
Console::WriteLine( m->MemberName );
}
}
int main()
{
SerializeObject( "OverrideArray.xml" );
DeserializeObject( "OverrideArray.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class Group
{
// This field will be overridden.
public Member [] Members;
}
public class Member
{
public string MemberName;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("OverrideArray.xml");
test.DeserializeObject("OverrideArray.xml");
}
// Return an XmlSerializer used for overriding.
public XmlSerializer CreateOverrider()
{
// Creating XmlAttributeOverrides and XmlAttributes objects.
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
XmlAttributes xAttrs = new XmlAttributes();
// Add an override for the XmlArray.
XmlArrayAttribute xArray = new XmlArrayAttribute("Staff");
xArray.Namespace = "http://www.cpandl.com";
xAttrs.XmlArray = xArray;
xOver.Add(typeof(Group), "Members", xAttrs);
// Create the XmlSerializer and return it.
return new XmlSerializer(typeof(Group), xOver);
}
public void SerializeObject(string filename)
{
// Create an instance of the XmlSerializer class.
XmlSerializer mySerializer = CreateOverrider();
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Create an instance of the class that will be serialized.
Group myGroup = new Group();
// Set the object properties.
Member m = new Member();
m.MemberName = "Paul";
myGroup.Members = new Member[1] {m};
// Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup);
writer.Close();
}
public void DeserializeObject(string filename)
{
XmlSerializer mySerializer = CreateOverrider();
FileStream fs = new FileStream(filename, FileMode.Open);
Group myGroup = (Group)
mySerializer.Deserialize(fs);
foreach(Member m in myGroup.Members)
{
Console.WriteLine(m.MemberName);
}
}
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class Group
' This field will be overridden.
Public Members() As Member
End Class
Public Class Member
Public MemberName As String
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("OverrideArray.xml")
test.DeserializeObject("OverrideArray.xml")
End Sub
' Return an XmlSerializer used for overriding.
Public Function CreateOverrider() As XmlSerializer
' Creating XmlAttributeOverrides and XmlAttributes objects.
Dim xOver As New XmlAttributeOverrides()
Dim xAttrs As New XmlAttributes()
' Add an override for the XmlArray.
Dim xArray As New XmlArrayAttribute("Staff")
xArray.Namespace = "http://www.cpandl.com"
xAttrs.XmlArray = xArray
xOver.Add(GetType(Group), "Members", xAttrs)
' Create the XmlSerializer and return it.
Return New XmlSerializer(GetType(Group), xOver)
End Function
Public Sub SerializeObject(ByVal filename As String)
' Create an instance of the XmlSerializer class.
Dim mySerializer As XmlSerializer = CreateOverrider()
' Writing the file requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Create an instance of the class that will be serialized.
Dim myGroup As New Group()
' Set the object properties.
Dim m As New Member()
m.MemberName = "Paul"
myGroup.Members = New Member(0) {m}
' Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup)
writer.Close()
End Sub
Public Sub DeserializeObject(ByVal filename As String)
Dim mySerializer As XmlSerializer = CreateOverrider()
Dim fs As New FileStream(filename, FileMode.Open)
Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)
Dim m As Member
For Each m In myGroup.Members
Console.WriteLine(m.MemberName)
Next m
End Sub
End Class
注解
有两种方法,即返回数组的公共字段或公共读/写属性由 XmlSerializer:默认序列化和受控序列化进行序列化。
在默认序列化中,不会向成员应用任何属性。 序列化后,数组将序列化为 XML 元素的嵌套序列,其中包含从成员名称中获取的嵌套序列的 XML 元素名称。
若要更准确地控制序列化,请对字段或属性应用一个 XmlArrayAttribute 。 例如,可以通过将 ElementName 属性设置为新值,将生成的 XML 元素名称从默认值更改为其他名称。
该 XmlArray 属性允许重写默认序列化,以及通过向成员应用 XmlArrayAttribute 控制序列化。 例如,可以将从默认 (成员标识符) 生成的 XML 元素名称更改为新值。 此外,如果对成员应用某个XmlArrayAttribute成员,则由分配给该XmlArray属性的任何XmlArrayAttribute成员重写。