使用 LINQ 解析肥皂响应

发布于 2024-11-01 23:56:59 字数 2174 浏览 1 评论 0原文

我在解析以下肥皂响应时遇到问题。这是我第一次使用 LINQ,我发现的示例必须使用 XML 而不是 SOAP 信封。我如何获得不同“项目”的值。我知道有不同的选项(使用添加服务引用),但这不是我当前项目中的选项。

<SOAP-ENV:Envelope 
xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" 
xmlns:ns1=\"http://random.com/api/1/service\" 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" 
xmlns:ns2=\"http://xml.apache.org/xml-soap\" 
xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" 
SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
   <SOAP-ENV:Body>
       <ns1:getBeatlesResponse>
           <return xsi:type=\"ns2:Map\">
               <item>
                   <key xsi:type=\"xsd:string\">error</key>
                   <value xsi:type=\"xsd:string\">OK</value>
               </item>
               <item>
                   <key xsi:type=\"xsd:string\">Beatles</key>
                   <value xsi:type=\"ns2:Map\">
                       <item>
                           <key xsi:type=\"xsd:int\">9</key>
                           <value xsi:type=\"xsd:string\">John Lennon</value>
                       </item>
                       <item>
                           <key xsi:type=\"xsd:int\">12</key>
                           <value xsi:type=\"xsd:string\">Paul McCartney</value>
                       </item>
                       <item>
                           <key xsi:type=\"xsd:int\">25</key>
                           <value xsi:type=\"xsd:string\">George Harrison</value>
                       </item>
                       <item>
                           <key xsi:type=\"xsd:int\">184</key>
                           <value xsi:type=\"xsd:string\">Ringo Starr</value>
                       </item>
                   </value>
               </item>
           </return>
       </ns1:getBeatlesResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

提前致谢

I'm having trouble parsing the following soap response. This is my first time working with LINQ and must examples I've found use XML and not a SOAP envelope. How do I get the values of the different "items". I know there are different options (using add service reference) but it is not an option in my current project.

<SOAP-ENV:Envelope 
xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" 
xmlns:ns1=\"http://random.com/api/1/service\" 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" 
xmlns:ns2=\"http://xml.apache.org/xml-soap\" 
xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" 
SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
   <SOAP-ENV:Body>
       <ns1:getBeatlesResponse>
           <return xsi:type=\"ns2:Map\">
               <item>
                   <key xsi:type=\"xsd:string\">error</key>
                   <value xsi:type=\"xsd:string\">OK</value>
               </item>
               <item>
                   <key xsi:type=\"xsd:string\">Beatles</key>
                   <value xsi:type=\"ns2:Map\">
                       <item>
                           <key xsi:type=\"xsd:int\">9</key>
                           <value xsi:type=\"xsd:string\">John Lennon</value>
                       </item>
                       <item>
                           <key xsi:type=\"xsd:int\">12</key>
                           <value xsi:type=\"xsd:string\">Paul McCartney</value>
                       </item>
                       <item>
                           <key xsi:type=\"xsd:int\">25</key>
                           <value xsi:type=\"xsd:string\">George Harrison</value>
                       </item>
                       <item>
                           <key xsi:type=\"xsd:int\">184</key>
                           <value xsi:type=\"xsd:string\">Ringo Starr</value>
                       </item>
                   </value>
               </item>
           </return>
       </ns1:getBeatlesResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮酒 2024-11-08 23:56:59

这一项非常棘手,因为您拥有的项目中可能也包含项目...因此,如果您执行类似的操作,

var returnResult = (from r in document.Descendants("item")
                            select r).ToList();

您将得到所有项目分离,并且其中一个项目具有所有值...

编辑:

这工作得很好,

