3

I want to have a class property when serializing it to have multiple namespaces in the output. The XmlElementAttribute isn't working for me. Can anyone help?

My code:

XML Output:

<MyClass>
    <Property1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <Property2/>
</MyClass>

Class:

public class MyClass
{
    [XmlElementAttribute(Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string Property1 { get; set; }

    public string Property1 { get; set; }
}
3
  • Could you write an example of such class and the output you want to get? Commented Oct 25, 2011 at 14:58
  • I don't understand. Why do you want to define the namespaces, when you never use them? And it seems the serializer actually defines those two namespaces by default. Commented Oct 25, 2011 at 16:41
  • BTW, if you edit your question in an answer to a comment, you should also reply to that comment at the same time. That way, the person who wrote the comment get notified about it. Commented Oct 25, 2011 at 16:45

1 Answer 1

3

The serializer can defines prefixes (like in your example) but you only have one namespace per element. Use XmlElementAttribute (aka XmlElement) for the single relevant namespace and define prefixes when you serialize.

To get this:

<?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>

You have:

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

And serialize with prefixes set in your XmlSerializerNamespaces:

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.