XmlArrayAttribute.Namespace 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
XML 요소의 네임스페이스를 가져오거나 설정합니다.
public:
property System::String ^ Namespace { System::String ^ get(); void set(System::String ^ value); };
public string Namespace { get; set; }
public string? Namespace { get; set; }
member this.Namespace : string with get, set
Public Property Namespace As String
속성 값
XML 요소의 네임스페이스입니다.
예제
다음 예제에서는 두 멤버가 포함된 클래스의 Library 인스턴스를 직렬화합니다. 하나는 책 제목을 포함하고 다른 하나는 주기적인 제목을 포함합니다. 두 XML 요소의 이름은 Titles서로 다르지만 각각 다른 접두사를 포함합니다. 이 예제에는 두 요소 이름을 XmlSerializerNamespaces 한정하는 데 사용되는 네임스페이스와 접두사를 포함하는 클래스의 인스턴스도 포함됩니다.
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Library
{
private Book[] books;
private Periodical [] periodicals;
/* This element will be qualified with the prefix
that is associated with the namespace http://wwww.cpandl.com. */
[XmlArray(ElementName = "Titles",
Namespace="http://wwww.cpandl.com")]
public Book[] Books
{
get{return books;}
set{books = value;}
}
/* This element will be qualified with the prefix that is
associated with the namespace http://www.proseware.com. */
[XmlArray(ElementName = "Titles", Namespace =
"http://www.proseware.com")]
public Periodical[] Periodicals
{
get{return periodicals;}
set{periodicals = value;}
}
}
public class Book
{
public string Title;
public string Author;
public string ISBN;
[XmlAttribute]
public string Publisher;
}
public class Periodical
{
private string title;
public string Title
{
get{return title;}
set{title = value;}
}
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.WriteBook("MyLibrary.xml");
test.ReadBook("MyLibrary.xml");
}
public void WriteBook(string filename)
{
// Creates a new XmlSerializer.
XmlSerializer mySerializer = new XmlSerializer(typeof(Library));
// Writing the file requires a StreamWriter.
TextWriter myStreamWriter = new StreamWriter(filename);
/* Creates an XmlSerializerNamespaces and adds prefixes and
namespaces to be used. */
XmlSerializerNamespaces myNamespaces =
new XmlSerializerNamespaces();
myNamespaces.Add("books", "http://wwww.cpandl.com");
myNamespaces.Add("magazines", "http://www.proseware.com");
// Creates an instance of the class to be serialized.
Library myLibrary = new Library();
// Creates two book objects.
Book b1 = new Book();
b1.Title = "My Book Title";
b1.Author = "An Author";
b1.ISBN = "000000000";
b1.Publisher = "Microsoft Press";
Book b2 = new Book();
b2.Title = "Another Book Title";
b2.Author = "Another Author";
b2.ISBN = "00000001";
b2.Publisher = "Another Press";
/* Creates an array using the objects, and sets the Books property
to the array. */
Book[] myBooks = {b1,b2};
myLibrary.Books = myBooks;
// Creates two Periodical objects.
Periodical per1 = new Periodical();
per1.Title = "My Magazine Title";
Periodical per2 = new Periodical();
per2.Title = "Another Magazine Title";
// Sets the Periodicals property to the array.
Periodical[] myPeridocials = {per1, per2};
myLibrary.Periodicals = myPeridocials;
// Serializes the myLibrary object.
mySerializer.Serialize(myStreamWriter, myLibrary, myNamespaces);
myStreamWriter.Close();
}
public void ReadBook(string filename)
{
/* Creates an instance of an XmlSerializer
with the class used to read the document. */
XmlSerializer mySerializer = new XmlSerializer(typeof(Library));
// A FileStream is needed to read the file.
FileStream myFileStream = new FileStream(filename, FileMode.Open);
Library myLibrary = (Library) mySerializer.Deserialize(myFileStream);
// Reads each book in the array returned by the Books property.
for(int i = 0; i< myLibrary.Books.Length;i++)
{
Console.WriteLine(myLibrary.Books[i].Title);
Console.WriteLine(myLibrary.Books[i].Author);
Console.WriteLine(myLibrary.Books[i].ISBN);
Console.WriteLine(myLibrary.Books[i].Publisher);
Console.WriteLine();
}
// Reads each Periodical returned by the Periodicals property.
for(int i = 0; i< myLibrary.Periodicals.Length;i++)
{
Console.WriteLine(myLibrary.Periodicals[i].Title);
}
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Public Class Library
Private myBooks() As Book
Private myPeriodicals() As Periodical
' This element will be qualified with the prefix
' that is associated with the namespace http://wwww.cpandl.com.
<XmlArray(ElementName := "Titles", _
Namespace := "http://wwww.cpandl.com")> _
Public Property Books() As Book()
Get
Return myBooks
End Get
Set
myBooks = value
End Set
End Property
' This element will be qualified with the prefix that is
' associated with the namespace http://www.proseware.com.
<XmlArray(ElementName := "Titles", _
Namespace := "http://www.proseware.com")> _
Public Property Periodicals() As Periodical()
Get
Return myPeriodicals
End Get
Set
myPeriodicals = value
End Set
End Property
End Class
Public Class Book
Public Title As String
Public Author As String
Public ISBN As String
<XmlAttribute()> Public Publisher As String
End Class
Public Class Periodical
Private myTitle As String
Public Property Title() As String
Get
Return myTitle
End Get
Set
myTitle = value
End Set
End Property
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.WriteBook("MyLibrary.xml")
test.ReadBook("MyLibrary.xml")
End Sub
Public Sub WriteBook(ByVal filename As String)
' Creates a new XmlSerializer.
Dim mySerializer As New XmlSerializer(GetType(Library))
' Writing the file requires a StreamWriter.
Dim myStreamWriter As New StreamWriter(filename)
' Creates an XmlSerializerNamespaces and adds prefixes and
' namespaces to be used.
Dim myNamespaces As New XmlSerializerNamespaces()
myNamespaces.Add("books", "http://wwww.cpandl.com")
myNamespaces.Add("magazines", "http://www.proseware.com")
' Create an instance of the class to be serialized.
Dim myLibrary As New Library()
' Creates two book objects.
Dim b1 As New Book()
b1.Title = "My Book Title"
b1.Author = "An Author"
b1.ISBN = "000000000"
b1.Publisher = "Microsoft Press"
Dim b2 As New Book()
b2.Title = "Another Book Title"
b2.Author = "Another Author"
b2.ISBN = "00000001"
b2.Publisher = "Another Press"
' Creates an array using the objects, and sets the Books property
' to the array.
Dim myBooks As Book() = {b1, b2}
myLibrary.Books = myBooks
' Creates two Periodical objects.
Dim per1 As New Periodical()
per1.Title = "My Magazine Title"
Dim per2 As New Periodical()
per2.Title = "Another Magazine Title"
' Sets the Periodicals property to the array.
Dim myPeriodicals() As Periodical = {per1, per2}
myLibrary.Periodicals = myPeriodicals
' Serializes the myLibrary object.
mySerializer.Serialize(myStreamWriter, myLibrary, myNamespaces)
myStreamWriter.Close()
End Sub
Public Sub ReadBook(ByVal filename As String)
' Creates an instance of an XmlSerializer
' with the class used to read the document.
Dim mySerializer As New XmlSerializer(GetType(Library))
' A FileStream is needed to read the file.
Dim myFileStream As New FileStream(filename, FileMode.Open)
Dim myLibrary As Library = _
CType(mySerializer.Deserialize(myFileStream), Library)
' Reads each book in the array returned by the Books property.
Dim i As Integer
For i = 0 To myLibrary.Books.Length - 1
Console.WriteLine(myLibrary.Books(i).Title)
Console.WriteLine(myLibrary.Books(i).Author)
Console.WriteLine(myLibrary.Books(i).ISBN)
Console.WriteLine(myLibrary.Books(i).Publisher)
Console.WriteLine()
Next i
' Reads each Periodical returned by the Periodicals property.
For i = 0 To myLibrary.Periodicals.Length - 1
Console.WriteLine(myLibrary.Periodicals(i).Title)
Next i
End Sub
End Class
설명
이 Namespace 속성을 사용하면 정규화된 XML 요소 이름을 만들 수 있습니다. 이 속성은 Namespace XML의 네임스페이스라는 1999년 World Wide Web 컨소시엄 문서에 있는 XML 네임스페이스를 만드는 규칙을 준수합니다.
접두사와 연결된 네임스페이스를 만들려면 XML 문서에 사용되는 네임스페이스 및 접두사를 포함하는 클래스의 XmlSerializerNamespaces 인스턴스를 만들어야 합니다. 각각 XmlArrayAttribute에 대해 네임스페이스를 설정하면 해당 네임스페이스가 에 있는 XmlSerializerNamespaces네임스페이스 중 하나와 일치해야 합니다. XML이 생성되면 각 배열에 지정된 네임스페이스와 연결된 접두사 접두사로 올바르게 접두사로 지정됩니다.