Data at the root level is invalid. Line 1, position 1. Is it error with the xml format? - c#

Im having a problem with reading my xml with an error of Data at the root level is invalid. Line 1,position 1.
This is my code below:
string soapmessage = response.Content;
XmlDocument xml = new XmlDocument();
xml.LoadXml(soapmessage); //loading soap message as string
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
below attached my XML response

Related

How to get xml response sent to our webpage/url in C#

We would like to ask how to retrieve/get the xml response coming from a third party website. We just supply the redirectUrl and the xml response will be sent to it.
We have tried the code below to retrieve the xml details but it's not retrieving the data.
public ActionResult Index()
{
XmlDocument xdoc = new XmlDocument(); //xml doc used for xml parsing
xdoc.Load("http://xxx/Home/Index");
XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("/Data/Quote/Product"); //reading node so that we can traverse thorugh the XML
foreach (XmlNode xNode in xNodelst)//traversing XML
{
Sessions.PartNumber = xNode["PartNum"].InnerText;
}
return View();
}

XML from URL - Data at the root level is invalid. Line 1, position 1 Why it works with one URL and not the other?

As far as I can tell, these two end points are both valid XML output. However when I use the same code on the second end point I get the error:
Data at the root level is invalid. Line 1, position 1
Here is my code:
//Works
XmlDocument testDocument = new XmlDocument();
testDocument.Load("https://www.w3schools.com/xml/note.xml");
//Fails
XmlDocument testDocumentTwo = new XmlDocument();
testDocumentTwo.Load("https://www.domainNameHere.com/direct/umbraco/api/productsearch/NameSearch?countryCode=en-gb");
I opened Fiddler and watched the request and its response, and lo and behold your endpoint is returning JSON, not XML:
If I use HttpClient to set an explicit Accept header, then I get XML back and everything works:
using var client = new HttpClient();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, "https://www.clinigengroup.com/direct/umbraco/api/productsearch/NameSearch?countryCode=en-gb");
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
var response = await client.SendAsync(requestMessage);
var xml = await response.Content.ReadAsStringAsync();
XmlDocument testDocumentTwo = new XmlDocument();
testDocumentTwo.LoadXml(xml);

Unexpected XML declaration. The XML declaration must be the first node in the document

