There is an error in XML document (2, 2).What does this mean?
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).
Related videos on Youtube
Losser Bad
Updated on May 13, 2021Comments
-
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 over 9 yearsI 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 over 9 yearsNo only this xml document...
-
Steve Wellens over 9 yearsAdd 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 over 9 yearsProblem is your instantiating an
XmlManager
of typeGameScreen
but trying to load an XML file with typeSplashScreen
in it. Maybe. I'm not sure because I do not understand the semantic difference betweenT
andType
in your code. Perhaps showing those two extra types would be helpful. This code works for me ifSplashScreen
is a subclass ofGameScreen
. -
Losser Bad over 9 yearsyes SplashScreen is a subclass of GameScreen
-
Amadeus Sánchez over 7 yearsJust 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 over 6 yearsFrom 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 about 6 yearsalso try deleting any
*.suo
files -
manit about 6 yearsI delete the .sln file and the project loaded. It looks like there was an encoding error in the sln file.
-
Jesse Sierks about 6 yearsI 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 about 6 yearsI'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 almost 6 yearsI 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 over 5 yearsI had the same issue, when I tried to open (by mistake) solution from VS2015 instead of VS2017.
-
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 about 5 years@mmcrae exactly