【问题标题】:remove soap envelope header and get the XML删除肥皂信封头并获取 XML
【发布时间】:2021-01-09 07:26:29
【问题描述】:

我有一个soap响应,我想删除所有与soap相关的东西,并获得只有一个标题的内部XML。

回复:

"<s:Envelope xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
    <s:Body>
        <getResponse xmlns = ""http://tempuri.org/"">
            <getResult xmlns:xsi = ""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd = ""http://www.w3.org/2001/XMLSchema"">
                <Id>999</Id>
                <EResponse>
                    <au>
                        <st>ABC</st>
                        <co>DGH</co>
                        <mo>MMM</mo>
                    </au>
         </EResponse>
            </getResult>
        </getResponse>
    </s:Body>
</s:Envelope> 
          

我想提取这个:

<getResult xmlns:xsi = ""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd = ""http://www.w3.org/2001/XMLSchema"">
                <Id>999</Id>
                <EResponse>
                    <au>
                        <st>ABC</st>
                        <co>DGH</co>
                        <mo>MMM</mo>
                    </au>
         </EResponse>
            </getResult>

我正在尝试这个:

XDocument input;

      using (var nodeReader = new XmlNodeReader(doc))
        {
            nodeReader.MoveToContent();
            input = XDocument.Load(nodeReader);
        }

        var xns = XNamespace.Get("http://tempuri.org/");
        var SoapBody = input.Descendants(xns + "getResult").First().ToString();

但我也在 getresult 标记中附加了命名空间 tempuri,这是我不想要的或任何其他方式来实现它?

【问题讨论】:

  • 您在结果中获得命名空间的原因是因为在上层元素中,命名空间被定义为 xmlns="tempuri.org" 这意味着该元素的子元素和属性将有这个特定的命名空间。如果您确实需要没有命名空间的内容,则需要添加一个额外的步骤以从结果中删除所有命名空间。
  • m 也有同样的想法,唯一的方法是删除命名空间,实际上我的最终目标是将它反序列化到我的命名不同的类中,比如 GetResultTrigger。有什么见解吗?
  • 要删除 xml 文档中的命名空间,您可以按照此处标记的答案进行操作:stackoverflow.com/questions/987135/… 使用类并不能帮助您删除命名空间。

标签: c# xml wcf soap


【解决方案1】:

尝试以下:

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

namespace ConsoleApplication169
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            XDocument doc = XDocument.Parse(xml);


            XElement results = doc.Descendants().Where(x=> x.Name.LocalName == "getResult").FirstOrDefault();
            XNamespace ns = results.GetDefaultNamespace();

            string id = (string)results.Element(ns + "Id");

            Dictionary<string, string> au = results.Descendants(ns + "au").FirstOrDefault().Elements()
                .GroupBy(x => x.Name.LocalName, y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }
}

【讨论】:

  • 感谢您的回复,但是我正在尝试从 w3.org/2001/XMLSchema-instance"" xmlns:xsd = ""w3.org/2001/XMLSchema"">data</getResult> 开始获取 xml。上述解决方案将丢弃 和根标签,如果根标签中有更多数据(有效负载)怎么办
  • 数据总是在响应中。我的代码附加了 xml 中的所有值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
相关资源
最近更新 更多