gpt4 book ai didi

c# - 解析具有相同父节点和子节点名称的 XML

转载 作者:太空宇宙 更新时间:2023-11-03 13:06:08 25 4
gpt4 key购买 nike

我的合作伙伴为我提供了一个 SOAP 网络服务,它返回以下格式的 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、...和它的 child

我尝试在 visual studio 上解析它,但得到错误 xml nodes have the same name。

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

最佳答案

试试这个

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

关于c# - 解析具有相同父节点和子节点名称的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30746542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com