XmlSerializerNamespaces.Add(String, String) Method

Definition

Adds a prefix and namespace pair to an XmlSerializerNamespaces object.

C#
public void Add(string prefix, string ns);
C#
public void Add(string prefix, string? ns);

Parameters

prefix
String

The prefix associated with an XML namespace.

ns
String

An XML namespace.

Examples

The following example creates an XmlSerializerNamespaces object, and adds three prefix and namespace pairs to it by calling the Add method.

C#
private XmlSerializerNamespaces AddNamespaces()
{
   XmlSerializerNamespaces xmlNamespaces =
   new XmlSerializerNamespaces();

   // Add three prefix-namespace pairs.
   xmlNamespaces.Add("money", "http://www.cpandl.com");
   xmlNamespaces.Add("books", "http://www.cohowinery.com");
   xmlNamespaces.Add("software", "http://www.microsoft.com");

   return xmlNamespaces;
}

Remarks

If you want the XmlSerializer to qualify the element and attribute names in an XML document, you must Add the prefix and namespace pairs to an XmlSerializerNamespaces object.

Any namespaces that you add must conform to the W3C Namespaces in XML specification.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0