views:

106

answers:

1

Hi, is there a simple way to remove the namespace from the XML root element. I have tried with

[XmlRootAttribute("MCP", Namespace = "", IsNullable = false)]    

on the serializable class. But no use. still getting the same result.

sample class

[Serializable]
[XmlRootAttribute("MCP", Namespace = "", IsNullable = false)]    
public class BINDRequest
{
    public BINDRequest()
    {

    }
    [XmlAttribute]
    public string CLIENT_REQUEST_ID { get; set; }

    public BINDRequestBody BIND { get; set; }

}

result xml

<?xml version="1.0" encoding="utf-8"?>
<MCP xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" CLIENT_REQUEST_ID="1">
  <BIND CLIENT_ID="test" PASSWORD="test" />
</MCP>

i don't understand then whats the use of specifying namsespace in XmlRootAttribute??

Cheers

+3  A: 

Try this:

public class BINDRequest
{
    [XmlAttribute]
    public string CLIENT_REQUEST_ID { get; set; }
}

class Program
{
    static void Main()
    {
        var request = new BINDRequest
        {
            CLIENT_REQUEST_ID = "123"
        };
        var serializer = new XmlSerializer(request.GetType());
        var xmlnsEmpty = new XmlSerializerNamespaces();
        xmlnsEmpty.Add("", "");
        using (var writer = XmlWriter.Create("result.xml"))
        {
            serializer.Serialize(writer, request, xmlnsEmpty);
        }
    }
}
Darin Dimitrov
@Darin, thanks. it works as expected
Ramesh Vel