I have a class which is adding attribute to the all other elements except the root element of the XML File. C# Class is as follows
[XmlRoot(ElementName = "bd", Namespace = "http://www.fca.com/ifast")]
public class Bd
{
[XmlElement(ElementName = "msg", Namespace = "http://www.fca.com/ifast")]
public Msg Msg { get; set; }
}
[XmlRoot(ElementName = "tr", Namespace = "http://www.fca.com/ifast")]
public class Tr
{
[XmlElement(ElementName = "hd", Namespace = "http://www.fca.com/ifast")]
public Hd Hd { get; set; }
[XmlElement(ElementName = "bd", Namespace = "http://www.fca.com/ifast")]
public Bd Bd { get; set; }
[XmlAttribute(AttributeName = "if", Namespace = "http://www.w3.org/2000/xmlns/")]
public string If { get; set; }
}
With this code i am able to generate XML file like this
'<?xml version="1.0" encoding="utf-16"?>
<tr xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema
instance" xmlns:if="http://www.fca.com/ifast" xmlns="http://www.fca.com/ifast">
<if:hd>
</if:hd>
<if:bd>
</if:bd>
</tr>'
But i need to add 'if:' element on root element as well like this
<?xml version="1.0" encoding="utf-16"?>
<if:tr xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema
instance" xmlns:if="http://www.fca.com/ifast" xmlns="http://www.fca.com/ifast">
<if:hd>
</if:hd>
<if:bd>
</if:bd>
</if:tr>
XmlSerializeralone: those two XML documents are semantically equivalent. You might need to generate it without the rootxmlns, and add it in later with e.g.XDocument.