There is an error in XML document (2, 2).What does this mean?

65,240

Solution 1

My experience from it would be that in the 2nd line in the 2nd chararacter, there is an error. have a look if your class names are different from the XML tags. are you maybe changing the "XML Root name" to a different one.

Have a look at the XML structure and which class are you serializing to which node.

Also, read the MSDN Documentation about the XmlRootAttribute Class.

Solution 2

In my case it appears one of the Visual Studio 2017 version 15.5 updates caused this error when trying to open SSRS projects. The solution is to delete the *.rptproj.rsuser file from the project folder and try again.

Solution 3

That usually means you have whitespace at the start of the file; check for a line-break before the <?xml.... Even better: please show the first few bytes (preferably as far as <SplashScreen) of the file as viewed in a binary editor.

It could also mean you have an invisible unicode or control character somewhere before the <SplashScreen

Solution 4

Just wanted to share what worked for me. I had a similar error

System.InvalidOperationException: There is an error in XML document (1, 40). ---> System.InvalidOperationException: <tsResponse xmlns='http://xxxyyyzzzz.com/api'> was not expected.

I was trying to deserialize a string to an object of type tsResponse.

After adding the following attribute [Serializable, XmlRoot(ElementName = "tsResponse", Namespace = "http://xxxyyyzzzz.com/api")] to the class tsResponse i was able to resolve my issue.

[Serializable, XmlRoot(ElementName = "tsResponse", Namespace = "http://xxxyyyzzzz.com/api")]
public class tsResponse
{
    [XmlElement]
    public CredentialsXml credentials { get; set; }
}

I.e had to add Namespace attribute (System.Xml.Serialization).

Share:
65,240

Related videos on Youtube

Losser Bad
Author by

Losser Bad

Updated on May 13, 2021

Comments

  • Losser Bad
    Losser Bad about 3 years

    I am trying to read XML document. My XML:

    <?xml version="1.0" encoding="utf-8"?>
    <SplashScreen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Path>SplashScreen/Image-King</Path>
    </SplashScreen>
    

    My code which is reading XML:

    XmlGameScreen = new XmlManager<GameScreen>();
    XmlGameScreen.Type = currentscreen.Type;
    currentscreen = XmlGameScreen.Load("LoadXML/SplashScreen.xml");
    

    And

    public Type Type;
    public T Load(string path)
    {
        T instance;
        using (TextReader textreader = new StreamReader(path))
        {
    
            XmlSerializer xml = new XmlSerializer(Type);
            instance = (T)xml.Deserialize(textreader);
        }
        return instance; 
    }
    

    I am getting error on instance = (T)xml.Deserialize(textreader); Is my XML document wrong? I am trying to read <Path>. Update : My Internal Exception: Cannot serialize member 'MyRPGgame.SplashScreen._image' of type 'Microsoft.Xna.Framework.Graphics.Texture2D'

    • Steve Wellens
      Steve Wellens over 9 years
      I think (2, 2) means the error is in line 2, column 2. But the XML document looks OK. Are you sure you aren't loading something else?
    • Losser Bad
      Losser Bad over 9 years
      No only this xml document...
    • Steve Wellens
      Steve Wellens over 9 years
      Add a line of code (textreader.ReadToEnd()) to put the output of the text reader into a string and use the debugger to examine it. (As a sanity check).
    • Jesse C. Slicer
      Jesse C. Slicer over 9 years
      Problem is your instantiating an XmlManager of type GameScreen but trying to load an XML file with type SplashScreen in it. Maybe. I'm not sure because I do not understand the semantic difference between T and Type in your code. Perhaps showing those two extra types would be helpful. This code works for me if SplashScreen is a subclass of GameScreen.
    • Losser Bad
      Losser Bad over 9 years
      yes SplashScreen is a subclass of GameScreen
    • Amadeus Sánchez
      Amadeus Sánchez over 7 years
      Just as a side note to fellow readers in the future: after a while struggling with these issues I decided to switch to JSON. If you a can do so in your project as well and it suits your interests just three words: go for it.
  • Admin
    Admin over 6 years
    From my experience, this error means that the root element has another Tag name than expected. The actual tag name begins on line 2, character 2.
  • Liam
    Liam about 6 years
    also try deleting any *.suo files
  • manit
    manit about 6 years
    I delete the .sln file and the project loaded. It looks like there was an encoding error in the sln file.
  • Jesse Sierks
    Jesse Sierks about 6 years
    I didn't have an rsuser file as the project was coming from TFS. So I created a new SSRS project, found the differences in it vs the project I could not open (which were just XML namespaces), changed the project I couldn't open, and then opened that project.
  • Fer R
    Fer R about 6 years
    I've tried delete some files as this answer suggest. A workaround which works for me was create a new SSRS project, then add all existent elements.
  • user2444499
    user2444499 almost 6 years
    I deleted both *.rptproj.ruser and *.rptproj.user and this fixed the issue for me. I had the same issue as @user875318, trying to open an SSRS solution in Visual Studio 2017 version 15.5.7.
  • Yury Kozlov
    Yury Kozlov over 5 years
    I had the same issue, when I tried to open (by mistake) solution from VS2015 instead of VS2017.
  • Don Cheadle
    Don Cheadle over 5 years
    @YuryKozlov - sounds like in other words, you had a 2017 project, but were trying to open it in VS 2015 and thus an error. That was how I got this error (someone upgraded it from 2015 to 2017 without telling others).
  • Yury Kozlov
    Yury Kozlov about 5 years
    @mmcrae exactly