简体   繁体   中英

c# Linq to XML to Object not working in XAMARIN (ios)

i have a problem to get an xml result from a webservice into a custom class in xamarin (ios).

I've calling a Webservice and get the following result:

<?xml version="1.0" encoding="utf-8" ?> 
<ArrayOfCO_App_Table_CO_VERTRETERFTPLOGIN xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<CO_App_Table_CO_VERTRETERFTPLOGIN>
    <id>2</id> 
    <PERSONAL>max.mustermann</PERSONAL> 
    <FTP>127.0.0.1</FTP> 
    <USERNAME /> 
    <PASSWORD /> 
    <DIRECTORY /> 
    <ACTIVE>1</ACTIVE> 
    <USERDOMAIN /> 
    <ZIPFILENAME>bla.zip</ZIPFILENAME> 
    <DBFILENAME>bla.db</DBFILENAME> 
</CO_App_Table_CO_VERTRETERFTPLOGIN>
</ArrayOfCO_App_Table_CO_VERTRETERFTPLOGIN>

I want to insert it into a class.

  public class co_vertreterftplogin
 {
public string id { get; set; }
public string INVERT { get; set; }
public string PERSONAL { get; set; }
public string FTP { get; set; }
public string USERNAME { get; set; }
public string PASSWORD { get; set; }
public string DIRECTORY { get; set; }
public string ACTIVE { get; set; }
public string USERDOMAIN { get; set; }
public string ZIPFILENAME { get; set; }
public string DBFILENAME { get; set; }
}

My Code:

var xmldoc = XDocument.Parse(result);
XElement dx = xmldoc.Elements().FirstOrDefault();

var data = (from item in dx.Elements().FirstOrDefault().Descendants()
select new Database.Tabellen.co_vertreterftplogin
{
        id =item.Element("id").Value,
        PERSONAL="",
        FTP="",
        USERNAME="",
        PASSWORD="",
        DIRECTORY="",
        ACTIVE="",
        USERDOMAIN="",
        ZIPFILENAME="",
        DBFILENAME=""

});

Database.Tabellen.co_vertreterftplogin bla = data.First ();

My problem is that the data.First() is allways null. Or in other words, "my code is not working" :-)

Thx forward

There are a couple of problems here. Firstly, I believe you've got too many calls to Elements / Descendants so you're actually iterating over all the descendants of <CO_App_Table_CO_VERTRETERFTPLOGIN> . Next, you haven't specified the namespace when you fetch the id element. Due to this attribute in the root element:

xmlns="http://tempuri.org/"

all the rest of the elements are in that namespace. So you want:

XNamespace ns = "http://tempuri.org/";
var doc = XDocument.Parse(result);
var data = doc.Root       // ArrayOfCO_App_Table_CO_VERTRETERFTPLOGIN
              .Elements() // CO_App_Table_CO_VERTRETERFTPLOGIN
              .Select(x => new Database.Tabellen.co_vertreterftplogin
              {
                  id = (string) x.Element(ns + "id"),
                  ...
              });

I would also strongly advise you to revisit the naming of both your type and properties to follow .NET naming conventions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM