I'm struggling with my XML output.
I have a method that takes in a PersonalID
and shall return address
, as well as some additional information from it.
Execute a API request and put separate values in string variables
public string ReturnRegistered(String personalID)
{
string apiurl = apiadress + personalID;
WebRequest request = WebRequest.Create(apiurl);
//TILLÄGG FÖR AUTHENTICATION
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(user, pass);
//TILLÄGG FÖR AUTHENTICATION
WebResponse respons = request.GetResponse();
Stream dataStream = respons.GetResponseStream();
//OBJEKT
XmlSerializer serializer = new XmlSerializer(typeof(List<PrimaryContact>), new XmlRootAttribute("QueueSlot"));
var input = (List<PrimaryContact>)serializer.Deserialize(dataStream);
string firstname = input[0].firstname;
string lastname = input[0].lastname;
string postalAddress = input[0].postalAddress;
string zipCode = input[0].zipCode;
string address = input[0].address;
//FÖR ATT KUNNA TA FRAM ENDAST ETT ATTRIBUT BEHÖVER MAN GÖRA OM HELA WEBREQUESTEN OCH NERÅT
WebRequest request2 = WebRequest.Create(apiurl);
//TILLÄGG FÖR AUTHENTICATION
request2.PreAuthenticate = true;
request2.Credentials = new NetworkCredential(user, pass);
//TILLÄGG FÖR AUTHENTICATION
WebResponse respons2 = request2.GetResponse();
Stream dataStream2 = respons2.GetResponseStream();
//ENVÄRDE
XmlDocument doc = new XmlDocument();
doc.Load(dataStream2);
XmlNodeList elemList2 = doc.GetElementsByTagName("lastPaymentDate");
string lastPaymentDate = elemList2[0].InnerText;
String returnvalue = "<Fields><Field><Name>lastPaymentDate</Name><Value>" + lastPaymentDate + "</Value></Field><Field><Name>Firstname</Name><Value>"+ firstname + "</Value></Field><Field><Name>Lastname</Name><Value>"+ lastname + "</Value></Field><Field><Name>Adress</Name><Value>"+ address + "</Value></Field><Field><Name>ZipCode</Name><Value>"+ zipCode + "</Value></Field><Field><Name>PostalAdress</Name><Value>"+ postalAddress + "</Value></Field></Fields>";
*//I have tryed start with <?xml version="1.0" encoding="UTF-8"?> but no different.*
return returnvalue;
}
Then my API uses that method and shall return a XmlElement
. "I have read on this site that I don't need to specify which element is Root and child and so on when using LoadXml"
Controller:
public XmlElement Get(string id)
{
Models.TomtkoRegistered tomtko = new Models.TomtkoRegistered();
String returnvalue = tomtko.ReturnRegistered(id);
XmlDocument doc = new XmlDocument();
doc.LoadXml(returnvalue);
XmlElement root = doc.DocumentElement;
return root;
}
If I print it to file I get it to work, but I don't know how to make the XML in the correct format for a webAPI so the format is usable by the receiver of my webAPI.
But when I use the API in Firefox or Chrome
It "looks" fine but when I look at the page source and try to use it, or add it to Notepad++ then everything is just on one line and not formatted or structured.
Can I force the Content-Type to be "text/xml" in some way? Did I use the wrong return type? Is it missing some other code?
When a friend used it in Postman
some action value was default set to anyvalue (*) in some way not 100% what it was but when he put it in XML it works. But how to I make the receiver understand from the beginning that it shall be XML if XmlElement
doesn't do the trick?