Can I somehow disable rendering of root element of collection?
This class with serialization attributes:
[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">
<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…
Best Answer
To disable rendering of root element of collection, you must replace the attribute
[XmlArrayItem]
with[XmlElement]
in your code.For removing the
xsi
andxsd
namespaces, create anXmlSerializerNamespaces
instance with an empty namespace and pass it when you need to serialize your object.Take a look on this example:
I got this output: