我试图用Linq(我恨Linq...主要因为我时间不多去了解它是如何运作的,因此,我总是在事情出错时迷惑不解。我希望有一个很好的简单方法来使用XSD或某样东西,以及某种消毒剂和再iririzer...但似乎在.net中没有任何本地的支持。所以我想,为什么不只是使用Linq?大错误。但无论如何,我想改变这个字符串...
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfPopulation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Population>
<id>3</id>
<name>CHF</name>
<description>FR Congestive Heart Failure</description>
<createdDate>2011-10-25T04:00:00Z</createdDate>
<createdUserID>WPS_ANEMIA_POP_OWNER</createdUserID>
<modifiedDate>2011-10-31T04:00:00Z</modifiedDate>
<modifiedUserID>WPS_ANEMIA_POP_OWNER</modifiedUserID>
<isActive>true</isActive>
<owner>
<type>AD GROUP</type>
</owner>
<owner>
<type>ACCOUNT</type>
</owner>
<type>
<name>ACO SURVEY</name>
</type>
</Population>
<Population>
<id>2</id>
<name>COPD</name>
<description>FR Chronic Obstructive Pulmonary Disease</description>
<createdDate>2011-10-25T04:00:00Z</createdDate>
<createdUserID>WPS_ANEMIA_POP_OWNER</createdUserID>
<modifiedDate>2011-10-31T04:00:00Z</modifiedDate>
<modifiedUserID>WPS_ANEMIA_POP_OWNER</modifiedUserID>
<isActive>true</isActive>
<owner>
<domain>1UPMC-ACCT</domain>
<name>InteropDev</name>
<type>AD GROUP</type>
</owner>
<owner>
<domain>1UPMC-ACCT</domain>
<name>ACOPopulationUsers</name>
<type>AD GROUP</type>
</owner>
<owner>
<domain>1UPMC-ACCT</domain>
<name>muesml</name>
<type>ACCOUNT</type>
</owner>
<owner>
<domain>1UPMC-ACCT</domain>
<name>andersonjm</name>
<type>ACCOUNT</type>
</owner>
<type>
<name>ACO SURVEY</name>
</type>
</Population>
</ArrayOfPopulation>
进入这些对象的阵列...
public class PopulationModel
{
public string populationID { set; get; }
public string PopName { set; get; }
public string Description { set; get; }
public int PopulationType { set; get; }
public int isActive { set; get; }
//public List<XSLTACOData> patients { set; get; }
}
这看起来有点像...
public class PopulationsModel
{
public List<PopulationModel> Populations { set; get; }
}
我无法用这个 Linq 到 XML 查询...
XDocument xDocument = XDocument.Parse(xmlFromSvc);
XElement elem = xDocument.Element("ArrayOfPopulation");
//client.GetOwnedPopulations();
IEnumerable<PopulationsModel> popModel = (IEnumerable<PopulationsModel>)
(from templates in elem.Elements("Population")
select new PopulationModel
{
populationID = templates.Attribute("id").Value,
PopName = templates.Attribute("name").Value,
Description = templates.Attribute("description").Value,
PopulationType = int.Parse(templates.Attribute("").Value),
isActive = int.Parse(templates.Attribute("isActive").Value)
}).ToList();
我被正式搞糊涂了。为什么地球上没有做这个工作?此外,必须有一个比较容易的方法来完成这个任务?这似乎对xsds和所有其他的废话来说是完美的。我也许在这里努力工作?
现在得到这个例外...
Unable to cast object of type System.Collections.Generic.List`1[FocusedReadMissionsRedux.Models.PopulationModel] to type System.Collections.Generic.IEnumerable`1[FocusedReadMissionsRedux.Models.PopulationsModel] .
这似乎是一种略为合理合理的例外。