c# - Remove unnecessary xsi and xsd namespaces from wcf -
i have built web services working except trying remove xsi , xsd namespaces.
i have seen lot link showing have use custom serializer :
xmlserializernamespaces namespaces = new xmlserializernamespaces(); namespaces.add(string.empty, string.empty);
but did not find way implement in code. here code :
[servicecontract, xmlserializerformat] [aspnetcompatibilityrequirements(requirementsmode = aspnetcompatibilityrequirementsmode.allowed)] public class myuser { [operationcontract] [webget(responseformat = webmessageformat.xml, uritemplate = "getuserinfo?token={userid}", bodystyle = webmessagebodystyle.bare)] public personnresponse validatetoken(string userid) { var response = new personnresponse(); response.userid = userid; response.firstname = myotherservicegetfirstname(userid); response.lastname = myotherservicegetlastname(userid); return response; } [datacontract(name = "person", namespace = "")] public class personnresponse { [datamember(name = "userid", emitdefaultvalue = false)] public string userid { get; set; } [datamember(name = "firstname", emitdefaultvalue = false)] public string firstname { get; set; } [datamember(name = "lastname", emitdefaultvalue = false)] public string lastname { get; set; } }
to you're looking should implement ixmlserializable:
[datacontract(name = "person", namespace = "")] public class personnresponse:ixmlserializable { ... public xmlschema getschema() { return null; } public void readxml (xmlreader reader) { var xd = xdocument.load(reader); firstname = xd.descendants().first (x => x.name.localname == "firstname" ).value; lastname = xd.descendants().first (x => x.name.localname == "lastname" ).value; userid = xd.descendants().first (x => x.name.localname == "userid" ).value; } public void writexml(xmlwriter writer){ writer.writeelementstring("userid", userid); writer.writeelementstring("firstname", firstname); writer.writeelementstring("lastname", lastname); } } public class test { static void main() { test t = new test(); t.serialize(); } private void serialize() { // create instance of class, , // instance of xmlserializer serialize it. var pers = new personnresponse(){ firstname="call me", lastname="heisenberg", userid="id"}; xmlserializer ser = new xmlserializer(typeof(personnresponse)); stringwriter tw = new stringwriter(); ser.serialize(tw,pers); console.writeline(tw.tostring()); //deserialize xml string var sw = new stringreader(tw.tostring()); var newperson = ser.deserialize(sw); } }
you'll end xml this:
<?xml version="1.0" encoding="utf-16"?> <personnresponse> <userid>id</userid> <firstname>call me</firstname> <lastname>heisenberg</lastname> </personnresponse>
Comments
Post a Comment