How to use XDocument class in Mono

给你一囗甜甜゛ 提交于 2020-01-07 08:47:13

问题


I'm trying to use XDocument class inside a Unity3D project on Windows 7.

I did the following:

  • added the reference System.Xml.Linq to the Mono project.
  • included the namespace:

    using System.Xml.Linq;
    
  • set the target framework to: Mono/.NET 3.5
  • clean and rebuild the project

But still Unity3D complains about it. Here's the error output in the console:

Assets/Scripts/Editor/RoadManager/RoadManager.cs(3,18): error CS0234: The type or namespace name `Linq' does not exist in the namespace `System.Xml'. Are you missing an assembly reference?

Any idea?


回答1:


This has been discussed many times before, but few of these answers are complete.

As has been said before, Unity3d only supports up to .NET version 2.0, and it seems System.Xml.Linq was introduced in .NET 3.5, besides the fact that it is not listed on the Unity3d compatibility list anywhere.

The only things to try are to set the Mono API compatibility level to 2.0 (Menu: Edit > Project Settings > Player and look in the Other Settings panel), but it seems that that was a mistaken solution for Linq2SQL.

Another possible solution is to add the DLL yourself into the Unity Editor as shown:

Try dragging the C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0\System.Xml.Linq.dll file into the unity project window like you would a texture or other game asset.

If none of these yield ANY luck for you, then I'm afraid you are out of luck.




回答2:


Unity3D supports .Net 2.0 only, so setting compatibility to .net 3.5 in MonoDevelop/Visual Studio will not work. You will have to make do without the class. This shows what library classes are available:

http://docs.unity3d.com/Documentation/ScriptReference/MonoCompatibility.html

See also this post in UnityAnswers: http://answers.unity3d.com/questions/46039/can-not-reference-systemxmllinq.html



来源:https://stackoverflow.com/questions/14588515/how-to-use-xdocument-class-in-mono

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!