XmlSerializerNamespaces Class
Contains the XML namespaces and prefixes that the XmlSerializer uses to generate qualified names in an XML-document instance.
Assembly: System.Xml (in System.Xml.dll)
The XmlSerializerNamespaces contains a collection of XML namespaces, each with an associated prefix. The XmlSerializer uses an instance of the XmlSerializerNamespaces class to create qualified names in an XML document.
XML namespaces contained by the XmlSerializerNamespaces must conform to the www.w3.org specification named Namespaces in XML.
XML namespaces provide a way to qualify the names of XML elements and attributes in XML documents. A qualified name consists of a prefix and a local name, separated by a colon. The prefix functions only as a placeholder; it is mapped to a URI that specifies a namespace. The combination of the universally-managed URI namespace and the local name produces a name that is guaranteed to be universally unique.
To create qualified names in an XML document:
Create an XmlSerializerNamespaces instance.
Add each prefix and namespace pair that you want to the instance.
Apply the appropriate.NET attribute to each property or class that the XmlSerializer serializes into an XML document. The available attributes are:
Set the Namespace property of each attribute to one of the namespace values from the XmlSerializerNamespaces object.
Pass the XmlSerializerNamespaces to the Serialize method of the XmlSerializer.
Note: |
|---|
The creation of an empty namespace and prefix pair is not supported. That is, you cannot create a pair using the following code: |
The following example creates an XmlSerializerNamespaces object, and adds two prefix and namespace pairs to it. The example then passes the XmlSerializerNamespaces to the Serialize method, which serializes a Books object into an XML document. Using the XmlSerializerNamespaces object, the Serialize method qualifies each XML element and attribute with one of the two namespaces.
Imports System Imports System.IO Imports System.Xml Imports System.Xml.Serialization Public Class Run Public Shared Sub Main() Dim test As New Run() test.SerializeObject("XmlNamespaces.xml") End Sub Public Sub SerializeObject(ByVal filename As String) Dim s As New XmlSerializer(GetType(Books)) ' Writing a file requires a TextWriter. Dim t As New StreamWriter(filename) ' Create an XmlSerializerNamespaces object and add two ' prefix-namespace pairs. Dim ns As New XmlSerializerNamespaces() ns.Add("books", "http://www.cpandl.com") ns.Add("money", "http://www.cohowinery.com") ' Create a Book instance. Dim b As New Book() b.TITLE = "A Book Title" Dim p As New Price() p.price = CDec(9.95) p.currency = "US Dollar" b.PRICE = p Dim bks As New Books() bks.Book = b s.Serialize(t, bks, ns) t.Close() End Sub End Class Public Class Books <XmlElement(Namespace := "http://www.cohowinery.com")> _ Public Book As Book End Class <XmlType(Namespace := "http://www.cpandl.com")> _ Public Class Book <XmlElement(Namespace := "http://www.cpandl.com")> _ Public TITLE As String <XmlElement(Namespace := "http://www.cohowinery.com")> _ Public PRICE As Price End Class Public Class Price <XmlAttribute(Namespace := "http://www.cpandl.com")> _ Public currency As String <XmlElement(Namespace := "http://www.cohowinery.com")> _ Public price As Decimal End Class
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public __gc class Price
{
public:
[XmlAttributeAttribute(Namespace = S"http://www.cpandl.com")]
String* currency;
[XmlElement(Namespace = S"http://www.cohowinery.com")]
Decimal price;
};
[XmlType(Namespace =S"http://www.cpandl.com")]
public __gc class Book
{
public:
[XmlElement(Namespace = S"http://www.cpandl.com")]
String* TITLE;
[XmlElement(Namespace =S"http://www.cohowinery.com")]
Price* PRICE;
};
public __gc class Books
{
public:
[XmlElement(Namespace = S"http://www.cohowinery.com")]
Book* Book;
};
public __gc class Run
{
public:
static void main()
{
Run* test = new Run();
test->SerializeObject(S"XmlNamespaces.xml");
}
void SerializeObject(String* filename)
{
XmlSerializer* s = new XmlSerializer(__typeof(Books));
// Writing a file requires a TextWriter.
TextWriter* t = new StreamWriter(filename);
/* Create an XmlSerializerNamespaces object and add two
prefix-namespace pairs. */
XmlSerializerNamespaces* ns = new XmlSerializerNamespaces();
ns->Add(S"books", S"http://www.cpandl.com");
ns->Add(S"money", S"http://www.cohowinery.com");
// Create a Book instance.
Book* b = new Book();
b->TITLE = S"A Book Title";
Price* p = new Price();
p->price = (Decimal) 9.95;
p->currency = S"US Dollar";
b->PRICE = p;
Books* bks = new Books();
bks->Book = b;
s->Serialize(t,bks,ns);
t->Close();
}
};
int main()
{
Run::main();
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: