解析WSDL将XElement发起为C#DTO类

时间:2016-07-07 10:18:26

标签: c# xml soap xml-parsing wsdl

我有这样的DTO课程:

 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;...

类对象意味着使用数据形式的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>
&#13;
&#13;
&#13;

所以,它是一种地图。地图&#34;键&#34;对应于ProjekatDTO类中的ProjekatDTO字段名称。我需要的是获得适当的价值&#34;填充ProjekatDTO字段。我是XML的新手,并试图搜索示例,但我要么找不到,要么我无法识别它。

1 个答案:

答案 0 :(得分:0)

尝试以下代码

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());
        }
    }
}