简体   繁体   中英

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

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.

I tried loading an xml by parsing a string from another XDocument and was getting the same error.

XDocument xDocData = XDocument.Parse(xDoc.Element("SecuredWebService").Element("data").Element("Approved").Value);

It turned out that doing this ".Value" generated an xml with a space at the beginning, this was the source of my error. I solved it by using ".ToString()" instead.

XDocument xDocData = XDocument.Parse(xDoc.Element("SecuredWebService").Element("data").Element("Approved").ToString());

Try using the 'System.Xml.Linq;' namespace.

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();
XDocument doc = new XDocument(blockListRequest);

and then just by calling the

IEnumerable<XElement> elements = doc.Descendants("blockedemail");

will return you the collection of 'XElement', which you can use to iterate through. There are methods like

foreach(var element in elements)
{
   element.GetElement("address").Value;
}

Doing so I've never had any problems with reading *.xml files. Here's a reference to some tutorial that might be useful:

http://www.techrepublic.com/blog/programming-and-development/access-xml-data-using-linq-to-xml/594 http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM