0

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>
1
  • I doubt you can do this with XmlSerializer alone: those two XML documents are semantically equivalent. You might need to generate it without the root xmlns, and add it in later with e.g. XDocument. Commented Feb 18, 2020 at 10:29

1 Answer 1

1

The xmlns:if attribute is a namespace declaration. These are special attributes and shouldn't be specified explicitly as properties as you've done here - the serialiser will handle this for you.

I'd also note that namespaces are inherited from their parents, so most of the places you've specified the namespace aren't required. You also only need an XmlRoot attribute on the root. This should be enough:

public class Bd
{
    [XmlElement("msg")]
    public Msg Msg { get; set; }
}

[XmlRoot("tr", Namespace = "http://www.fca.com/ifast")]
public class Tr
{
    [XmlElement("hd")]
    public Hd Hd { get; set; }
    [XmlElement("bd")]
    public Bd Bd { get; set; }
}

The two bits of XML you've included are semantically the same thing, but if you want to control the namespace prefix you can do this on serialisation:

var ns = new XmlSerializerNamespaces();
ns.Add("if", "http://www.fca.com/ifast");

var serializer = new XmlSerializer(typeof(Tr));

serializer.Serialize(stream, obj, ns);

See this fiddle for a working demo.

Sign up to request clarification or add additional context in comments.

2 Comments

Your output doesn't include xmlns="http://www.fca.com/ifast" on the root, which is something the OP said they wanted
@canton7 that doesn't have any meaning, so the serialiser won't let you add it as far as I'm aware. I think your suggestion to add this after the fact via XDocument would be the best way - though I would emphasise it really shouldn't be necessary.

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.