public class XmlSerializerNamespaces
|
XML namespaces contained by the XmlSerializerNamespaces must conform to the www.w3.org specification named 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.
To create qualified names in an XML document:
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("XmlNamespaces.xml");
}
public 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("books", "http://www.cpandl.com");
ns.Add("money", "http://www.cohowinery.com");
// Create a Book instance.
Book b = new Book();
b.TITLE = "A Book Title";
Price p = new Price();
p.price = (decimal) 9.95;
p.currency = "US Dollar";
b.PRICE = p;
Books bks = new Books();
bks.Book = b;
s.Serialize(t,bks,ns);
t.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;
}
| ctor #1 | Overloaded:.ctor()Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the XmlSerializerNamespaces class. |
| ctor #2 | Overloaded:.ctor(XmlQualifiedName[] namespaces) Initializes a new instance of the XmlSerializerNamespaces class. |
| ctor #3 | Overloaded:.ctor(XmlSerializerNamespaces namespaces) Initializes a new instance of the XmlSerializerNamespaces class, using the specified array of XmlQualifiedName objects to create a collection of prefix-namespace pairs. |
| Count | Read-only Gets the number of prefix-namespace pairs in the collection. |
| Add | Adds a prefix-namespace pair to an XmlSerializerNamespaces object. |
| Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
| GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
| GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
| ToArray | Gets the array of prefix-namespace pairs in an XmlSerializerNamespaces object. |
| ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
| Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
| MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public XmlSerializerNamespaces(); |
private void CreateBook(string filename)
{
try
{
// Create instance of XmlSerializerNamespaces and add the namespaces.
XmlSerializerNamespaces myNameSpaces = new XmlSerializerNamespaces();
myNameSpaces.Add("BookName", "http://www.cpandl.com");
// Create instance of XmlSerializer and specify the type of object
// to be serialized.
XmlSerializer mySerializerObject =
new XmlSerializer(typeof(MyBook));
TextWriter myWriter = new StreamWriter(filename);
// Create object to be serialized.
MyBook myXMLBook = new MyBook();
myXMLBook.Author = "XMLAuthor";
myXMLBook.BookName = "DIG THE XML";
myXMLBook.Description = "This is a XML Book";
MyPriceClass myBookPrice = new MyPriceClass();
myBookPrice.Price = (decimal) 45.89;
myBookPrice.Units = "$";
myXMLBook.BookPrice = myBookPrice;
// Serialize the object.
mySerializerObject.Serialize(myWriter, myXMLBook,myNameSpaces);
myWriter.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception :" + e.Message + "Occured");
}
}
public XmlSerializerNamespaces( |
namespaces
public XmlSerializerNamespaces(XmlSerializerNamespaces( |
namespaces
private XmlSerializerNamespaces CreateFromQNames()
{
XmlQualifiedName q1 =
new XmlQualifiedName("money", "http://www.cohowinery.com");
XmlQualifiedName q2 =
new XmlQualifiedName("books", "http://www.cpandl.com");
XmlQualifiedName[] names = {q1, q2};
return new XmlSerializerNamespaces(names);
}
public int Count {get;}
|
prefix
ns
Any namespaces that you add must conform to the www.w3.org specification Namespaces in XML.
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;
}
~XmlSerializerNamespaces(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public XmlQualifiedName[] ToArray(); |
private void PrintNamespacePairs(XmlSerializerNamespaces namespaces)
{
XmlQualifiedName[] qualifiedNames = namespaces.ToArray();
for(int i = 0; i < qualifiedNames.Length; i++)
{
Console.WriteLine
(
qualifiedNames[i].Name + "\t" +
qualifiedNames[i].Namespace
);
}
}
public virtual string ToString(); |