5 Replies - 18582 Views - Last Post: 17 July 2012 - 05:50 AM Rate Topic: -----

#1 SkyFirez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-May 12

XML Exception: Data at the root level is invalid. Line 1, position 1

Posted 17 July 2012 - 05:33 AM

I started a project yesterday for me to make a on-line 'birthday' list file, that my brother could edit, and it would automatically refresh on the program. That bit works fine, today, I decided to make the on-line file XML instead of using a plain .txt file and splitting it with a character. Here is the code...

                    // Loads XML document.
                    xDoc.LoadXml("http://www.einyx.com/birthday-list.xml"); //Properties.Settings.Default.FilePathXML);
                    // Gets Name & Date.
                    info[0] = xDoc.SelectSingleNode("List/Information/Name").InnerText;
                    info[1] = xDoc.SelectSingleNode("List/Information/Date").InnerText;
                    // Uses the first string info to construct the 'Welcome' text.
                    richTextBox1.Text = "You are reading " + info[0] + "'s Birthday List for his Birthday on " + info[1];
                    // Uses the first string info to construct program title.
                    this.Text = info[0] + "'s Birthday List";
                    // Loops through each Item node.
                    foreach(XmlNode node in xDoc.SelectNodes("List/Item"))
                    {
                        // Creates a new string to pass through what we will construct.
                        string s;
                        // Constructs information like we did before.
                        s = node.SelectSingleNode("Title").InnerText + "" + node.SelectSingleNode("URL") + "" + node.SelectSingleNode("Description");
                        // Adds to list for the itemchanged event to use.
                        List1.Add(s);
                        // Adds to listbox.
                        listBox1.Items.Add(node.SelectSingleNode("Title").InnerText);



A few things, Properties.Settings.Default.FilePathXML is the web address to the XML File, I commented it out to try the direct address, no difference. At the end, I made it the long string so it would work with my already existing code for the .txt file (there is a radiobutton to choose what you wish to use, txt or xml, so I kept that code). My XML file is this:

<List>
    <Information>
        <Name>Test</Name>
        <Date>Test</Date>
    </Information>
    <Item>
        <Title>test_Title1</Title>
        <URL>test_URL1</URL>
        <Description>Test_description1</Description>
    </Item>
    <Item>
        <Title>test_Title2</Title>
        <URL>test_URL2</URL>
        <Description>Test_description2</Description>
    </Item>
    <Item>
        <Title>test_Title3</Title>
        <URL>test_URL3</URL>
        <Description>Test_description3</Description>
    </Item>
    <Item>
        <Title>test_Title4</Title>
        <URL>test_URL4</URL>
        <Description>Test_description4</Description>
    </Item>
    <Item>
        <Title>test_Title4</Title>
        <URL>test_URL4</URL>
        <Description>Test_description4</Description>
    </Item>
    <Item>
        <Title>test_Title5</Title>
        <URL>test_URL5</URL>
        <Description>Test_description5</Description>
    </Item>
</List>



Also, I'm pretty new to all this, started about a month ago, been watching TheNewBoston tutorials on C# (got to just after the Paint project videos). So I might not understand anything too complex. :)

Thanks, SkyFirez

Also, forgot to mention again in the topic, and couldn't find a edit button. The error I'm getting is
Data at the root level is invalid. Line 1, position 1.

and I have used breakpoints to find it comes from where I load the XML document.

Thanks again, SkyFirez.

Edit 2: Sorry again, another thing I forgot to mention is that the file is hosted on my web server on the root directory.

Is This A Good Question/Topic? 0
  • +

Replies To: XML Exception: Data at the root level is invalid. Line 1, position 1

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: XML Exception: Data at the root level is invalid. Line 1, position 1

Posted 17 July 2012 - 05:34 AM

Try adding the XML preamble to the top of the file:

<?xml version="1.0"?>

Was This Post Helpful? 0
  • +
  • -

#3 SkyFirez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-May 12

Re: XML Exception: Data at the root level is invalid. Line 1, position 1

Posted 17 July 2012 - 05:39 AM

View PostJackOfAllTrades, on 17 July 2012 - 05:34 AM, said:

Try adding the XML preamble to the top of the file:

<?xml version="1.0"?>


Thanks for the suggestion, tried that, still got the Exception, the code after adding should have been:
<?xml version="1.0"?>
<List>
    <Information>
        <Name>Test</Name>
        <Date>Test</Date>
    </Information>
    <Item>
        <Title>test_Title1</Title>
        <URL>test_URL1</URL>
        <Description>Test_description1</Description>
    </Item>
    <Item>
        <Title>test_Title2</Title>
        <URL>test_URL2</URL>
        <Description>Test_description2</Description>
    </Item>
    <Item>
        <Title>test_Title3</Title>
        <URL>test_URL3</URL>
        <Description>Test_description3</Description>
    </Item>
    <Item>
        <Title>test_Title4</Title>
        <URL>test_URL4</URL>
        <Description>Test_description4</Description>
    </Item>
    <Item>
        <Title>test_Title4</Title>
        <URL>test_URL4</URL>
        <Description>Test_description4</Description>
    </Item>
    <Item>
        <Title>test_Title5</Title>
        <URL>test_URL5</URL>
        <Description>Test_description5</Description>
    </Item>
</List>


right?

Actually, oops, I feel stupid, forgot to add the file with that preamble to the web server!
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: XML Exception: Data at the root level is invalid. Line 1, position 1

Posted 17 July 2012 - 05:39 AM

Oh.

Try reading the documentation:

Quote

Loads the XML document from the specified string


Is http://www.einyx.com/birthday-list.xml an XML string? NO.

You need to retrieve the document from the remote location and then use XmlDocument on it.
Was This Post Helpful? 0
  • +
  • -

#5 SkyFirez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-May 12

Re: XML Exception: Data at the root level is invalid. Line 1, position 1

Posted 17 July 2012 - 05:44 AM

Tried with the right file, still does work with preamble.

View PostJackOfAllTrades, on 17 July 2012 - 05:39 AM, said:

Oh.

Try reading the documentation:

Quote

Loads the XML document from the specified string


Is http://www.einyx.com/birthday-list.xml an XML string? NO.

You need to retrieve the document from the remote location and then use XmlDocument on it.

Oh, ok, from the XML tutorial on TheNewBoston it said just shove the path as a string in there, I'll try that, thanks :)

Oh, I got it, silly mistake, I should have used the Load method not the LoadXml method, thanks for your help!

Someone can close this thread or something, thanks ::)
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: XML Exception: Data at the root level is invalid. Line 1, position 1

Posted 17 July 2012 - 05:50 AM

Not having done C# in a while, does Load actually download the data? If so, very cool. If not, then you might get help from this topic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1