I am receiving an XML string from the controller of my web API that is constructed as shown:
private string CreateXDoc(IEnumerable<PassedJSONConverted> passed)
{
XNamespace xmlns = "http://host.adp.com";
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
var jobListElement = new XElement(xmlns + "JobXML");
foreach (var objectItem in passed)
{
var loopElement = new XElement(xmlns + "JobsXML", new XElement(xmlns + "ID", objectItem.ID.ToString()), new XElement(xmlns + "Name", objectItem.Name), new XElement(xmlns + "Age", objectItem.Age.ToString()), new XElement(xmlns + "JobTitle", objectItem.JobTitle), new XElement(xmlns + "StartDate", objectItem.StartDate));
jobListElement.Add(loopElement);
}
doc.Add(jobListElement);
//Format without \n's
return doc.ToString(SaveOptions.DisableFormatting);
}
This is fine and the XML is set as shown:
- <JobXML xmlns="http://host.xxx.com">
- <JobsXML>
<ID>1</ID>
<Name>Dave</Name>
<Age>23</Age>
<JobTitle>Developer</JobTitle>
<StartDate>10/24/2013 6:40:28 AM</StartDate>
</JobsXML>
- <JobsXML>
<ID>2</ID>
<Name>John</Name>
<Age>44</Age>
<JobTitle>QA</JobTitle>
<StartDate>10/24/2013 6:40:28 AM</StartDate>
</JobsXML>
- <JobsXML>
<ID>3</ID>
<Name>Dave</Name>
<Age>23</Age>
<JobTitle>Senior Developer</JobTitle>
<StartDate>10/24/2013 6:40:28 AM</StartDate>
</JobsXML>
</JobXML>
When I then return this as a string and try to parse it back to an xDoc as shown:
private static string HandleResponse(HttpWebResponse httpResponse)
{
using (var responseReader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
{
string responsePayload = responseReader.ReadToEnd();
var newxDoc = XDocument.Parse(responsePayload);
return responsePayload;
}
}
The string 'responsePayLoad' at runtime is set as shown:
"<JobXML xmlns=\"http://host.adp.com\"><JobsXML><ID>1</ID><Name>Dave</Name><Age>23</Age><JobTitle>Developer</JobTitle><StartDate>10/24/2013 6:45:22 AM</StartDate></JobsXML><JobsXML><ID>2</ID><Name>John</Name><Age>44</Age><JobTitle>QA</JobTitle><StartDate>10/24/2013 6:45:22 AM</StartDate></JobsXML><JobsXML><ID>3</ID><Name>Dave</Name><Age>23</Age><JobTitle>Senior Developer</JobTitle><StartDate>10/24/2013 6:45:22 AM</StartDate></JobsXML></JobXML>"
This is giving me an exception at the 'newxDoc' object of:
XmlException is unhandled. Data at the root level is invalid. Line 1, position 1.
Can anyone tell me where I'm going wrong?