问题
I'm trying to parse an xml document using the following:
XmlDocument doc = new XmlDocument ();
doc.LoadXml (myXMLstring);
I get the error:
The type or namespace name 'XmlDocument' could not be found. Are you missing an assembly reference?
..even though I have using System.Xml;
I'm not sure what an assembly reference is. I'm using Xamarin Studio. I see there are several folders for references. In the references folder for the base project I don't see an entry for System.Xml
But in individual platforms I do see System.Xml in there.
What can I do to get this error line to clear?
回答1:
As I posted on the Xamarin forums: http://forums.xamarin.com/discussion/46042/missing-assembly-reference-for-system-xml#latest
Use XDocument.Parse(string)
to accomplish this from your Portable Class Library.
XDocument is included in the System.Xml.Linq
namespace so you will need to add the using directive for that namespace in your class.
The reason that you cannot access XmlDocument
from a Portable Class Library (PCL) is due to the fact that a PCL will only expose APIs that are supported on all platforms that the PCL targets. Windows Phone 8 does not support XmlDocument, and so this isn't available in the PCL. Windows Store apps support XmlDocument but it is available in a different namespace (Windows.Data.Xml.Dom
). So for this reason System.Xml.XmlDocument
cannot be used from the PCL. Anything else provided through the System.Xml
namespace in the PCL is fine to use.
回答2:
You can use the following steps:
- Under the solution explorer on the left, right click on References and select Edit References
- Go to .Net Assembly or All Tab and pick the System.Xml
- Click OK.
回答3:
Check if System.Xml is referenced in your project. Unfortunately I don't know how to do it in Xamarin Studio. In Visual Studio if you expand your project you will see References directory. If System.Xml is not there you can easily add it there by right-clicking on References and selecting Add Reference. After that you will be able to use XmlDocument class and entire content of System.Xml assembly.
回答4:
Just add to source
using Windows.Data.Xml.Dom;
来源:https://stackoverflow.com/questions/31494879/the-type-or-namespace-name-xmldocument-could-not-be-found-are-you-missing-an