質問

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?

役に立ちましたか?

解決

I'll post this as an answer as it's a bit hard to describe in comments

public HttpResponseMessage PostXMLFile(string serialNum, string siteNum, string xmlString)
{
    var xElement = XElement.Parse(xmlString);
    // do whatever now with your xElement;
}

Then you'd be able to call your method like:

http://<machineName>:<port>/api/<ControllerName>?serialNum=2&siteNum=7&xmlString=<xml bla bla </xml>

(though watch out for encoding)

As for adding the XML to your url, because it's just a string, you can just read it as normal text and append it to your url.

If you want to accept lists of elements, I believe you can change your controller method to

public HttpResponseMessage PostXMLFile(string serialNum, string siteNum, [ModelBinder]List<string> xmlStrings)
{
    foreach(var xmlString in xmlStrings)
    {
       var xElement = XElement.Parse(xmlString);
       // do whatever now with your xElement;
    }
}

and your url will look something like

http://<machineName>:<port>/api/<ControllerName>?serialNum=2&siteNum=7&xmlStrings=<xml bla bla </xml>&xmlStrings=<xml more bla bla </xml>&xmlStrings=<xml even more bla bla </xml>

It doesn't quite make sense to me that you would want to load text from a file and then 'serialize' it to XML for wire transmission when both the file contents and xml are already serialized. You don't need to create any XDocuments or XElements until your controller method receives the xml string.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top