views:

900

answers:

3

hi, can i someway disable rendering of root element of collection?

this class with serialization attibutes

 [XmlRoot(ElementName="SHOPITEM", Namespace="")]
    public class ShopItem
    {
        [XmlElement("PRODUCTNAME")]
        public string ProductName { get; set; }       

        [XmlArrayItem("VARIANT")]
        public List<ShopItem> Variants { get; set; }

    }

generates this xml:

<SHOPITEM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      <PRODUCTNAME>test</PRODUCTNAME>
      <Variants>
        <VARIANT>
          <PRODUCTNAME>hi 1</PRODUCTNAME>
        </VARIANT>
        <VARIANT>
          <PRODUCTNAME>hi 2</PRODUCTNAME>
        </VARIANT>           
      </Variants>        
    </SHOPITEM>

I don't want <Variants> element here. What must i do?

Also i don't need xsi and xsd namespaces in root element..

Thanks a lot for help.

A: 

I don't believe it is possible to remove this element using the default xml serialization (with attributes). If you could do this, then serializing your ShopItem class would result in badly formed xml (no root element) for the object, which is not allowed.

What you can do however, is manually implement IXmlSerializable. This will give you the sort of fine-grained control you a re after.

[Edit] - sorry - misread that you were trying to remove Variants, not SHOPITEM. To remove the List "outer" element, just mark it up with an [XmlElement] attribute rather than an [XmlArrayItem] attribute. This will cause the list entries to just use the specified element name, without wrapping the list in an outer element.

For removing the namespaces, this is controlled by the seriliazer itself, not the markup on the class. I've just noticed that while I've updated this answer, Rubens Farias has provided an reply that shows you how to eliminate the namespace.

Rob Levine
oops - misread your post - thought you were trying to remove SHOPITEM - just editing answer!
Rob Levine
+9  A: 

Try this:

[XmlRoot(ElementName = "SHOPITEM", Namespace = "")]
public class ShopItem
{
    [XmlElement("PRODUCTNAME")]
    public string ProductName { get; set; }

    [XmlElement("VARIANT")] // was [XmlArrayItem]
    public List<ShopItem> Variants { get; set; }
}

// ...

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");

ShopItem item = new ShopItem()
{
    ProductName = "test",
    Variants = new List<ShopItem>()
    {
        new ShopItem(){ ProductName = "hi 1" },
        new ShopItem(){ ProductName = "hi 2" }
    }
};

XmlSerializer ser = new XmlSerializer(typeof(ShopItem));
ser.Serialize(Console.Out, item, ns);

I got this output:

<?xml version="1.0" encoding="ibm850"?>
<SHOPITEM>
  <PRODUCTNAME>test</PRODUCTNAME>
  <VARIANT>
    <PRODUCTNAME>hi 1</PRODUCTNAME>
  </VARIANT>
  <VARIANT>
    <PRODUCTNAME>hi 2</PRODUCTNAME>
  </VARIANT>
</SHOPITEM>
Rubens Farias
THANKS! so simple.. :)
Jan Remunda
+2  A: 

Replace [XmlArrayItem("VARIANT")] with [XmlElement("VARIANT")].

Pent Ploompuu