Сериализатор может определять префиксы (как в вашем примере), но у вас есть только одно пространство имен на элемент.Используйте XmlElementAttribute (он же XmlElement) для одного релевантного пространства имен и определите префиксы при сериализации.
Чтобы получить это:
<?xml version="1.0" encoding="utf-16"?>
<OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<inventory:ItemName>Widget</inventory:ItemName>
<inventory:Description>Regular Widget</inventory:Description>
<money:UnitPrice>2.3</money:UnitPrice>
<inventory:Quantity>10</inventory:Quantity>
<money:LineTotal>23</money:LineTotal>
</OrderedItem>
У вас есть:
public class OrderedItem
{
[XmlElementAttribute(Namespace = "http://www.cpandl.com")]
public string ItemName { get; set; }
[XmlElementAttribute(Namespace = "http://www.cpandl.com")]
public string Description { get; set; }
[XmlElementAttribute(Namespace = "http://www.cohowinery.com")]
public decimal UnitPrice { get; set; }
[XmlElementAttribute(Namespace = "http://www.cpandl.com")]
public int Quantity { get; set; }
[XmlElementAttribute(Namespace = "http://www.cohowinery.com")]
public int LineTotal { get; set; }
}
Исериализация с префиксами, установленными в ваших пространствах имен XmlSerializer:
OrderedItem example = new OrderedItem
{
ItemName = "Widget",
Description = "Regular Widget",
UnitPrice = (decimal) 2.3,
Quantity = 10,
LineTotal = 23
};
XmlSerializer serializerX = new XmlSerializer(example.GetType());
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("inventory", "http://www.cpandl.com");
namespaces.Add("money", "http://www.cohowinery.com");
serializerX.Serialize(Console.Out, example, namespaces);