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.
Ruft einen Wert ab, der angibt, ob alle Namespacedeklarationen beibehalten werden sollen, wenn ein Objekt überschrieben wird, das einen Member enthält, der ein XmlSerializerNamespaces-Objekt zurückgibt, oder legt diesen fest.
Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)
Syntax
'Declaration
Public Property Xmlns As Boolean
'Usage
Dim instance As XmlAttributes
Dim value As Boolean
value = instance.Xmlns
instance.Xmlns = value
public bool Xmlns { get; set; }
public:
property bool Xmlns {
bool get ();
void set (bool value);
}
/** @property */
public boolean get_Xmlns ()
/** @property */
public void set_Xmlns (boolean value)
public function get Xmlns () : boolean
public function set Xmlns (value : boolean)
Eigenschaftenwert
true, wenn die Namespacedeklarationen beibehalten werden sollen, andernfalls false.
Beispiel
Das folgende Beispiel enthält die Klasse Student. Die Klasse enthält den Member MyNamespaces, der ein XmlSerializerNamespaces-Objekt zurückgibt. In diesem Beispiel wird ein XmlAttributes-Objekt erstellt, das einer Instanz der XmlAttributeOverrides-Klasse hinzugefügt wird. Die Xmlns-Eigenschaft wird auf true festgelegt. Damit wird XmlSerializer angewiesen, den Namespace beizubehalten, wenn die Serialisierung des Student-Objekts überschrieben wird.
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Public Class Student
<XmlAttributeAttribute()> _
Public Name As String
<XmlNamespaceDeclarationsAttribute()> _
Public myNamespaces As XmlSerializerNamespaces
End Class 'Student
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeStudent("Student_v.xml")
test.DeserializeStudent("Student_v.xml")
End Sub
Public Sub SerializeStudent(filename As String)
Dim atts As New XmlAttributes()
' Set to true to preserve namespaces, or false to ignore them.
atts.Xmlns = True
Dim xover As New XmlAttributeOverrides()
' Add the XmlAttributes and specify the name of
' the element containing namespaces.
xover.Add(GetType(Student), "myNamespaces", atts)
' Create the XmlSerializer using the
' XmlAttributeOverrides object.
Dim xser As New XmlSerializer(GetType(Student), xover)
Dim myStudent As New Student()
Dim ns As New XmlSerializerNamespaces()
ns.Add("myns1", "http://www.cpandl.com")
ns.Add("myns2", "http://www.cohowinery.com")
myStudent.myNamespaces = ns
myStudent.Name = "Student1"
Dim fs As New FileStream(filename, FileMode.Create)
xser.Serialize(fs, myStudent)
fs.Close()
End Sub
Private Sub DeserializeStudent(filename As String)
Dim atts As New XmlAttributes()
' Set to true to preserve namespaces, or false to ignore them.
atts.Xmlns = True
Dim xover As New XmlAttributeOverrides()
' Add the XmlAttributes and specify the name
' of the element containing namespaces.
xover.Add(GetType(Student), "myNamespaces", atts)
' Create the XmlSerializer using the
' XmlAttributeOverrides object.
Dim xser As New XmlSerializer(GetType(Student), xover)
Dim fs As New FileStream(filename, FileMode.Open)
Dim myStudent As Student
myStudent = CType(xser.Deserialize(fs), Student)
fs.Close()
' Use the ToArray method to get an array
' of XmlQualifiedName objects.
Dim qNames As XmlQualifiedName() = _
myStudent.myNamespaces.ToArray()
Dim i As Integer
For i = 0 To qNames.Length - 1
Console.WriteLine("{0}:{1}", _
qNames(i).Name, qNames(i).Namespace)
Next i
End Sub
End Class
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Student
{
[XmlAttributeAttribute]
public string Name;
[XmlNamespaceDeclarationsAttribute]
public XmlSerializerNamespaces myNamespaces;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeStudent("Student.xml");
test.DeserializeStudent("Student.xml");
}
public void SerializeStudent(string filename)
{
XmlAttributes atts = new XmlAttributes();
// Set to true to preserve namespaces,
// or false to ignore them.
atts.Xmlns=true;
XmlAttributeOverrides xover = new XmlAttributeOverrides();
// Add the XmlAttributes and specify the name of the element
// containing namespaces.
xover.Add(typeof(Student),"myNamespaces", atts);
// Create the XmlSerializer using the
// XmlAttributeOverrides object.
XmlSerializer xser = new XmlSerializer(typeof (Student),xover);
Student myStudent = new Student();
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("myns1", "http://www.cpandl.com");
ns.Add("myns2", "http://www.cohowinery.com");
myStudent.myNamespaces= ns;
myStudent.Name= "Student1";
FileStream fs = new FileStream(filename,FileMode.Create);
xser.Serialize(fs,myStudent);
fs.Close();
}
private void DeserializeStudent(string filename)
{
XmlAttributes atts = new XmlAttributes();
// Set to true to preserve namespaces, or false to ignore them.
atts.Xmlns=true;
XmlAttributeOverrides xover = new XmlAttributeOverrides();
// Add the XmlAttributes and specify the name of the
// element containing namespaces.
xover.Add(typeof(Student),"myNamespaces", atts);
// Create the XmlSerializer using the
// XmlAttributeOverrides object.
XmlSerializer xser =
new XmlSerializer(typeof (Student),xover);
FileStream fs = new FileStream(filename,FileMode.Open);
Student myStudent;
myStudent= (Student) xser.Deserialize(fs);
fs.Close();
// Use the ToArray method to get an array of
// XmlQualifiedName objects.
XmlQualifiedName[] qNames= myStudent.myNamespaces.ToArray();
for(int i = 0; i < qNames.Length;i++)
{
Console.WriteLine("{0}:{1}",
qNames[i].Name,qNames[i].Namespace);
}
}
}
#using <System.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public ref class Student
{
public:
[XmlAttributeAttribute]
String^ Name;
[XmlNamespaceDeclarationsAttribute]
XmlSerializerNamespaces^ myNamespaces;
};
void SerializeStudent( String^ filename );
void DeserializeStudent( String^ filename );
int main()
{
SerializeStudent( "Student.xml" );
DeserializeStudent( "Student.xml" );
}
void SerializeStudent( String^ filename )
{
XmlAttributes^ atts = gcnew XmlAttributes;
// Set to true to preserve namespaces,
// or false to ignore them.
atts->Xmlns = true;
XmlAttributeOverrides^ xover = gcnew XmlAttributeOverrides;
// Add the XmlAttributes and specify the name of the element
// containing namespaces.
xover->Add( Student::typeid, "myNamespaces", atts );
// Create the XmlSerializer using the
// XmlAttributeOverrides object.
XmlSerializer^ xser = gcnew XmlSerializer( Student::typeid,xover );
Student^ myStudent = gcnew Student;
XmlSerializerNamespaces^ ns = gcnew XmlSerializerNamespaces;
ns->Add( "myns1", "http://www.cpandl.com" );
ns->Add( "myns2", "http://www.cohowinery.com" );
myStudent->myNamespaces = ns;
myStudent->Name = "Student1";
FileStream^ fs = gcnew FileStream( filename,FileMode::Create );
xser->Serialize( fs, myStudent );
fs->Close();
}
void DeserializeStudent( String^ filename )
{
XmlAttributes^ atts = gcnew XmlAttributes;
// Set to true to preserve namespaces, or false to ignore them.
atts->Xmlns = true;
XmlAttributeOverrides^ xover = gcnew XmlAttributeOverrides;
// Add the XmlAttributes and specify the name of the
// element containing namespaces.
xover->Add( Student::typeid, "myNamespaces", atts );
// Create the XmlSerializer using the
// XmlAttributeOverrides object.
XmlSerializer^ xser = gcnew XmlSerializer( Student::typeid,xover );
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
Student^ myStudent;
myStudent = safe_cast<Student^>(xser->Deserialize( fs ));
fs->Close();
// Use the ToArray method to get an array of
// XmlQualifiedName objects.
array<XmlQualifiedName^>^qNames = myStudent->myNamespaces->ToArray();
for ( int i = 0; i < qNames->Length; i++ )
{
Console::WriteLine( "{0}:{1}", qNames[ i ]->Name, qNames[ i ]->Namespace );
}
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;
public class Student
{
/** @attribute XmlAttributeAttribute()
*/
public String name;
/** @attribute XmlNamespaceDeclarationsAttribute()
*/
public XmlSerializerNamespaces myNamespaces;
} //Student
public class Run
{
public static void main(String[] args)
{
Run test = new Run();
test.SerializeStudent("Student.xml");
test.DeserializeStudent("Student.xml");
} //main
public void SerializeStudent(String fileName)
{
XmlAttributes atts = new XmlAttributes();
// Set to true to preserve namespaces,
// or false to ignore them.
atts.set_Xmlns(true);
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
// Add the XmlAttributes and specify the name of the element
// containing namespaces.
xOver.Add(Student.class.ToType(), "myNamespaces", atts);
// Create the XmlSerializer using the
// XmlAttributeOverrides object.
XmlSerializer xSer = new XmlSerializer(Student.class.ToType(),
xOver);
Student myStudent = new Student();
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("myns1", "http://www.cpandl.com");
ns.Add("myns2", "http://www.cohowinery.com");
myStudent.myNamespaces = ns;
myStudent.name = "Student1";
FileStream fs = new FileStream(fileName, FileMode.Create);
xSer.Serialize(fs, myStudent);
fs.Close();
} //SerializeStudent
private void DeserializeStudent(String fileName)
{
XmlAttributes atts = new XmlAttributes();
// Set to true to preserve namespaces, or false to ignore them.
atts.set_Xmlns(true);
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
// Add the XmlAttributes and specify the name of the
// element containing namespaces.
xOver.Add(Student.class.ToType(), "myNamespaces", atts);
// Create the XmlSerializer using the
// XmlAttributeOverrides object.
XmlSerializer xSer = new XmlSerializer(Student.class.ToType(), xOver);
FileStream fs = new FileStream(fileName, FileMode.Open);
Student myStudent;
myStudent = (Student)xSer.Deserialize(fs);
fs.Close();
// Use the ToArray method to get an array of
// XmlQualifiedName objects.
XmlQualifiedName qNames[] = myStudent.myNamespaces.ToArray();
for (int i = 0; i < qNames.get_Length(); i++) {
Console.WriteLine("{0}:{1}", qNames[i].get_Name(), qNames[i].
get_Namespace());
}
} //DeserializeStudent
} //Run
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
XmlAttributes-Klasse
XmlAttributes-Member
System.Xml.Serialization-Namespace