maurodefilippis
maurodefilippis

Reputation: 19

System.XML on XAMARIN forms project

I am trying to use XmlDocument class ** and **XmlDocument .Load(..) function on the Portable Project of XAMARIN.Forms Portable solution with visual studio community.

The compiler says that "The type or namespace name 'XmlDocument' could not be found (are you missing a using directive or an assembly reference?"

If i go in References it don't allow me to add the System.XML namespace (there is no) and if i browse file and go to system.xml.dll it says me that the file could not be added because this component is already automatically referenced by build system.

what i can to do to use the class??

NOTE: in .Droid and .IOS project there is a referenc to System.xml and in those projects I can use XmlDocument class.

Upvotes: 1

Views: 2355

Answers (3)

charles young
charles young

Reputation: 2289

I had no trouble adding XML to my project:

using System.Xml;
using System.Xml.Serialization;

   public string ToXML(Object oObject)
   {
      XmlDocument xmlDoc = new XmlDocument();
      XmlSerializer xmlSerializer = new XmlSerializer(oObject.GetType());
      using (MemoryStream xmlStream = new MemoryStream())
      {
         xmlSerializer.Serialize(xmlStream, oObject);
         xmlStream.Position = 0;
         xmlDoc.Load(xmlStream);
         return xmlDoc.InnerXml;
      }
   }

After that the XML string can be shared:

   public MvxCommand ShareWaypoints => new MvxCommand(ShareWaypointsAsync);
   public async void ShareWaypointsAsync()
   {
        try
        {
           string strXML = "";
           foreach (var wp in waypoints)
           {
              strXML += ToXML(wp); 
           }
           if (strXML != "")
              await Share.RequestAsync(new ShareTextRequest
                                       {
                                          Text = strXML,
                                          Title = "Share Text"
                                       });
        }
        catch (Exception ex)
        {
            await _userDialogs.AlertAsync(ex.Message);
        }
   }

Upvotes: 0

Jason
Jason

Reputation: 89082

PCL doesn't support XmlDocument. You can use System.Xml.Linq.XDocument instead.

Upvotes: 2

Mage Xy
Mage Xy

Reputation: 1803

The XmlDocument class is not available for use in a PCL library, as you can see on its documentation page under Version Information. (Compare to the Version Information section of the XmlDictionary class - notice how this class has Portable Class Library available while XmlDocument does not.)

If you want to use an XmlDocument, you'll have to create a dependency service and implement it separately under both Android and iOS versions.

Upvotes: 0

Related Questions