I am trying to get my xml response in the correct format using HttpUtility.Decode. Upon doing and loading it in Xml Document, it is throwing the following exception : Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 1, position 313. I'm not sure why because the xml declaration does appear as the first node if I'm not mistaken. Im fairly new to XML so any help will be appreciated. How do I fix this? Thanks.
XML decode response:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><Query_With_StringResponse xmlns="http://www.syspro.com/ns/query/"><Query_With_StringResult><?xml version="1.0" encoding="Windows-1252"?>
<ARListOfCustomers Language='05' Language2='EN' CssStyle='' DecFormat='1' DateFormat='01' Role='01' Version='7.0.005' OperatorPrimaryRole=' ' >
<QueryOptions>
<ReportSequence>CU</ReportSequence>
<PriceCode/>
<PriceProductmatrix>N</PriceProductmatrix>
<ExtraFields>N</ExtraFields>
<InterestExemptionStatusSelection>A</InterestExemptionStatusSelection>
<TaxExemptionSelection>A</TaxExemptionSelection>
<CustomerSelectionFilterType>A</CustomerSelectionFilterType>
<CustomerSelectionFilterValue/>
<CustomerClassSelectionFilterType>A</CustomerClassSelectionFilterType>
<CustomerClassSelectionFilterValue/>
<GeographicAreaSelectionFilterType>A</GeographicAreaSelectionFilterType>
<GeographicAreaSelectionFilterValue/>
<BranchSelectionFilterType>A</BranchSelectionFilterType>
<BranchSelectionFilterValue/>
<SalespersonSelectionFilterType>A</SalespersonSelectionFilterType>
<SalespersonSelectionFilterValue/>
<LineDiscountCodeSelectionFilterType>A</LineDiscountCodeSelectionFilterType>
<LineDiscountCodeSelectionFilterValue/>
<TermsSelectionFilterType>A</TermsSelectionFilterType>
<TermsSelectionFilterValue/>
<ProductCategorySelectionFilterType>A</ProductCategorySelectionFilterType>
<ProductCategorySelectionFilterValue/>
<InvoiceDiscountCodeSelectionFilterType>A</InvoiceDiscountCodeSelectionFilterType>
<InvoiceDiscountCodeSelectionFilterValue/>
<CurrencySelectionFilterType>A</CurrencySelectionFilterType>
<CurrencySelectionFilterValue/>
<CreditLimitSelectionFilterType>A</CreditLimitSelectionFilterType>
<CreditLimitSelectionFilterValue/>
</QueryOptions>
<Customer>
<CustomerListHeader>
<Customer>TSAR</Customer>
<CustomerName>TSAR BUSINESS SOLUTION</CustomerName>
<CustomerShortName>TSAR BUSINESS SOLUTI</CustomerShortName>
<Branch>TSAR</Branch>
<BranchDescription>HEAD OFFICE TSAR</BranchDescription>
<Geography>031</Geography>
<GeographyDescription>DURBAN</GeographyDescription>
<Class/>
<ClassDescription>** Not on file **</ClassDescription>
<BalanceType>Op-item</BalanceType>
<Sales>IVAN</Sales>
<CreditLimit> 0</CreditLimit>
<Currency>R</Currency>
<CurrencyDescription>Rand</CurrencyDescription>
<Telephone/>
<InvoiceTermsCode>CO</InvoiceTermsCode>
<TermsCodeDescription>CASH ON DELIVERY</TermsCodeDescription>
</CustomerListHeader>
<CustomerListDetails>
<Contact/>
<TaxNo>Tax No:</TaxNo>
<SpecialInstructions/>
<SoldToAddress1/>
<SoldToAddress2/>
<SoldToAddress3/>
<SoldToAddress3Loc/>
<SoldToAddress4/>
<SoldToAddress5/>
<SoldToAddress6/>
<SoldToGpsLat> 0.000000</SoldToGpsLat>
<SoldToGpsLong> 0.000000</SoldToGpsLong>
<ShipToAddress1>STRAUSS DALY</ShipToAddress1>
<ShipToAddress2>41 RICHFONT CRICLE</ShipToAddress2>
<ShipToAddress3>DURBAN</ShipToAddress3>
<ShipToAddress3Loc/>
<ShipToAddress4>KZB</ShipToAddress4>
<ShipToAddress5>SOUTH AFRICA</ShipToAddress5>
<ShipToAddress6>4000</ShipToAddress6>
<ShipToGpsLat> 0.000000</ShipToGpsLat>
<ShipToGpsLong> 0.000000</ShipToGpsLong>
<GSTNumber/>
<LineDiscCode/>
<InvDiscCode/>
<DefaultPriceCode/>
<CompanyTaxNumber/>
<ExemptFinChg>No finance charges</ExemptFinChg>
</CustomerListDetails>
</Customer>
<ReportSummary>
<NoOfCustomersListed> 1</NoOfCustomersListed>
</ReportSummary>
</ARListOfCustomers>
</Query_With_StringResult></Query_With_StringResponse></soap:Body></soap:Envelope>
My code: Code breaks on "doc.LoadXml(decodedXml);"
public async Task<string> CreateSoapEnvelop()
{
string soapString = #"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<Query_With_String xmlns=""http://www.syspro.com/ns/query/"">
<UserId>" + Settings.GUID + #"</UserId>
<BusinessObject></BusinessObject>
<XMLIn></XMLIn>
</Query_With_String>
</soap:Body>
</soap:Envelope>";
try
{
HttpResponseMessage response = await PostXmlRequest("http://sysprowebservices/query.asmx", soapString);
var soapResponse = await response.Content.ReadAsStringAsync();
var decodedXml = HttpUtility.HtmlDecode(soapResponse);
XmlDocument doc = new XmlDocument();
doc.LoadXml(decodedXml);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("ab", "http://www.syspro.com/ns/query/");
nsmgr.AddNamespace("bg", " https://bixg.choicepoint.com/webservices/3.0");
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
XmlNode xmlnode = doc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/ab:Query_With_StringResponse/ab:Query_With_StringResult", nsmgr);
string customer = xmlnode.SelectSingleNode("CustomerName").InnerText;
}
catch (Exception ex)
{
string msg = ex.Message;
}
return "";
}
public static async Task<HttpResponseMessage> PostXmlRequest(string baseUrl, string xmlString)
{
using (var httpClient = new HttpClient())
{
var httpContent = new StringContent(xmlString, Encoding.UTF8, "text/xml");
httpContent.Headers.Add("SOAPAction", "http://www.syspro.com/ns/query/Query_With_String");
return await httpClient.PostAsync(baseUrl, httpContent);
}
}
UPDATE
{
HttpResponseMessage response = await PostXmlRequest("http://196.37.159.30/sysprowebservices/query.asmx", soapString);
var soapResponse = await response.Content.ReadAsStringAsync();
XmlDocument doc = new XmlDocument();
doc.LoadXml(soapResponse);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("ab", "http://www.syspro.com/ns/query/");
nsmgr.AddNamespace("bg", " https://bixg.choicepoint.com/webservices/3.0");
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
XmlNode xmlnode = doc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/ab:Query_With_StringResponse/ab:Query_With_StringResult", nsmgr);
var xmlDecoded = HttpUtility.HtmlDecode(xmlnode.ToString());
}
Update Response from XmlDecode: "System.Xml.XmlElement"
First of all, don't roll your own SOAP client unless you know very well what you're doing. Can't you use WCF instead?
But you have a SOAP call that returns ... XML. You can't just decode the entire response as if it were HTML, because then the encoding of the inner XML will be lost, resulting in an invalid XML document.
You need to read the repsonse first, then obtain the inner XML string and then decode that:
XmlDocument doc = new XmlDocument();
doc.LoadXml(soapResponse);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("ab", "http://www.syspro.com/ns/query/");
nsmgr.AddNamespace("bg", " https://bixg.choicepoint.com/webservices/3.0");
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
XmlNode xmlnode = doc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/ab:Query_With_StringResponse/ab:Query_With_StringResult", nsmgr);
Now xmlnode holds the encoded XML. Decode that and do whatever you want with it:
var decodedXml = HttpUtility.HtmlDecode(xmlnode.InnerXml);

