XmlAnyAttributeAttribute-Konstruktor
Erstellt eine neue Instanz der XmlAnyAttributeAttribute-Klasse.
Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)
Syntax
'Declaration
Public Sub New
'Usage
Dim instance As New XmlAnyAttributeAttribute
public XmlAnyAttributeAttribute ()
public:
XmlAnyAttributeAttribute ()
public XmlAnyAttributeAttribute ()
public function XmlAnyAttributeAttribute ()
Beispiel
Im folgenden Beispiel wird ein XmlAnyAttributeAttribute erstellt, das zum Überschreiben der Deserialisierung eines Objekts verwendet wird. Zum Ausführen des Beispiels erstellen Sie eine Datei mit dem Namen UnknownAttributes.xml, die folgenden XML-Code enthält:
<?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>
Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Public Class Group
' The Things array will be used to collect all unknown
' attributes found when deserializing.
public GroupName As String
public Things() As XmlAttribute
End Class
Public Class Test
Shared Sub Main()
Dim t As Test = New Test()
t.DeserializeObject("UnknownAttributes.xml")
End Sub
Private Sub SerializeObject(filename As String)
Dim ser As XmlSerializer = New XmlSerializer(GetType (Group))
Dim writer As TextWriter = New StreamWriter(filename)
Dim g As Group = New Group()
g.GroupName = "MyGroup"
ser.Serialize(writer, g)
writer.Close()
End Sub
Private Sub DeserializeObject(filename As String)
' Use the CreateOverrideSerializer to return an instance
' of the XmlSerializer customized for overrides.
Dim ser As XmlSerializer = CreateOverrideSerializer()
' 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()
Console.WriteLine(g.GroupName)
Console.WriteLine(g.Things.Length)
Dim xAtt As XmlAttribute
for each xAtt in g.Things
Console.WriteLine(xAtt.Name & ": " & xAtt.InnerXml)
Next
End Sub
Private Function CreateOverrideSerializer() As XmlSerializer
' Override the Things field to capture all
' unknown XML attributes.
Dim myAnyAttribute As XmlAnyAttributeAttribute = _
New XmlAnyAttributeAttribute()
Dim xOverride As XmlAttributeOverrides = _
New XmlAttributeOverrides()
Dim xAtts As XmlAttributes = New XmlAttributes()
xAtts.XmlAnyAttribute=myAnyAttribute
xOverride.Add(GetType(Group), "Things", xAtts)
return New XmlSerializer(GetType(Group) , xOverride)
End Function
End Class
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
public class Group{
public string GroupName;
// The Things array will be used to collect all unknown
// attributes found when deserializing.
public XmlAttribute[]Things;
}
public class Test{
static void Main(){
Test t = new Test();
t.DeserializeObject("UnknownAttributes.xml");
}
private void DeserializeObject(string filename){
// Use the CreateOverrideSerializer to return an instance
// of the XmlSerializer customized for overrides.
XmlSerializer ser = CreateOverrideSerializer();
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
Group g = (Group)
ser.Deserialize(fs);
fs.Close();
Console.WriteLine(g.GroupName);
Console.WriteLine(g.Things.Length);
foreach(XmlAttribute xAtt in g.Things){
Console.WriteLine(xAtt.Name + ": " + xAtt.InnerXml);
}
}
private XmlSerializer CreateOverrideSerializer(){
// Override the Things field to capture all
// unknown XML attributes.
XmlAnyAttributeAttribute myAnyAttribute =
new XmlAnyAttributeAttribute();
XmlAttributeOverrides xOverride =
new XmlAttributeOverrides();
XmlAttributes xAtts = new XmlAttributes();
xAtts.XmlAnyAttribute=myAnyAttribute;
xOverride.Add(typeof(Group), "Things", xAtts);
return new XmlSerializer(typeof(Group) , xOverride);
}
}
#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 Things array will be used to collect all unknown
// attributes found when deserializing.
array<XmlAttribute^>^Things;
};
XmlSerializer^ CreateOverrideSerializer();
void DeserializeObject( String^ filename )
{
// Use the CreateOverrideSerializer to return an instance
// of the XmlSerializer customized for overrides.
XmlSerializer^ ser = CreateOverrideSerializer();
// 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();
Console::WriteLine( g->GroupName );
Console::WriteLine( g->Things->Length );
for ( IEnumerator ^ e = g->Things->GetEnumerator(); e->MoveNext(); )
{
XmlAttribute^ xAtt = safe_cast<XmlAttribute^>(e->Current);
Console::WriteLine( "{0}: {1}", xAtt->Name, xAtt->InnerXml );
}
}
XmlSerializer^ CreateOverrideSerializer()
{
// Override the Things field to capture all
// unknown XML attributes.
XmlAnyAttributeAttribute^ myAnyAttribute = gcnew XmlAnyAttributeAttribute;
XmlAttributeOverrides^ xOverride = gcnew XmlAttributeOverrides;
XmlAttributes^ xAtts = gcnew XmlAttributes;
xAtts->XmlAnyAttribute = myAnyAttribute;
xOverride->Add( Group::typeid, "Things", xAtts );
return gcnew XmlSerializer( Group::typeid,xOverride );
}
int main()
{
DeserializeObject( "UnknownAttributes.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Xml.*;
public class Group
{
public String groupName;
// The things array will be used to collect all unknown
// attributes found when deserializing.
public XmlAttribute things[];
} //Group
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
t.DeserializeObject("UnknownAttributes.xml");
} //main
private void DeserializeObject(String filename)
{
// Use the CreateOverrideSerializer to return an instance
// of the XmlSerializer customized for overrides.
XmlSerializer ser = CreateOverrideSerializer();
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
Group g = (Group)ser.Deserialize(fs);
fs.Close();
Console.WriteLine(g.groupName);
Console.WriteLine(g.things.get_Length());
for (int iCtr = 0; iCtr < g.things.get_Count(); iCtr++) {
XmlAttribute xAtt = (XmlAttribute)g.things.get_Item(iCtr);
Console.WriteLine(xAtt.get_Name() + ": " + xAtt.get_InnerXml());
}
} //DeserializeObject
private XmlSerializer CreateOverrideSerializer()
{
// Override the things field to capture all
// unknown XML attributes.
XmlAnyAttributeAttribute myAnyAttribute =
new XmlAnyAttributeAttribute();
XmlAttributeOverrides xOverride = new XmlAttributeOverrides();
XmlAttributes xAtts = new XmlAttributes();
xAtts.set_XmlAnyAttribute(myAnyAttribute);
xOverride.Add(Group.class.ToType(), "things", xAtts);
return new XmlSerializer(Group.class.ToType(), xOverride);
} //CreateOverrideSerializer
} //Test
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
XmlAnyAttributeAttribute-Klasse
XmlAnyAttributeAttribute-Member
System.Xml.Serialization-Namespace