Linq を使用して XML 文字列を .net オブジェクトの通常のコレクションに変換しようとしています (私は linq が嫌いです...主に、それがどのように機能するかを正確に学ぶ時間がほとんどないためです。うまくいかない)。XSD か何かのような、ある種のデシリアライザーとリシリアライザーを使用する、素晴らしく簡単な方法があればいいのにと思います...しかし、.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 to 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();
私は正式に混乱しています。なぜこれがうまくいかないのですか?また、これを行うためのより簡単な方法が必要ですか?これは、xsd やその他のがらくたにとって完璧なもののようです。私はここで一生懸命働いているのでしょうか?
今、この例外を取得しています...
Unable to cast object of type 'System.Collections.Generic.List`1[FocusedReadMissionsRedux.Models.PopulationModel]' to type 'System.Collections.Generic.IEnumerable`1[FocusedReadMissionsRedux.Models.PopulationsModel]'.
これは、もう少し合理的な例外のようです。