I need to send a uri to a Controller method in my my Web API app which has the following signature:
public HttpResponseMessage PostXMLFile(string serialNum, string siteNum, XElement xElement)
Or should it be this instead:
public HttpResponseMessage PostXMLFile(string serialNum, string siteNum, List<XElement> xElements)
?
Anyway, as you can see, it can't be just the XML, but must have the serialNum and siteNum args, too.
I'm assuming the uri can look like this:
http://<machineName>:<port>/api/<ControllerName>?serialNum=N&siteNum=N&xElement=<some flattened XML>
IOW, something like:
http://Platypus:4242/api/Platypi?serialNum=2&siteNum=7&xElement=<xml bla bla </xml>
Am I right (about what needs to be placed in the URI)?
If so, how do I get that "some flattened XML" there. Is it something like this:
string xml = Bla; //some XML as string
XMLSerializer xmlSerial = new XMLSerializer();
string serialized = xmlSerial.Serialize(xml);
...and then tack the contents of serialized onto the end of the URI? Or how can I accomplish this?
Note: the XML passed as an arg will usually contain hundreds of XML "records" (arrays of elements?)
UPDATE
This is what I would like to be able to do (fantasy pseudocode):
UriBuilder uriBuilder = new UriBuilder();
XMLSerializer xmlCereal = new XMLSerializer();
File xmlToSerialize = // Load XML from file
string xmlAsStr = xmlCereal.Serialize(xmlToSerialize);
uriBuilder.Query.Add(xmlAsStr);
// pass uri to the Web API Post method
...but don't know just how to implement that in reality.
UPDATE 2
Also, do I have to explicitly specify XML as a type to be serialized in my Web API by doing adding something to WebApiConfig's Register method? I currently have this, with Json explcitly added:
public static void Register(HttpConfiguration config, IWindsorContainer container)
{
MapRoutes(config);
RegisterControllerActivator(container);
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
}
Do I need to add a:
config.Formatters.XmlFormatter.UseXmlSerializer = true;
line or lines, too?