Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Fügt der Auflistung ein XmlAnyElementAttribute hinzu.
Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)
Syntax
'Declaration
Public Function Add ( _
attribute As XmlAnyElementAttribute _
) As Integer
'Usage
Dim instance As XmlAnyElementAttributes
Dim attribute As XmlAnyElementAttribute
Dim returnValue As Integer
returnValue = instance.Add(attribute)
public int Add (
XmlAnyElementAttribute attribute
)
public:
int Add (
XmlAnyElementAttribute^ attribute
)
public int Add (
XmlAnyElementAttribute attribute
)
public function Add (
attribute : XmlAnyElementAttribute
) : int
Parameter
- attribute
Das hinzuzufügende XmlAnyElementAttribute.
Rückgabewert
Der Index des neu hinzugefügten XmlAnyElementAttribute.
Beispiel
Im folgenden Beispiel wird ein neues XmlAnyElementAttribute erstellt und der Auflistung von Objekten hinzugefügt, auf die über die XmlAnyElements-Eigenschaft zugegriffen werden kann. Das XmlAttributes wird dann einem XmlAttributeOverrides hinzugefügt, mit dem ein XmlSerializer erstellt wird. Der XmlSerializer wird zum Serialisieren bzw. zum Deserialisieren eines Objekts verwendet. Um die Auswirkungen der Verwendung von XmlAnyElementAttributes anzeigen zu lassen, erstellen Sie durch Ausführen der SerializeObject-Methode in der Main-Methode ein XML-Dokument mit dem Namen UnknownElements.xml. Bearbeiten Sie das erhaltene Dokument, um weitere (unbekannte) Elemente aufnehmen zu können. Kommentieren Sie den SerializeObject-Aufruf in der Main-Methode aus, und heben Sie die Auskommentierung des Aufrufs der DeserializeObject-Methode auf, mit der der Name und der Wert aller unbekannten XML-Elemente ausgegeben wird.
Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Public Class Group
Public GroupName As String
<XmlAnyElement> _
Public Things () As object
End Class
Public Class Test
Shared Sub Main()
Dim t As Test = New Test()
' 1 Run this and create the XML document.
' 2 Add New elements to the XML document.
' 3 Comment out the New line, and uncomment
' the DeserializeObject line to deserialize the
' XML document and see unknown elements.
t.SerializeObject("UnknownElements.xml")
't.DeserializeObject("UnknownElements.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)
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 xelement As XmlELement
for each xelement in g.Things
Console.WriteLine(xelement.Name &": " & xelement.InnerXml)
next
End Sub
Private Function CreateOverrideSerializer() As XmlSerializer
Dim myAnyElement As XmlAnyElementAttribute = _
New XmlAnyElementAttribute()
Dim xOverride As XmlAttributeOverrides = _
New XmlAttributeOverrides()
Dim xAtts As XmlAttributes = New XmlAttributes()
xAtts.XmlAnyElements.Add(myAnyElement)
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;
[XmlAnyElement]
public object[]Things;
}
public class Test{
static void Main(){
Test t = new Test();
// 1 Run this and create the XML document.
// 2 Add new elements to the XML document.
// 3 Comment out the new line, and uncomment
// the DeserializeObject line to deserialize the
// XML document and see unknown elements.
t.SerializeObject("UnknownElements.xml");
// t.DeserializeObject("UnknownElements.xml");
}
private void SerializeObject(string filename){
XmlSerializer ser = new XmlSerializer(typeof (Group));
TextWriter writer = new StreamWriter(filename);
Group g = new Group();
g.GroupName = "MyGroup";
ser.Serialize(writer, g);
writer.Close();
}
private void DeserializeObject(string filename){
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(XmlElement xelement in g.Things){
Console.WriteLine(xelement.Name + ": " + xelement.InnerXml);
}
}
private XmlSerializer CreateOverrideSerializer(){
XmlAnyElementAttribute myAnyElement =
new XmlAnyElementAttribute();
XmlAttributeOverrides xOverride =
new XmlAttributeOverrides();
XmlAttributes xAtts = new XmlAttributes();
xAtts.XmlAnyElements.Add(myAnyElement);
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::IO;
using namespace System::Xml::Serialization;
using namespace System::Xml;
public ref class Group
{
public:
String^ GroupName;
[XmlAnyElement]
array<Object^>^Things;
};
void SerializeObject( String^ filename );
void DeserializeObject( String^ filename );
XmlSerializer^ CreateOverrideSerializer();
int main()
{
// 1 Run this and create the XML document.
// 2 Add new elements to the XML document.
// 3 Comment out the next line, and uncomment
// the DeserializeObject line to deserialize the
// XML document and see unknown elements.
SerializeObject( "UnknownElements.xml" );
// DeserializeObject(S"UnknownElements.xml");
}
void SerializeObject( String^ filename )
{
XmlSerializer^ ser = gcnew XmlSerializer( Group::typeid );
TextWriter^ writer = gcnew StreamWriter( filename );
Group^ g = gcnew Group;
g->GroupName = "MyGroup";
ser->Serialize( writer, g );
writer->Close();
}
void DeserializeObject( String^ filename )
{
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 ( int i = 0; i < g->Things->Length; ++i )
{
XmlElement^ xelement = safe_cast<XmlElement^>(g->Things[ i ]);
Console::WriteLine( "{0}: {1}", xelement->Name, xelement->InnerXml );
}
}
XmlSerializer^ CreateOverrideSerializer()
{
XmlAnyElementAttribute^ myAnyElement = gcnew XmlAnyElementAttribute;
XmlAttributeOverrides^ xOverride = gcnew XmlAttributeOverrides;
XmlAttributes^ xAtts = gcnew XmlAttributes;
xAtts->XmlAnyElements->Add( myAnyElement );
xOverride->Add( Group::typeid, "Things", xAtts );
return gcnew XmlSerializer( Group::typeid,xOverride );
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Xml.*;
public class Group
{
public String groupName;
/** @attribute XmlAnyElement()
*/
public Object things[];
} //Group
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
// 1 Run this and create the XML document.
// 2 Add new elements to the XML document.
// 3 Comment out the new line, and uncomment
// the DeserializeObject line to deserialize the
// XML document and see unknown elements.
t.SerializeObject("UnknownElements.xml");
} //main
// t.DeserializeObject("UnknownElements.xml");
private void SerializeObject(String fileName)
{
XmlSerializer ser = new XmlSerializer(Group.class.ToType());
TextWriter writer = new StreamWriter(fileName);
Group g = new Group();
g.groupName = "MyGroup";
ser.Serialize(writer, g);
writer.Close();
} //SerializeObject
private void DeserializeObject(String fileName)
{
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++) {
XmlElement xElement = (XmlElement)g.things[iCtr];
Console.WriteLine(xElement.get_Name() + ": "
+ xElement.get_InnerXml());
}
} //DeserializeObject
private XmlSerializer CreateOverrideSerializer()
{
XmlAnyElementAttribute myAnyElement = new XmlAnyElementAttribute();
XmlAttributeOverrides xOverride = new XmlAttributeOverrides();
XmlAttributes xAtts = new XmlAttributes();
xAtts.get_XmlAnyElements().Add(myAnyElement);
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
Siehe auch
Referenz
XmlAnyElementAttributes-Klasse
XmlAnyElementAttributes-Member
System.Xml.Serialization-Namespace