1

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]'.

これは、もう少し合理的な例外のようです。

4

2 に答える 2

4

なぜこれがうまくいかないのですか?

さて、あなたが何をしているのか見てみましょう。要素として取得templatesし、次の<Population>ようにクエリします。

  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)

Attributeここですべてをどのように使用しているかに注意してください。次に XML を見てください。

<Population>
  <id>3</id>
  <name>CHF</name>
  ...
</Population>

Population要素には属性がありません。ネストされた要素があります。詳細は確認していませんが、すべてのAttribute呼び出しをElement呼び出しに変更すると、 以外のすべてが修正される可能性がありますPopulationType。(どこから取得しようとしているのかは明らかではありません。)

XAttributeandからの明示的な変換は、一般にetcXElementよりもクリーンに使用できることに注意してください。int.Parse

于 2012-05-21T19:02:45.750 に答える
2

Attributeそれらが実際には要素である場合、メソッドを使用して値を取得しようとしています。

これを試してください(正直に言うとテストしていません):

XDocument xDocument = XDocument.Parse(xmlFromSvc);
XElement elem = xDocument.Element("ArrayOfPopulation");

var popModel = (from templates in elem.Elements("Population")
                select new PopulationModel
                {
                     populationID = (string)templates.Element("id"),
                     PopName = (string)templates.Element("name"),
                     Description = (string)templates.Element("description"),
                     PopulationType = (int)templates.Element("Type").Element("Name"),
                     isActive = (int)templates.Element("isActive")
                }).ToList();
于 2012-05-21T19:03:23.613 に答える