How to qualify XML element and XML attribute names
XML namespaces contained by instances of the XmlSerializerNamespaces class must conform to the World Wide Web Consortium (W3C) specification called Namespaces in XML.
XML namespaces provide a method for qualifying the names of XML elements and XML 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.
By creating an instance of XmlSerializerNamespaces
and adding the namespace pairs to the object, you can specify the prefixes used in an XML document.
To create qualified names in an XML document
Create an instance of the
XmlSerializerNamespaces
class.Add all prefixes and namespace pairs to the
XmlSerializerNamespaces
.Apply the appropriate
System.Xml.Serialization
attribute to each member or class that the XmlSerializer is to serialize into an XML document.The available attributes are: XmlAnyElementAttribute, XmlArrayAttribute, XmlArrayItemAttribute, XmlAttributeAttribute, XmlElementAttribute, XmlRootAttribute, and XmlTypeAttribute.
Set the
Namespace
property of each attribute to one of the namespace values from theXmlSerializerNamespaces
.Pass the
XmlSerializerNamespaces
to theSerialize
method of theXmlSerializer
.
Example
The following example creates an XmlSerializerNamespaces
, and adds two prefix and namespace pairs to the object. The code creates an XmlSerializer
that is used to serialize an instance of the Books
class. The code calls the Serialize
method with the XmlSerializerNamespaces
, allowing the XML to contain prefixed namespaces.
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Public Module Program
Public Sub Main()
SerializeObject("XmlNamespaces.xml")
End Sub
Public Sub SerializeObject(filename As String)
Dim mySerializer As New XmlSerializer(GetType(Books))
' Writing a file requires a TextWriter.
Dim myWriter As New StreamWriter(filename)
' Creates an XmlSerializerNamespaces and adds two
' prefix-namespace pairs.
Dim myNamespaces As New XmlSerializerNamespaces()
myNamespaces.Add("books", "http://www.cpandl.com")
myNamespaces.Add("money", "http://www.cohowinery.com")
' Creates a Book.
Dim myBook As New Book()
myBook.TITLE = "A Book Title"
Dim myPrice As New Price()
myPrice.price = CDec(9.95)
myPrice.currency = "US Dollar"
myBook.PRICE = myPrice
Dim myBooks As New Books()
myBooks.Book = myBook
mySerializer.Serialize(myWriter, myBooks, myNamespaces)
myWriter.Close()
End Sub
End Module
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 System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Program
{
public static void Main()
{
SerializeObject("XmlNamespaces.xml");
}
public static void SerializeObject(string filename)
{
var mySerializer = new XmlSerializer(typeof(Books));
// Writing a file requires a TextWriter.
TextWriter myWriter = new StreamWriter(filename);
// Creates an XmlSerializerNamespaces and adds two
// prefix-namespace pairs.
var myNamespaces = new XmlSerializerNamespaces();
myNamespaces.Add("books", "http://www.cpandl.com");
myNamespaces.Add("money", "http://www.cohowinery.com");
// Creates a Book.
var myBook = new Book();
myBook.TITLE = "A Book Title";
var myPrice = new Price();
myPrice.price = (decimal) 9.95;
myPrice.currency = "US Dollar";
myBook.PRICE = myPrice;
var myBooks = new Books();
myBooks.Book = myBook;
mySerializer.Serialize(myWriter, myBooks, myNamespaces);
myWriter.Close();
}
}
public class Books
{
[XmlElement(Namespace = "http://www.cohowinery.com")]
public Book Book;
}
[XmlType(Namespace ="http://www.cpandl.com")]
public class Book
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string TITLE;
[XmlElement(Namespace ="http://www.cohowinery.com")]
public Price PRICE;
}
public class Price
{
[XmlAttribute(Namespace = "http://www.cpandl.com")]
public string currency;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal price;
}
See also
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for