XmlAttributeOverrides.Item[] 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取表示替代属性的集合的对象。
重载
Item[Type] |
获取与指定的基类类型相关联的对象。 |
Item[Type, String] |
获取与指定(基类)类型相关联的对象。 成员参数指定被替代的基类成员。 |
Item[Type]
- Source:
- XmlAttributeOverrides.cs
- Source:
- XmlAttributeOverrides.cs
- Source:
- XmlAttributeOverrides.cs
获取与指定的基类类型相关联的对象。
public:
property System::Xml::Serialization::XmlAttributes ^ default[Type ^] { System::Xml::Serialization::XmlAttributes ^ get(Type ^ type); };
public System.Xml.Serialization.XmlAttributes this[Type type] { get; }
public System.Xml.Serialization.XmlAttributes? this[Type type] { get; }
member this.Item(Type) : System.Xml.Serialization.XmlAttributes
Default Public ReadOnly Property Item(type As Type) As XmlAttributes
参数
属性值
表示重写属性集合的 XmlAttributes。
示例
以下示例创建 XmlAttributeOverrides 对象、 XmlAttributes 对象和 XmlRootAttribute 对象。 该示例将 分配给 XmlRootAttributeXmlRoot 对象的 属性 XmlAttributes ,并将 对象 XmlAttributes 添加到 对象 XmlAttributeOverrides 。 最后,该示例通过将序列化类的 传递给 Type 对象来XmlAttributeOverrides获取 XmlAttributes 对象。 在此示例中, Type 为 Group
。
// This is the class that will be serialized.
public ref class Group
{
public:
String^ GroupName;
[XmlAttributeAttribute]
int GroupCode;
};
public ref class Sample
{
public:
XmlSerializer^ CreateOverrider()
{
// Create an XmlSerializer with overriding attributes.
XmlAttributes^ attrs = gcnew XmlAttributes;
XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
XmlRootAttribute^ xRoot = gcnew XmlRootAttribute;
// Set a new Namespace and ElementName for the root element.
xRoot->Namespace = "http://www.cpandl.com";
xRoot->ElementName = "NewGroup";
attrs->XmlRoot = xRoot;
xOver->Add( Group::typeid, attrs );
// Get the XmlAttributes object, based on the type.
XmlAttributes^ tempAttrs;
tempAttrs = xOver[ Group::typeid ];
// Print the Namespace and ElementName of the root.
Console::WriteLine( tempAttrs->XmlRoot->Namespace );
Console::WriteLine( tempAttrs->XmlRoot->ElementName );
XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid,xOver );
return xSer;
}
};
// This is the class that will be serialized.
public class Group
{
public string GroupName;
[XmlAttribute]
public int GroupCode;
}
public class Sample
{
public XmlSerializer CreateOverrider()
{
// Create an XmlSerializer with overriding attributes.
XmlAttributes attrs = new XmlAttributes();
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
XmlRootAttribute xRoot = new XmlRootAttribute();
// Set a new Namespace and ElementName for the root element.
xRoot.Namespace = "http://www.cpandl.com";
xRoot.ElementName = "NewGroup";
attrs.XmlRoot = xRoot;
xOver.Add(typeof(Group), attrs);
// Get the XmlAttributes object, based on the type.
XmlAttributes tempAttrs;
tempAttrs = xOver[typeof(Group)];
// Print the Namespace and ElementName of the root.
Console.WriteLine(tempAttrs.XmlRoot.Namespace);
Console.WriteLine(tempAttrs.XmlRoot.ElementName);
XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
return xSer;
}
}
' This is the class that will be serialized.
Public Class Group
Public GroupName As String
<XmlAttribute()> Public GroupCode As Integer
End Class
Public Class Sample
Public Function CreateOverrider() As XmlSerializer
' Create an XmlSerializer with overriding attributes.
Dim attrs As New XmlAttributes()
Dim xOver As New XmlAttributeOverrides()
Dim xRoot As New XmlRootAttribute()
' Set a new Namespace and ElementName for the root element.
xRoot.Namespace = "http://www.cpandl.com"
xRoot.ElementName = "NewGroup"
attrs.XmlRoot = xRoot
xOver.Add(GetType(Group), attrs)
' Get the XmlAttributes object, based on the type.
Dim tempAttrs As XmlAttributes
tempAttrs = xOver(GetType(Group))
' Print the Namespace and ElementName of the root.
Console.WriteLine(tempAttrs.XmlRoot.Namespace)
Console.WriteLine(tempAttrs.XmlRoot.ElementName)
Dim xSer As New XmlSerializer(GetType(Group), xOver)
Return xSer
End Function
End Class
注解
使用此重载可返回一个XmlAttributes对象,该对象包含 或 XmlTypeAttribute 对象的属性XmlRootAttribute。
XmlAttributes如果 对象包含替代 XmlArrayAttribute、、XmlArrayItemAttributeXmlElementAttribute、 XmlEnumAttribute或 XmlAttributeAttribute的对象,则必须使用指定重写成员和类型的重载。
另请参阅
适用于
Item[Type, String]
- Source:
- XmlAttributeOverrides.cs
- Source:
- XmlAttributeOverrides.cs
- Source:
- XmlAttributeOverrides.cs
获取与指定(基类)类型相关联的对象。 成员参数指定被替代的基类成员。
public:
property System::Xml::Serialization::XmlAttributes ^ default[Type ^, System::String ^] { System::Xml::Serialization::XmlAttributes ^ get(Type ^ type, System::String ^ member); };
public System.Xml.Serialization.XmlAttributes this[Type type, string member] { get; }
public System.Xml.Serialization.XmlAttributes? this[Type type, string member] { get; }
member this.Item(Type * string) : System.Xml.Serialization.XmlAttributes
Default Public ReadOnly Property Item(type As Type, member As String) As XmlAttributes
参数
- member
- String
指定要返回的 XmlAttributes 的替代成员名称。
属性值
表示重写属性集合的 XmlAttributes。
示例
以下示例创建 一个 XmlAttributeOverrides 对象、一个 XmlAttributes和 一个 XmlAttributeAttribute 对象。 该示例将 分配给 XmlAttributeAttributeXmlAttribute 对象的 属性 XmlAttributes ,并将 对象 XmlAttributes 添加到 对象 XmlAttributeOverrides 。 最后,该示例通过将序列化类和成员名称的 传递给 Type 对象来XmlAttributeOverrides获取 XmlAttributes 对象。
// This is the class that will be serialized.
public ref class Group
{
public:
String^ GroupName;
[XmlAttributeAttribute]
int GroupCode;
};
public ref class Sample
{
public:
XmlSerializer^ CreateOverrider()
{
// Create an XmlSerializer with overriding attributes.
XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
/* Create an XmlAttributeAttribute object and set the
AttributeName property. */
XmlAttributeAttribute^ xAtt = gcnew XmlAttributeAttribute;
xAtt->AttributeName = "Code";
/* Create a new XmlAttributes object and set the
XmlAttributeAttribute object to the XmlAttribute property. */
XmlAttributes^ attrs = gcnew XmlAttributes;
attrs->XmlAttribute = xAtt;
/* Add the XmlAttributes to the XmlAttributeOverrides object. The
name of the overridden attribute must be specified. */
xOver->Add( Group::typeid, "GroupCode", attrs );
// Get the XmlAttributes object for the type and member.
XmlAttributes^ tempAttrs;
tempAttrs = xOver[Group::typeid, "GroupCode"];
Console::WriteLine( tempAttrs->XmlAttribute->AttributeName );
// Create the XmlSerializer instance and return it.
XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid,xOver );
return xSer;
}
};
// This is the class that will be serialized.
public class Group
{
public string GroupName;
[XmlAttribute]
public int GroupCode;
}
public class Sample
{
public XmlSerializer CreateOverrider()
{
// Create an XmlSerializer with overriding attributes.
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
/* Create an XmlAttributeAttribute object and set the
AttributeName property. */
XmlAttributeAttribute xAtt = new XmlAttributeAttribute();
xAtt.AttributeName = "Code";
/* Create a new XmlAttributes object and set the
XmlAttributeAttribute object to the XmlAttribute property. */
XmlAttributes attrs = new XmlAttributes();
attrs.XmlAttribute = xAtt;
/* Add the XmlAttributes to the XmlAttributeOverrides object. The
name of the overridden attribute must be specified. */
xOver.Add(typeof(Group), "GroupCode", attrs);
// Get the XmlAttributes object for the type and member.
XmlAttributes tempAttrs;
tempAttrs = xOver[typeof(Group), "GroupCode"];
Console.WriteLine(tempAttrs.XmlAttribute.AttributeName);
// Create the XmlSerializer instance and return it.
XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
return xSer;
}
}
' This is the class that will be serialized.
Public Class Group
Public GroupName As String
<XmlAttribute()> Public GroupCode As Integer
End Class
Public Class Sample
Public Function CreateOverrider() As XmlSerializer
' Create an XmlSerializer with overriding attributes.
Dim xOver As New XmlAttributeOverrides()
' Create an XmlAttributeAttribute object and set the
' AttributeName property.
Dim xAtt As New XmlAttributeAttribute()
xAtt.AttributeName = "Code"
' Create a new XmlAttributes object and set the
' XmlAttributeAttribute object to the XmlAttribute property.
Dim attrs As New XmlAttributes()
attrs.XmlAttribute = xAtt
' Add the XmlAttributes to the XmlAttributeOverrides object. The
' name of the overridden attribute must be specified.
xOver.Add(GetType(Group), "GroupCode", attrs)
' Get the XmlAttributes object for the type and member.
Dim tempAttrs As XmlAttributes
tempAttrs = xOver(GetType(Group), "GroupCode")
Console.WriteLine(tempAttrs.XmlAttribute.AttributeName)
' Create the XmlSerializer instance and return it.
Dim xSer As New XmlSerializer(GetType(Group), xOver)
Return xSer
End Function
End Class
注解
使用此重载可返回一个 XmlAttributes 对象,该对象包含重写 XmlArrayAttribute、、XmlArrayItemAttributeXmlAttributeAttribute、 XmlElementAttribute或 XmlEnumAttribute的对象。 XmlAttributes如果 对象包含 XmlRootAttribute 或 XmlTypeAttribute,则必须使用仅指定重写类型的重载。