none
unexpected token. The expected token is '='.

    Pergunta

  • Hi I am running below code and I am getting below error,

    'ID' is an unexpected token. The expected token is '='. Line 1, position 15.

    MY code is below.

     string ss = "<item product ID='124390'>" +

                           "<price>5.95</price>" +
                           "</item>";

                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                settings.IgnoreProcessingInstructions = true;
                settings.IgnoreWhitespace = true;
                XmlReader xmlReader = XmlReader.Create(new StringReader(ss), settings);

                while (xmlReader.Read())
                {

                }

    I know this error coming with space before ID. How to resolve this issue.

    thank you

    Amitsp


    Amitsp

    quinta-feira, 12 de abril de 2012 09:09

Respostas

  • I want to use space how can i use space.

    "<item product ID='124390'><price>5.95</price></item>";


    If you want to use XML then you have to follow the XML syntax rules and they don't allow spaces in names so you can use "product_ID" or "product-ID" as an attribute name but not "product ID".

    Another option is the escape the space for instance

      XmlConvert.EncodeLocalName("product ID")

    gives

      product_x0020_ID

    That's all I can suggest, a space itself is not allowed in names in XML documents.


    MVP Data Platform Development My blog

    • Sugerido como Resposta Petalvik quinta-feira, 12 de abril de 2012 11:51
    • Marcado como Resposta Alan_chenModerator terça-feira, 17 de abril de 2012 03:39
    quinta-feira, 12 de abril de 2012 09:42

Todas as Respostas

  • The task of XmlReader is to tell you whether the input it parses is well-formed XML or not; in your case the input is not well-formed so you get the error reported, as any other XML parser would do as well. To resolve the issue you need to fix the input you pass to XmlReader. If you want to create XML without syntax errors then instead of using string concatenation consider to use XmlWriter.

    MVP Data Platform Development My blog

    quinta-feira, 12 de abril de 2012 09:25
  • thank you for reply.

    can you give example , because my xml reder is not well formed. means it like string ss== "<item product ID='124390'><price>5.95</price></item>";


    Amitsp

    quinta-feira, 12 de abril de 2012 09:28
  • Your input markup

      <item product ID='124390'><price>5.95</price></item>

    is not well-formed, you need to fix that using e.g

      <item product_ID='124390'><price>5.95</price></item>


    MVP Data Platform Development My blog

    quinta-feira, 12 de abril de 2012 09:31
  • I want to use space how can i use space.

    "<item product ID='124390'><price>5.95</price></item>";


    Amitsp

    quinta-feira, 12 de abril de 2012 09:33
  • I want to use space how can i use space.

    "<item product ID='124390'><price>5.95</price></item>";


    If you want to use XML then you have to follow the XML syntax rules and they don't allow spaces in names so you can use "product_ID" or "product-ID" as an attribute name but not "product ID".

    Another option is the escape the space for instance

      XmlConvert.EncodeLocalName("product ID")

    gives

      product_x0020_ID

    That's all I can suggest, a space itself is not allowed in names in XML documents.


    MVP Data Platform Development My blog

    • Sugerido como Resposta Petalvik quinta-feira, 12 de abril de 2012 11:51
    • Marcado como Resposta Alan_chenModerator terça-feira, 17 de abril de 2012 03:39
    quinta-feira, 12 de abril de 2012 09:42
  • How can I write well format xml using my string<item product ID='124390'><price>5.95</price></item>?


    Amitsp

    quinta-feira, 12 de abril de 2012 09:49
  • How can I write well format xml using my string<item product ID='124390'><price>5.95</price></item>?


    Well you can't, XML has syntax rules and space characters are not allowed in names so the markup you are presenting is not XML and if you want to use XML you have to follow its syntax rules. I have pointed out several ways XML allows and I have also made it clear that the space is not allowed but you seem to come back each time with the same mal-formed markup.

    So to repeat, if you want to use XML tools you need to use input that is well-formed XML, your sample is not, so you need to change the input sample.


    MVP Data Platform Development My blog

    quinta-feira, 12 de abril de 2012 10:37