XDocument document = XDocument.Load(@"XMLFile1.xml");

        List<Item> items = new List<Item>();

        var returnResult = (from r in document.Descendants("item")
                            select r).ToList();

        foreach (XElement xElement in returnResult)
        {

            Item item = new Item();

            item.Key = xElement.Element("key") != null ? xElement.Element("key").Value : "";
            item.Value = xElement.Element("value") != null ? xElement.Element("value").Value : "";

            items.Add(item);
        }

        //sort the list to get the one that have the rest to the end
        var sorted = (from s in items
                      orderby s.Value.Length ascending
                      select s).ToList();

        List<Item> finalList = new List<Item>();

        items.Clear();
        for (int i = 0; i < sorted.Count - 1; i++)
        {
            for (int j = 1; j < sorted.Count; j++)
            {
                if (sorted[j].Value.Contains(sorted[i].Value) &&
                    sorted[j].Value.Length > sorted[i].Value.Length)
                {
                    Item itm = new Item();
                    itm.Key = sorted[j].Key;
                    KeyValuePair<string, string> kvp = new KeyValuePair<string, string>(sorted[i].Key,sorted[i].Value);
                    itm.Items.Add(kvp);
                    items.Add(itm);

                }
                else
                {
                    if (!finalList.Contains(sorted[i]))
                        finalList.Add(sorted[i]);
                }
            }
        }
class Item
{
    public List<Item> Items { get; set; }
    public string Key { get; set; }
    public string Value {get;set;}
    public Item()
    {
        Items = new List<Item>();
    }
}

您现在可以看到所有子项目及其正确的密钥...只有确定/错误会造成一些麻烦...但是您可以从最终列表中获取它们,并在它们不是任何关键时选择它们-值对...

希望这有帮助

this one is pretty tricky because you have items that have items which probably could have items too... so if you do something like this

var returnResult = (from r in document.Descendants("item")
                            select r).ToList();

you will get all the items separated and one which has all the values in one...

edit:

this works somewhat fine

XDocument document = XDocument.Load(@"XMLFile1.xml");

        List<Item> items = new List<Item>();

        var returnResult = (from r in document.Descendants("item")
                            select r).ToList();

        foreach (XElement xElement in returnResult)
        {

            Item item = new Item();

            item.Key = xElement.Element("key") != null ? xElement.Element("key").Value : "";
            item.Value = xElement.Element("value") != null ? xElement.Element("value").Value : "";

            items.Add(item);
        }

        //sort the list to get the one that have the rest to the end
        var sorted = (from s in items
                      orderby s.Value.Length ascending
                      select s).ToList();

        List<Item> finalList = new List<Item>();

        items.Clear();
        for (int i = 0; i < sorted.Count - 1; i++)
        {
            for (int j = 1; j < sorted.Count; j++)
            {
                if (sorted[j].Value.Contains(sorted[i].Value) &&
                    sorted[j].Value.Length > sorted[i].Value.Length)
                {
                    Item itm = new Item();
                    itm.Key = sorted[j].Key;
                    KeyValuePair<string, string> kvp = new KeyValuePair<string, string>(sorted[i].Key,sorted[i].Value);
                    itm.Items.Add(kvp);
                    items.Add(itm);

                }
                else
                {
                    if (!finalList.Contains(sorted[i]))
                        finalList.Add(sorted[i]);
                }
            }
        }
class Item
{
    public List<Item> Items { get; set; }
    public string Key { get; set; }
    public string Value {get;set;}
    public Item()
    {
        Items = new List<Item>();
    }
}

you can now see all sub items with their correct key... only the ok/error is making some trouble... but you can get them from the final list and pick them if they are not any of the key-value pairs...

hope this helps

不必你懂 2024-11-08 23:56:59

您应该能够通过将其加载到 中来解析它X文档。然后您可以通过指定元素名称来选择所需的值。最后,您可以将这些值作为新的匿名类型返回。可以在对此问题的回复中找到一个示例: 使用 LINQ到 XML 来解析 SOAP 消息

如果您有任何问题,请告诉我。

You should be able to parse this by loading it into an XDocument. Then you can select out the values you require by specifying the element names. Finally you could return these values as a new anonymous type. An example can be found in the response to this question: Using LINQ to XML to Parse a SOAP message

Let me know if you have any problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文