Web API Request Data error on second time

Strange Error.
var xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(this.Request.Content.ReadAsStreamAsync().Result);
var xmlDoc1 = new System.Xml.XmlDocument();
xmlDoc1.Load(this.Request.Content.ReadAsStreamAsync().Result);
In WEB API, I try to load the POST data in to xmlXoc it is working good
When I try to load it again in to xmlDoc1 (new variable), I am getting a Root Element missing error.
I see that ReadAsStreamAsync is a Read-Only-Stream but why the error on the last line ?
Save the Stream in a local variable and reset it to the beginning when reading it a second time.
var stream = this.Request.Content.ReadAsStreamAsync().Result
var xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(stream);
// RESET
stream.Position = 0;
var xmlDoc1 = new System.Xml.XmlDocument();
xmlDoc1.Load(stream);

Converting JSON to XML

I trying to convert JSON output into XML. Unfortunately I get this error:
JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.
This is what I up to now created.
string url = string.Format("https://graph.facebook.com/{0}?fields=posts.fields(message)&access_token={1}", user_name, access_token);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
jsonOutput = reader.ReadToEnd();
Console.WriteLine("THIS IS JSON OUTPUT: " + jsonOutput);
}
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonOutput);
Console.WriteLine(doc);
And this is my JSON output:
{"id":"108013515952807","posts":{"data":[{"id":"108013515952807_470186843068804","created_time":"2013-05-14T20:43:28+0000"},{"message":"TEKST","id":"108013515952807_470178529736302","created_time":"2013-05-14T20:22:07+0000"}
How can I solve this problem?
Despite the fact your JSON provided in the question is not complete, you have multiple properties at the top level as indicated by the exception. You have to define the root for it to get valid XML:
var doc = JsonConvert.DeserializeXmlNode(jsonOutput, "root");
EDIT: In order to print out your XML with indentation you can use XDocument class from System.Xml.Linq namespace: XDocument.Parse(doc.InnerXml).
I thought it's worth linking to the Documentation for turning xml to json and the other way around.
The guys are right..
// To convert an XML node contained in string xml into a JSON string
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
// To convert JSON text contained in string json into an XML node
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
You can do JSON-to-XML also by using the .NET Framework (System.Runtime.Serialization.Json):
private static XDocument JsonToXml(string jsonString)
{
using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(jsonString)))
{
var quotas = new XmlDictionaryReaderQuotas();
return XDocument.Load(JsonReaderWriterFactory.CreateJsonReader(stream, quotas));
}
}
DeserializeXmlNode returns XDcument.
If needed XNode use FirstNode.
//string jsonOutput="{"id":"108013515952807","posts":{"data":[{"id":"108013515952807_470186843068804","created_time":"2013-05-14T20:43:28+0000"},{"message":"TEKST","id":"108013515952807_470178529736302","created_time":"2013-05-14T20:22:07+0000"}";
var myelement= JsonConvert.DeserializeXmlNode(jsonOutput, "myelement").FirstNode;
Your shared JSON is invalid please go through http://jsonformatter.curiousconcept.com/ and validate your JSON first.
Yourt JSON should look like:
{
"id":"108013515952807",
"posts":{
"data":[
{
"id":"108013515952807_470186843068804",
"created_time":"2013-05-14T20:43:28+0000"
},
{
"message":"TEKST",
"id":"108013515952807_470178529736302",
"created_time":"2013-05-14T20:22:07+0000"
}
]
}
}
Adding on #jwaliszko's answer, converting json to XDocument:
XDocument xml = JsonConvert.DeserializeXNode(json);

Categories

Resources