使用相同的父节点名和子节点名解析XML

时间:2015-06-10 03:22:16

标签: c# xml web-services soap

我的合作伙伴为我提供了一个SOAP Web服务,它以这种格式返回XML:

 <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:RestControllerwsdl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <SOAP-ENV:Body>
      <ns1:getInfoResponse>
         <return xsi:type="ns2:Map">
            <item>
               <key xsi:type="xsd:string">l3</key>
               <value SOAP-ENC:arrayType="ns2:Map[10]" xsi:type="SOAP-ENC:Array">
                  <item xsi:type="ns2:Map">
                     <item>
                        <key xsi:type="xsd:string">id</key>
                        <value xsi:type="xsd:string">2815</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">date</key>
                        <value xsi:type="xsd:string">2015-06-10 00:00:14</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Success</key>
                        <value xsi:type="xsd:string">0</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Failed</key>
                        <value xsi:type="xsd:string">0</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Cancel</key>
                        <value xsi:type="xsd:string">4</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Error</key>
                        <value xsi:type="xsd:string">932</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Active</key>
                        <value xsi:type="xsd:string">2644</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Total</key>
                        <value xsi:type="xsd:string">3618</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Summary</key>
                        <value xsi:type="xsd:string">1864000</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">CustomerID</key>
                        <value xsi:type="xsd:string">4</value>
                     </item>
                  </item>
                  <item xsi:type="ns2:Map">
                     <item>
                        <key xsi:type="xsd:string">id</key>
                        <value xsi:type="xsd:string">2805</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">date</key>
                        <value xsi:type="xsd:string">2015-06-09 00:00:30</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Success</key>
                        <value xsi:type="xsd:string">0</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Failed</key>
                        <value xsi:type="xsd:string">0</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Cancel</key>
                        <value xsi:type="xsd:string">11</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Error</key>
                        <value xsi:type="xsd:string">1294</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Active</key>
                        <value xsi:type="xsd:string">7321</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Total</key>
                        <value xsi:type="xsd:string">3622</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">Summary</key>
                        <value xsi:type="xsd:string">2588000</value>
                     </item>
                     <item>
                        <key xsi:type="xsd:string">CustomerID</key>
                        <value xsi:type="xsd:string">4</value>
                     </item>
                  </item>
               </value>
            </item>
         </return>
      </ns1:getInfoResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是我从他的网络服务中获得的,我认为它来自PHP SOAP 正如您所看到的,父节点“item”标识一个没有值的项“key”字符串,其子节点显示其信息,如id,date,Success,... 和它的孩子

我试图在visual studio上解析它但得到错误的xml节点具有相同的名称。

请帮我一个解决方案或演示代码,以便从xsi中的每个子项获取“l3”的子节点:type =“ns2:Map”

1 个答案:

答案 0 :(得分:1)

试试这个

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

using System.IO;

namespace ConsoleApplication33
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
            "<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"urn:RestControllerwsdl\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ns2=\"http://xml.apache.org/xml-soap\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
               "<SOAP-ENV:Body>" +
                  "<ns1:getInfoResponse>" +
                     "<return xsi:type=\"ns2:Map\">" +
                        "<item>" +
                           "<key xsi:type=\"xsd:string\">l3</key>" +
                           "<value SOAP-ENC:arrayType=\"ns2:Map[10]\" xsi:type=\"SOAP-ENC:Array\">" +
                              "<item xsi:type=\"ns2:Map\">" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">id</key>" +
                                    "<value xsi:type=\"xsd:string\">2815</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">date</key>" +
                                    "<value xsi:type=\"xsd:string\">2015-06-10 00:00:14</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Success</key>" +
                                    "<value xsi:type=\"xsd:string\">0</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Failed</key>" +
                                    "<value xsi:type=\"xsd:string\">0</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Cancel</key>" +
                                    "<value xsi:type=\"xsd:string\">4</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Error</key>" +
                                    "<value xsi:type=\"xsd:string\">932</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Active</key>" +
                                    "<value xsi:type=\"xsd:string\">2644</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Total</key>" +
                                    "<value xsi:type=\"xsd:string\">3618</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Summary</key>" +
                                    "<value xsi:type=\"xsd:string\">1864000</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">CustomerID</key>" +
                                    "<value xsi:type=\"xsd:string\">4</value>" +
                                 "</item>" +
                              "</item>" +
                              "<item xsi:type=\"ns2:Map\">" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">id</key>" +
                                    "<value xsi:type=\"xsd:string\">2805</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">date</key>" +
                                    "<value xsi:type=\"xsd:string\">2015-06-09 00:00:30</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Success</key>" +
                                    "<value xsi:type=\"xsd:string\">0</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Failed</key>" +
                                    "<value xsi:type=\"xsd:string\">0</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Cancel</key>" +
                                    "<value xsi:type=\"xsd:string\">11</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Error</key>" +
                                    "<value xsi:type=\"xsd:string\">1294</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Active</key>" +
                                    "<value xsi:type=\"xsd:string\">7321</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Total</key>" +
                                    "<value xsi:type=\"xsd:string\">3622</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">Summary</key>" +
                                    "<value xsi:type=\"xsd:string\">2588000</value>" +
                                 "</item>" +
                                 "<item>" +
                                    "<key xsi:type=\"xsd:string\">CustomerID</key>" +
                                    "<value xsi:type=\"xsd:string\">4</value>" +
                                 "</item>" +
                              "</item>" +
                           "</value>" +
                        "</item>" +
                     "</return>" +
                  "</ns1:getInfoResponse>" +
               "</SOAP-ENV:Body>" +
               "</SOAP-ENV:Envelope>";

            XDocument doc = XDocument.Parse(input);
            XElement value = doc.Descendants("value").FirstOrDefault();
            var items = value.Elements("item")
                .Select(x => x.Elements("item").Select(y => new { key = y.Element("key").Value, value = y.Element("value").Value})
                .ToList()).ToList();
        }

    }
}

这是结果的图片 enter image description here