XmlArrayAttribute.Namespace Propiedad

Definición

Obtiene o establece el espacio de nombres del elemento XML.

public string Namespace { get; set; }
public string? Namespace { get; set; }

Valor de propiedad

Espacio de nombres del elemento XML.

Ejemplos

En el ejemplo siguiente se serializa una instancia de la Library clase que contiene dos miembros, uno que contiene títulos de libro y otro que contiene títulos periódicos. Aunque ambos elementos XML se denominan Titles, cada uno contiene un prefijo diferente. En el ejemplo también se incluye una instancia de la XmlSerializerNamespaces clase que contiene los espacios de nombres y prefijos usados para calificar los dos nombres de elemento.

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);
      }
   }
}

Comentarios

La Namespace propiedad permite crear nombres de elementos XML completos. La Namespace propiedad se ajusta a las reglas para crear un espacio de nombres XML tal y como se encuentra en el documento world Wide Web Consortium de 1999 titulado Espacios de nombres en XML.

Para crear espacios de nombres asociados a un prefijo, debe crear una instancia de la XmlSerializerNamespaces clase que contenga los espacios de nombres y prefijos usados en el documento XML. Al establecer el espacio de nombres para cada XmlArrayAttribute, debe coincidir con uno de los espacios de nombres de XmlSerializerNamespaces. Cuando se genera el XML, cada matriz se prefijo correctamente con el prefijo asociado al espacio de nombres especificado.

Se aplica a

Producto Versiones
.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

Consulte también