XmlAttributes.Xmlns 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
개체를 반환 XmlSerializerNamespaces 하는 멤버를 포함하는 개체가 재정의될 때 모든 네임스페이스 선언을 유지할지 여부를 지정하는 값을 가져오거나 설정합니다.
public:
property bool Xmlns { bool get(); void set(bool value); };
public bool Xmlns { get; set; }
member this.Xmlns : bool with get, set
Public Property Xmlns As Boolean
속성 값
true네임스페이스 선언을 유지해야 하면 이고, 그렇지 않으면 . false
예제
다음 예제에는 이름이 MyNamespaces 된 멤버가 포함됩니다. 이 예제에서는 클래스의 인스턴스에 추가되는 개체를 XmlAttributeOverrides 만듭니다XmlAttributes. 개체 Xmlns 의 serialization이 재정의 Student 될 때 네임스페이스를 유지하도록 지시 XmlSerializer 하는 속성이 설정true됩니다.
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);
}
}
}
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
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