System.Xml.XmlReader does not contain a definition for 'Close'

Hello!

I am working on a Windows Store port of a mobile game. Currently when I am building, I get this error in the Unity console:

Which is confusing because I’m looking at the System.Xml.XmlReader.Close() method on MSDN right now.

What causes this error? What do I need to do for a fix or work-around? Thanks!

I also get the same thing for System.IO.StringReader.Close().

These are the current two compiler warnings I get when trying to build.

Solved. It was a compile setting.

Build Settings → Player Settings → Publishing Settings → Compilation Overrides → Changed from “Use Net Core” to “None”

That’s not the right thing to do.
The right way is to call Dispose() instead of Close() (does the same). You can also use XmlReader from WinRTLegacy.Xml namespace.

As for Close() method for StringReader, use WinRTLegacy namespace, it has extention method.

1 Like

Hello!

Thanks for the reply Aurimas. I think I’m starting to figure a lot of the differences between Windows Store and iOS.

So now I am working on passing WACK, the Windows App Certification Kit. Which is requiring me to properly fix this now.

Setting the Compilation Overrides in the Build Settings from “Use Net Core” to “None” let me make a Win8.1 build, however we cannot pass Windows App certification like that. I’m figuring out that the parts of .NET available are restricted for Windows Store builds.

So looking at the XmlReader documentation, I see that the Close() method doesn’t have a green suitcase icon next to it, but the Dispose() method that Aurimas mentioned does have a green suitcase. Which, if I understand everything correctly, should mean that I can use Dispose() for Windows Store builds.

However, Unity is giving me an error on this code.

void LoadXml(string text) {
  StringReader stringReader = new StringReader(text);
  XmlReader xmlReader = XmlReader.Create (stringReader);
  // ERROR here!
  xmlReader.Dispose();
}

The only other overloaded method I see is a protected Dispose(bool) method. What am I misunderstanding here?

So it seems Dispose() has only existed since .NET 4.5. From what I can gather, Mono has not caught up to .NET 4.5 yet. I’m guessing this is why Unity doesn’t know about Dispose() and won’t let me build.

Can’t call Close() since it is not available to use in the Windows Store and can’t call Dispose() because Mono has not caught up to .NET 4.5 yet. What are my options?

  • I suppose I could comment the line out, create a Visual Studio Project, then edit the source through Visual Studio to call Dispose() before building.
  • Should I be using something other than XmlReader?
  • Some other option?

You should be able to set compilation overrides to “Use .net core” (which will make Unity use .NET For Windows Store compiler, rather than Mono) and then add “using XmlReader = WinRTLegacy.Xml.XmlReader” to the top of your source file.

Hello! Thanks again for the reply! I was able to get this working properly.

I also found some more info in this forum post about Unity 4.3.3 being released.

EDIT: To recap, Windows Store apps require some classes from .NET 4.5, which aren’t supported with Mono yet. So in the meantime, Unity is awesome and made some classes available through the WinRTLegacy namespace.

#if NETFX_CORE
using XmlReader = WinRTLegacy.Xml.XmlReader;
#else
using XmlReader = System.Xml.XmlReader;
#endif

public class SomeClass : MonoBehaviour
{

  public void ParseXml(string xmlText) {
    StringReader stringReader = new StringReader(text);
    XmlReader xmlReader = XmlReader.Create(stringReader);

#if NETFX_CORE
    xmlReader.Dispose();
#else
    xmlReader.Close();
#endif  

  }

}

XmlReader in WinRTLegacy should have Close() method, so the second #if can be avoided.

Hi Garth

How do you handle the lack of StringReader?

StringReader belongs to System.IO which is not available on Metro applications.

Regards
Andres

Hello bakno!

System.IO is actually available. StringReader is available too. The Close() method is not, but the Dispose() method is!

EDIT: I found no way to tell if a class/method was available or not from the documentation. The only way i could tell was to attempt a build and see what classes and methods the compiler errors mentioned.

if you look up the documentation on MS’s website there is notation

for example… your dispose method

“Version Information
.NET Framework
Supported in: 4.5.1, 4.5
Portable Class Library
Supported in: Portable Class Library
.NET for Windows Store apps
Supported in: Windows 8”

and your close method
“Version Information
.NET Framework
Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
.NET Framework Client Profile
Supported in: 4, 3.5 SP1”

if you see “Supported in: Windows 8” in the Version info it is safe to use for windows store apps and windows phone.

Hello musikit!

I have noticed that before. If I see “.NET for Windows Store apps / Supported in: Windows 8”, then I know for sure the class/method is allowed.

The inverse isn’t true. Even if that text is missing, the class/method may still be available.

Eg: someObject.GetType() is not allowed, but someObject.GetTypeInfo().GetType() IS allowed. I couldn’t find anything on MSDN that would indicate this. I gave up searching MSDN and got back to coding. >.>

:slight_smile:

Yes you’re right.

My confusion was from looking at the docs for Type.GetType() instead of Object.GetType(). D’oh!

Hello Garth
I’m getting this error when using the solution above

error CS0266: Cannot implicitly convert type ‘System.Xml.XmlReader’ to ‘WinRTLegacy.Xml.XmlReader’. An explicit conversion exists (are you missing a cast?)

Did this happened to you?

Edit: Here’s what I put on this top of my script:

#if NETFX_CORE
using XmlReader = WinRTLegacy.Xml.XmlReader;
#else
using XmlReader = System.Xml.XmlReader;
#endif

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System.Text;

It sounds like somewhere the compiler thinks you want the wrong XmlReader. Maybe you are calling a function from another class that returns System.Xml.XmlReader instead of WinRTLegacy.Xml.XmlReader or something like that.

I’ve since moved on from the Windows Store port. I did find this page which mostly states stuff you might already know.
http://docs.unity3d.com/Manual/windowsstore-missingtypes.html