将源自 WSDL 的 XElement 解析为 C# DTO class

Parse WSDL originated XElement to C# DTO class

我有一个这样的 DTO class:

 class ProjekatDTO
{
    private int idProjekta;
    private string nazivProjekta;
    private DateTime datumPocetkaRada;
    private DateTime datumKrajaRada;
    private decimal budzet;
    private string opisProjekta;
    private int aktivan;
    private DateTime krajnjiRok;
    private int uradjeno;
    private string sef_projekta;
    private string nadzor;...

Class 对象旨在使用来自 Web SOAP 服务器的数据进行填充。我以 XElement:

的形式获取数据
  SoapClient client = new SoapClient("http://somelink.someserver.net/~johndoe/gogogo/servis");
        XElement myEle = client.Invoke("getProjekti");

当我打印 XElement 时,结果是:

<SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <ns1:getProjektiResponse xmlns:ns1="http://somelink.someserver.net/~johndoe/gogogo/servis?ws=1">
    <return SOAP-ENC:arrayType="ns2:Map[3]" xsi:type="SOAP-ENC:Array" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">

<item xsi:type="ns2:Map">
        <item>
          <key xsi:type="xsd:string">id</key>
          <value xsi:type="xsd:string">53</value>
        </item>
        <item>
          <key xsi:type="xsd:string">naziv</key>
          <value xsi:type="xsd:string">projekat</value>
        </item>
        <item>
          <key xsi:type="xsd:string">datum_pocetka_rada</key>
          <value xsi:type="xsd:string">2016-07-07</value>
        </item>
        <item>
          <key xsi:type="xsd:string">datum_kraja_rada</key>
          <value xsi:type="xsd:string">2016-07-20</value>
        </item>
        <item>
          <key xsi:type="xsd:string">budzet</key>
          <value xsi:type="xsd:string">131313.00</value>
        </item>
        <item>
          <key xsi:type="xsd:string">opis_projekta</key>
          <value xsi:type="xsd:string">Opis projekta...</value>
        </item>
        <item>
          <key xsi:type="xsd:string">aktivan</key>
          <value xsi:type="xsd:string">1</value>
        </item>
        <item>
          <key xsi:type="xsd:string">krajnji_rok</key>
          <value xsi:type="xsd:string">2016-07-11</value>
        </item>
        <item>
          <key xsi:type="xsd:string">uradjeno</key>
          <value xsi:type="xsd:string">23</value>
        </item>
        <item>
          <key xsi:type="xsd:string">postoji</key>
          <value xsi:type="xsd:string">1</value>
        </item>
        <item>
          <key xsi:type="xsd:string">sef_projekta</key>
          <value xsi:type="xsd:string">12</value>
        </item>
        <item>
          <key xsi:type="xsd:string">nadzor</key>
          <value xsi:type="xsd:string">12</value>
        </item>
      </item>
      <item xsi:type="ns2:Map">
        <item>
          <key xsi:type="xsd:string">id</key>
          <value xsi:type="xsd:string">54</value>
        </item>
        <item>
          <key xsi:type="xsd:string">naziv</key>
          <value xsi:type="xsd:string">drugi projekat</value>
        </item>
        <item>
          <key xsi:type="xsd:string">datum_pocetka_rada</key>
          <value xsi:type="xsd:string">2016-07-06</value>
        </item>
        <item>
          <key xsi:type="xsd:string">datum_kraja_rada</key>
          <value xsi:type="xsd:string">2016-07-29</value>
        </item>
        <item>
          <key xsi:type="xsd:string">budzet</key>
          <value xsi:type="xsd:string">13331.00</value>
        </item>
        <item>
          <key xsi:type="xsd:string">opis_projekta</key>
          <value xsi:type="xsd:string">opis drugog projekta</value>
        </item>
        <item>
          <key xsi:type="xsd:string">aktivan</key>
          <value xsi:type="xsd:string">1</value>
        </item>
        <item>
          <key xsi:type="xsd:string">krajnji_rok</key>
          <value xsi:type="xsd:string">2016-07-28</value>
        </item>
        <item>
          <key xsi:type="xsd:string">uradjeno</key>
          <value xsi:type="xsd:string">12</value>
        </item>
        <item>
          <key xsi:type="xsd:string">postoji</key>
          <value xsi:type="xsd:string">1</value>
        </item>
        <item>
          <key xsi:type="xsd:string">sef_projekta</key>
          <value xsi:type="xsd:string">12</value>
        </item>
        <item>
          <key xsi:type="xsd:string">nadzor</key>
          <value xsi:type="xsd:string">12</value>
        </item>
      </item>
    </return>
  </ns1:getProjektiResponse>
</SOAP-ENV:Body>

所以,这是一张地图。映射 "keys" 对应于 ProjekatDTO class 中的 ProjekatDTO 字段名称。我需要的是获得适当的 "values" 来填充 ProjekatDTO 字段。我是 XML 的新手,并试图搜索示例,但我要么找不到,要么无法识别。

试试下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            //more than one value per key
            Dictionary<string, List<string>> dict1 = doc.Descendants("item").First().Elements("item")
                .GroupBy(x => x.Element("key").Value, y => y.Element("value").Value)
                .ToDictionary(x => x.Key, y => y.ToList());

            //one value per key
            Dictionary<string, string> dict2 = doc.Descendants("item").First().Elements("item")
                .GroupBy(x => x.Element("key").Value, y => y.Element("value").Value)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }
}