I am currently having the following code to generate an XML response for a web service (.asmx) request method.
StringWriter stringwriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringwriter);
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement("root");
xmlTextWriter.WriteElementString("Value", demo.Value);
xmlTextWriter.WriteElementString("ID", demo.ID);
xmlTextWriter.WriteElementString("CoreProductCode", demo.CoreProductCode);
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndDocument();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(stringwriter.ToString());
return xmlDoc;
As you can see, there is a 'root' tag which is the starting tag. Now the issue is that it is default accompanied by a xmlns="" attribute with it. So basically, the final response looks like this :
<requestProductResponse xmlns="http://tempuri.org/">
<requestProductResult>
<root xmlns="">
<Value>0</Value>
<ID>11</ID>
<CoreProductCode>1</CoreProductCode>
</root>
</requestTokenResult>
Now my requirement is such that only one xmlns attribute is to be allowed and I am having two of them. So I have to get rid of the xmlns or the whole root tag.
Things I have tried :
It would be helpful if you can provide me some more insight on how this can be done and what steps are to be taken. Any sort of guidance and help is appreciated.
Thanks.