Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Windows' started by GarthSmith, Mar 4, 2014.

  1. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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!
     
    Last edited: Mar 18, 2014
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I also get the same thing for System.IO.StringReader.Close().
    These are the current two compiler warnings I get when trying to build.
     
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Solved. It was a compile setting.

    Build Settings -> Player Settings -> Publishing Settings -> Compilation Overrides -> Changed from "Use Net Core" to "None"
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,710
    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.
     
    Mikael-H likes this.
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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.
    Code (csharp):
    1.  
    2. void LoadXml(string text) {
    3.   StringReader stringReader = new StringReader(text);
    4.   XmlReader xmlReader = XmlReader.Create (stringReader);
    5.   // ERROR here!
    6.   xmlReader.Dispose();
    7. }
    8.  
    The only other overloaded method I see is a protected Dispose(bool) method. What am I misunderstanding here?
     
    Last edited: Mar 17, 2014
  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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?
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,628
    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.
     
  8. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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.
    Code (csharp):
    1.  
    2. #if NETFX_CORE
    3. using XmlReader = WinRTLegacy.Xml.XmlReader;
    4. #else
    5. using XmlReader = System.Xml.XmlReader;
    6. #endif
    7.  
    8. public class SomeClass : MonoBehaviour
    9. {
    10.  
    11.   public void ParseXml(string xmlText) {
    12.     StringReader stringReader = new StringReader(text);
    13.     XmlReader xmlReader = XmlReader.Create(stringReader);
    14.  
    15. #if NETFX_CORE
    16.     xmlReader.Dispose();
    17. #else
    18.     xmlReader.Close();
    19. #endif  
    20.  
    21.   }
    22.  
    23. }
     
    Last edited: Mar 18, 2014
  9. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,710
    XmlReader in WinRTLegacy should have Close() method, so the second #if can be avoided.
     
  10. bakno

    bakno

    Joined:
    Mar 18, 2007
    Posts:
    603
    Hi Garth

    How do you handle the lack of StringReader?

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

    Regards
    Andres
     
  11. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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.
     
    Last edited: Apr 1, 2014
  12. musikit

    musikit

    Joined:
    Jan 30, 2012
    Posts:
    160
    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.
     
  13. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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. >.>
     
    Last edited: Apr 1, 2014
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,628
  15. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
  16. NeoSu

    NeoSu

    Joined:
    Aug 11, 2013
    Posts:
    12
    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;

    ...
     
    Last edited: Oct 26, 2014
  17. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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