Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data at the root level is invalid. Line 1, position 1. while reading xml

Tags:

c#

xml

asp.net

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>
like image 485
middelpat Avatar asked Sep 16 '25 11:09

middelpat


1 Answers

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.

like image 57
SynerCoder Avatar answered Sep 18 '25 10:09

SynerCoder