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?
The problem is that your responsePayLoad string is not valid XML.
Your problem is here:
The presence of the quote character at the start and end of the string, and the backslashes before the quotes, is causing the XML to be malformed. If your string did not have these quotes at the start and end and the backslashes the XML will be valid i.e. This will make the XML well-formed:
As for why this problem is occurring, it could be because of the way that you are creating your XDocument. The example in Microsoft's documentation here, indicates that an XDocument which uses a specific XDeclaration should be instantiated using the following constructor:
So I'd try re-factoring your code to make us of this method e.g.
If this does not work also try turning off the disable formating in CreateXDoc i.e.