0

Following code qualifies XML Element and XML Attribute names.

public static void Main()
{
   Run test = new Run();
   test.SerializeObject("XmlNamespaces.xml");
}

public void SerializeObject(string filename)
{
   XmlSerializer mySerializer = new XmlSerializer(typeof(Books));

   TextWriter myWriter = new StreamWriter(filename);

   XmlSerializerNamespaces myNamespaces = new XmlSerializerNamespaces();
   myNamespaces.Add("books", "http://www.cpandl.com");
   myNamespaces.Add("money", "http://www.cohowinery.com");

   Book myBook = new Book();
   myBook.TITLE = "A Book Title";
   Price myPrice = new Price();
   myPrice.price = (decimal) 9.95;
   myPrice.currency = "US Dollar";
   myBook.PRICE = myPrice;
   Books 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;
}

I wonder is it possible to make it more generic? For example in class Books and Book it uses hard coded namespaces. For example in below XmlType:

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

NOTE: we have a situation where we have same element names in two different types of xml. But difference is in namespaces.

mrd
  • 2,095
  • 6
  • 23
  • 48
  • If you're worried about having to refactor he namespaces in the future you could always put them in a settings file and reference them instead of hard coding them, alternatively you could accept them as parameters for the objects. – Patrick Apr 04 '19 at 12:46
  • Possible duplicate of [How do I change the XmlElement name dynamically?](https://stackoverflow.com/questions/47751704/how-do-i-change-the-xmlelement-name-dynamically) – The One Apr 04 '19 at 12:47
  • @TheOne: I have repharsed my question. Is there any generic way to use namespace here? [XmlType(Namespace ="http://www.cpandl.com")] – mrd Apr 04 '19 at 13:08
  • You're also "hard coding" the element names (Book, TITLE, PRICE). So I fail to see how making the namespaces dynamic in some sort of way actually provides any advantages or flexibility. – MarkL Apr 04 '19 at 13:09
  • @MarkL: we have a situation where we have same element names in two different types of xml. But difference is in namespaces. – mrd Apr 04 '19 at 13:12
  • With respect, those are not the "same element names" - namespace is part of an element name, effectively, and you have to treat them as such, especially for serializing and deserializing. Instead of that, you could programatically handle the reading and writing of your xml via the xml document dom, then you could treat the namespaces (and element names) handily via variables, config file definitions, database structures, etc. – MarkL Apr 04 '19 at 17:57

0 Answers0