0

I want add a namespace in my xml root. Below is the result I want:

<ReconciliationRequest xmlns="http://schemas.xxx.co.xxx/test/crm">
    <Patient>
        <NHSNumber>1234567890</NHSNumber>
        <PDSChangeSerialNumber>1234</PDSChangeSerialNumber>
    </Patient>
    <!-- ... -->
</ReconciliationRequest>

But I'm getting below result

<ReconciliationRequest>
    <Patient>
        <NHSNumber>1234567890</NHSNumber>
        <PDSChangeSerialNumber>1234</PDSChangeSerialNumber>
    </Patient>
    <!-- ... -->
</ReconciliationRequest>

Below is the code I'm using to add the namespace:

TextWriter writer = new StreamWriter("D:\\ReconcillationRequest.xml");
XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();
nameSpace.Add(string.Empty, "http://schemas.XXXX.co.XX/Test/crm");

serializer.Serialize(writer, obj, nameSpace);
writer.Close();

I'm unable to add the namespace. Please guide me on this.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
shweta
  • 319
  • 2
  • 8
  • 21
  • 2
    You need to add above the class : [XmlRoot(ElementName = "ReconciliationRequest", Namespace = "http://schemas.xxx.co.xxx/test/crm")] Root does not have to be just on root class it can be on any class in the serialization. – jdweng Dec 06 '17 at 07:25
  • Duplicate of https://stackoverflow.com/questions/2500111/how-do-i-add-a-default-namespace-with-no-prefix-using-xmlserializer – Dylan Nicholson Dec 06 '17 at 07:30

0 Answers0