XmlAnyAttributeAttribute 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定成员(返回 XmlAttribute 对象的数组的字段)可以包含任何 XML 特性。
public ref class XmlAnyAttributeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=false)]
public class XmlAnyAttributeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class XmlAnyAttributeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=false)>]
type XmlAnyAttributeAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)>]
type XmlAnyAttributeAttribute = class
inherit Attribute
Public Class XmlAnyAttributeAttribute
Inherits Attribute
- 继承
- 属性
示例
以下示例将所有未知属性收集到 对象的数组中 XmlAttribute 。 若要尝试此示例,请创建一个名为 UnknownAttributes.xml
的文件,其中包含以下 XML:
<?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"
GroupType = 'Technical' GroupNumber = '42' GroupBase = 'Red'>
<GroupName>MyGroup</GroupName>
</Group>
#using <System.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Collections;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Xml;
public ref class Group
{
public:
String^ GroupName;
// The UnknownAttributes array will be used to collect all unknown
// attributes found when deserializing.
[XmlAnyAttributeAttribute]
array<XmlAttribute^>^XAttributes;
};
void SerializeObject( String^ filename, Object^ g )
{
XmlSerializer^ ser = gcnew XmlSerializer( Group::typeid );
TextWriter^ writer = gcnew StreamWriter( filename );
ser->Serialize( writer, g );
writer->Close();
}
void DeserializeObject( String^ filename )
{
XmlSerializer^ ser = gcnew XmlSerializer( Group::typeid );
// A FileStream is needed to read the XML document.
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
Group^ g = safe_cast<Group^>(ser->Deserialize( fs ));
fs->Close();
// Write out the data, including unknown attributes.
Console::WriteLine( g->GroupName );
Console::WriteLine( "Number of unknown attributes: {0}", g->XAttributes->Length );
for ( IEnumerator ^ e = g->XAttributes->GetEnumerator(); e->MoveNext(); )
{
XmlAttribute^ xAtt = safe_cast<XmlAttribute^>(e->Current);
Console::WriteLine( "{0}: {1}", xAtt->Name, xAtt->InnerXml );
}
SerializeObject( "AttributesAdded.xml", g );
}
int main()
{
// Deserialize the file containing unknown attributes.
DeserializeObject( "UnknownAttributes.xml" );
}
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
public class Group{
public string GroupName;
// The UnknownAttributes array will be used to collect all unknown
// attributes found when deserializing.
[XmlAnyAttribute]
public XmlAttribute[]XAttributes;
}
public class Test{
static void Main(){
Test t = new Test();
// Deserialize the file containing unknown attributes.
t.DeserializeObject("UnknownAttributes.xml");
}
private void DeserializeObject(string filename){
XmlSerializer ser = new XmlSerializer(typeof(Group));
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
Group g = (Group) ser.Deserialize(fs);
fs.Close();
// Write out the data, including unknown attributes.
Console.WriteLine(g.GroupName);
Console.WriteLine("Number of unknown attributes: " +
g.XAttributes.Length);
foreach(XmlAttribute xAtt in g.XAttributes){
Console.WriteLine(xAtt.Name + ": " + xAtt.InnerXml);
}
// Serialize the object again with the attributes added.
this.SerializeObject("AttributesAdded.xml",g);
}
private void SerializeObject(string filename, object g){
XmlSerializer ser = new XmlSerializer(typeof(Group));
TextWriter writer = new StreamWriter(filename);
ser.Serialize(writer, g);
writer.Close();
}
}
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Public Class Group
Public GroupName As String
' The UnknownAttributes array will be used to collect all unknown
' attributes found when deserializing.
<XmlAnyAttribute> _
Public UnknownAttributes()As XmlAttribute
End Class
Public Class Test
Shared Sub Main()
Dim t As Test = New Test()
' Deserialize the file containing unknown attributes.
t.DeserializeObject("UnknownAttributes.xml")
End Sub
Private Sub DeserializeObject(filename As String)
Dim ser As XmlSerializer = New XmlSerializer(GetType(Group))
' A FileStream is needed to read the XML document.
Dim fs As FileStream = New FileStream(filename, FileMode.Open)
Dim g As Group = CType(ser.Deserialize(fs), Group)
fs.Close()
' Write out the data, including unknown attributes.
Console.WriteLine(g.GroupName)
Console.WriteLine("Number of unknown attributes: " & _
g.UnknownAttributes.Length)
Dim xAtt As XmlAttribute
for each xAtt in g.UnknownAttributes
Console.WriteLine(xAtt.Name & ": " & xAtt.InnerXml)
Next
' Serialize the object again with the attributes added.
Me.SerializeObject("AttributesAdded.xml",g)
End Sub
Private Sub SerializeObject(filename As String, g As object)
Dim ser As XmlSerializer = New XmlSerializer(GetType(Group))
DIm writer As TextWriter = New StreamWriter(filename)
ser.Serialize(writer, g)
writer.Close()
End Sub
End Class
注解
XmlAnyAttributeAttribute使用 包含任意数据 (作为 XML 文档的一部分发送的 XML 属性) ,例如,作为文档的一部分发送的元数据。
将 XmlAnyAttributeAttribute 应用于返回 或 XmlNode 对象的数组的XmlAttribute字段。 Deserialize调用 类的 XmlSerializer 方法时,在被反序列化的类中没有相应成员的所有 XML 属性都会收集到 数组中。 反序列化后,可以循环访问项集合 XmlAttribute 来处理数据。
UnknownNode如果将 应用于类的成员,XmlSerializerXmlAnyAttributeAttribute则 不会发生 的 和 UnknownAttribute 事件。
注意
可以在代码中使用 单词 XmlAnyAttribute
,而不是较长 XmlAnyAttributeAttribute的 。
有关使用特性的详细信息,请参阅 特性。
构造函数
XmlAnyAttributeAttribute() |
构造 XmlAnyAttributeAttribute 类的新实例。 |
属性
TypeId |
在派生类中实现时,获取此 Attribute 的唯一标识符。 (继承自 Attribute) |
方法
Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute) |
GetHashCode() |
返回此实例的哈希代码。 (继承自 Attribute) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
IsDefaultAttribute() |
在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute) |
Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
显式接口实现
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。 (继承自 Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。 (继承自 Attribute) |