0

I know this is a silly question but I am receiving to my endpoint xml, upon receiving it I would like to extract a value from the xml and display it as a response. I am using the .Net Framework Web API project in Visual studio

Here is the xml someone is sending to my API endpoint:

<?xml version="1.0" ?>
<Message>
<ResultCode>3</ResultCode>
</Message>

The other challenge I have related to this question is how do I receive the XML above? Usually an xml file would be called via "doc.Load("c:\temp.xml");" but how do I receive the XML inside my API and from there extract the ResultCode value. This is the header of my API code:

public string Post(string incomingXML)
{

}
Ruan
  • 33
  • 7
  • maybe you're looking for this https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c – Uniquedesign Jul 04 '20 at 17:18
  • Thanks that helped already... The other challenge I have related to this question is how do I receive the XML above? In this link, they use "doc.Load("c:\\temp.xml");" as the file which contains the XML. How do I substitute that with the incoming POST on my API? – Ruan Jul 04 '20 at 17:23

1 Answers1

1

For the 1st question, you don't need a file, you can use

doc.LoadXml(@"<?xml version=""1.0"" ?>
<Message>
<ResultCode>3</ResultCode>
</Message>");

or

doc.LoadXml(incomingXML) for your case

About your 2nd question if it still actual. There is not enough information, but there is all options for you:

  • [the best solution] you can use Server.MapPath("~/path/your.xml") (or HttpContext.Current.Server.MapPath("~/path/your.xml")) - ~ here means root folder of your webapp. You need to have that xml in folder with webapp created manualy, or uploaded in future.

  • you can change Copy to Output Directory option to the 2nd or the 3rd, so it could be possible to access your file with doc.Load("your.xml"). You need to have that xml in folder with webapp in time of build.

Copy to Output Directory

  • [not required solution] you could let your IIS process to access other folders then your webapp is - right click on folder with your xml -> Properties -> Security -> Edit -> Add

iis_iusrs

Then write IIS_IUSRS at text area, click Check Names and Ok. Now you can specify Access

enter image description here

In future please create one question for one problem.

SiarheiK
  • 797
  • 4
  • 17
  • Thank you for the response. The main issue is not the XML file but how to load an incoming API call that has a XML body and then to use that in code. The "string incomingXML" in my code header is expecting a URL POST that is for example HTTP://A.A.com?incomingXML=.... however this XML is coming in the body and hence I need to extract it from body and then get the "ResposeCode" element – Ruan Jul 04 '20 at 19:28
  • I don't know how do you pass that to API, but here 2 options: add attribute to parameter `[FromBody]string incomingXML`. or, if body is json object, you have to create new `public class MyResponseModel{ public string incomingXML}` and you post become `Post([FromBody]MyResponseModel model)`. There are also option with `StreamReader`: `using (var reader = new StreamReader(Request.Body, ncoding.UTF8)) var textFromBody = await reader.ReadToEndAsync();` and your post will be with out parameters `string Post()` – SiarheiK Jul 04 '20 at 19:35
  • Thank you, if I add to my Post the '[FromBody] string incomingXML' how would I get the ResultCode value as a variable? – Ruan Jul 04 '20 at 19:40
  • I don't get your question. It should be in your `incomingXML` variable, and you will use `doc.LoadXml(incomingXML)`. It will work is your body string, but usualy it is json object so if incomingXML is null, then use 2nd method from my comment (with new class). – SiarheiK Jul 04 '20 at 19:48
  • Apologies for being unclear :-). Let me ask it this way. In the XML example above there is 3 inside the object . After I have added doc.LoadXml(incomingXML) how do I get the value of ? – Ruan Jul 04 '20 at 19:53
  • `doc.Descendants("ResultCode").FirstOrDefault();` – SiarheiK Jul 04 '20 at 19:59