Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when reading XML

Tags:

c#

xml

I am currently writing an XML writer/reader. I have it writing to the xml file, now I am attempting to read from it. However, when I do so the following error is thrown and I am not sure why:

'>' is an unexpected token. The expected token is '='. Line 6, position 16. XML reader c#

Please could someone shed some light on this for me?

The XML file:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<assignments>
  <assignment>
    <ModuleTitle>Internet Programming</ModuleTitle>
    <AssignmentTitle>Assignment 01</AssignmentTitle>
    <Date Given>11/02/2015</Date Given>
    <Date Due>20/02/2015</Date Due>
  </assignment>
</assignments>

UPDATE:

The problem was the fact that in some of my tag names I had spaces, which was causing the error.

like image 309
Callum Holden Avatar asked Feb 20 '26 07:02

Callum Holden


2 Answers

You have invalid spaces, the following will work:

XElement config = XElement.Parse (
@"<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<assignments>
  <assignment>
    <ModuleTitle>Internet Programming</ModuleTitle>
    <AssignmentTitle>Assignment 01</AssignmentTitle>
    <DateGiven>11/02/2015</DateGiven>
    <DateDue>20/02/2015</DateDue>
  </assignment>
</assignments>");

Please note DateGiven and DateDuewithout spaces.

The spaces are the reason for the error as shown below:

enter image description here

like image 171
hutchonoid Avatar answered Feb 21 '26 21:02

hutchonoid


<Date Given> is not a valid XML syntax. Given is supposed to be an attribute with a value, so it should look something like this: <Date Given="true">

Edit to be useful in the future: as @James mentioned, it is just a space in the tag name, which is also invalid in XML.

like image 22
nXu Avatar answered Feb 21 '26 22:02

nXu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!