0

Good Day Fellow Coders,

I'm working on a simple Web API (.NET CORE 2.0) that accepts XML request.

Controller:

Route("api/Product")]
public class ProductController : Controller
{
    [Route("List")]
    [HttpPost]
    public IActionResult List([FromBody]Body request)
    {
        return Ok();
    }
}

Class:

public class ProductRequest
{
    public string ProductId { get; set; }

    public string ProductDescription { get; set; }
}

[XmlRoot(ElementName = "Katawan")]
public class Body
{
    [XmlElement(ElementName = "Product", Namespace = "")]
    public ProductRequest ProductRequest { get; set; }
}

Request: enter image description here

This works fine and able to see the the actual request when debugging, however we did some changes on the request. We added some namespace on the Body. See below Image

enter image description here

After we added the prefix 'x', the request is now always null. I would like to request for your help here. I've already did some research like adding a DataContract and DataMember Attribute and set the namespace to blank, still no luck.

Update

We added a namespace for x but still the same, we also added new prefix on the Product. This is now the final xml.

<?xml version="1.0"?>
<x:Katawan
    xmlns:x="http://www.w3.org/1999/xhtml">
    <v:Product>
        <ProductId>string</ProductId>
        <ProductDescription>string</ProductDescription>
    </v:Product>
</x:Katawan>
dbc
  • 104,963
  • 20
  • 228
  • 340
Chris
  • 599
  • 7
  • 23
  • 1
    Is it the your case? https://stackoverflow.com/questions/2339782/xml-serialization-and-namespace-prefixes – Yury Schkatula Jan 10 '18 at 15:00
  • sorry, not namespace, but a prefix. Edited the post. Thank you – Chris Jan 10 '18 at 15:05
  • 2
    Your xml isn't valid. You need to add an xmlns attribute for the x alias. – StuartLC Jan 10 '18 at 15:12
  • Added but still the same. the request is still null. Thanks – Chris Jan 10 '18 at 15:17
  • You need to define the xnamespace – jdweng Jan 10 '18 at 15:20
  • Added but still the same, we added new prefix on the Product. This is now the final xml.` string string ` – Chris Jan 10 '18 at 15:31
  • Your updated XML is still not well-formed -- the namespace prefix `v:` now has no `xmlns:v="..."` definition. For the difference between well-formed and valid XML, see https://en.wikipedia.org/wiki/Well-formed_document and http://www.informit.com/articles/article.aspx?p=24992&seqNum=8. To make sure your XML is well-formed you can just upload it to https://www.xmlvalidation.com/ or test-parse it with [`XDocument.Parse()`](https://msdn.microsoft.com/en-us/library/bb345532(v=vs.110).aspx). The asp.net XML serializers will not deserialize ill-formed XML. – dbc Jan 10 '18 at 17:25
  • 1
    Also, can you include your XML as text rather than as an image? For why see [Why not upload images of code on SO when asking a question](https://meta.stackoverflow.com/a/285557/3744182) and [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/q/303812/3744182). – dbc Jan 10 '18 at 17:28
  • You shouldn't be using the `http://www.w3.org/1999/xhtml` namespace for your own XML format. That namespace is reserved for, well, xhtml. Anyway, after you added a namespace, did you update this to refer to that namespace: `[XmlElement(ElementName = "Product", Namespace = "")]` and this one: `[XmlRoot(ElementName = "Katawan")]` – JLRishe Jan 10 '18 at 17:30

0 Answers0