when i try to read an xml file from a third party company, i get the error:
Data at the root level is invalid. Line 1, position 1.
i've read on google the problem could be because the data of the xml document is utf-8 and String only accepts utf-16.
but i can't find a proper solution. I read the xml file from an url.
this is the code i wrote:
private void GetBlockList(DateTime lastUpdate, string username, string password)
{
List<String> m_list = new List<String>();
HttpWebRequest blockListRequest = (HttpWebRequest)WebRequest.Create(string.Format("https://www.apiemail.net/api.aspx?Username={0}&Password={1}&Function=get_blocklist&SID=4", username, password));
HttpWebResponse blockListResponse = (HttpWebResponse)blockListRequest.GetResponse();
XmlDocument blockListXmlDoc = new XmlDocument();
XmlNode root = blockListXmlDoc.DocumentElement;
XmlNodeList blockNodeList = root.SelectNodes("blockedemail");
blockListXmlDoc.Load(blockListResponse.GetResponseStream());
int count = 0;
while (blockNodeList.Count < count)
{
m_list.Add(blockNodeList.Item(count).SelectSingleNode("address").InnerText);
count++;
}
return m_list;
}
first few lines op xml: (note it's a quite large xml.)
<?xml version="1.0" encoding="ISO-8859-1"?>
<blockedemails>
<blockedemail>
<address>email</address>
<date>6/4/2011 12:11:14 AM</date>
</blockedemail>
<blockedemail>
<address>email</address>
<date>6/6/2011 1:39:04 PM</date>
</blockedemail>
<blockedemail>
<address>email</address>
<date>4/23/2011 8:56:06 PM</date>
</blockedemail>
We (middelpat and me) work at the same company and figured out why it gave the error.
We wrote the response we got to a file and look into that file. There was a error message instead of the xml.
Apiemail works with a trusted IP. If your ip is not trusted you get a plain text saying your not allowed and that would bring a error on line 1 position 1. Cause that is not xml. We will now add the ip to the trusted list and work on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With