首页 > 解决方案 > SOAP XML 消息

问题描述

我试图阅读这个 SOAP XML 消息

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <EchoSoapRequest xmlns="http://tempuri.org/">
      <grantapplication externalsystemreference="0743A61C-B3F8-4B51-AF1E-FBE76172D34C" externalid="d77ddae7-ad19-4c4a-b3bf-1e83df82e40f">
        <scheme>CDD</scheme>
        <applicationdate> 20170126 </applicationdate>
        <category> CDD F </category>
        <applicant>
          <title>Mr</title>
        </applicant>

      </grantapplication>
    </EchoSoapRequest>
  </soap:Body>
</soap:Envelope>

这是我的方法

public bool SaveContacts(XmlDocument application)
        {

                XDocument xmessage = XDocument.Parse(application.OuterXml);


                XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";//Envelop namespace s
                XNamespace xsd = "http://www.w3.org/2001/XMLSchema";//Envelop namespace s
                XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";//Envelop namespace s


                XNamespace d = "http://tempuri.org/";//bookHotelResponse namespace
                XNamespace externalsystemreference = "0743A61C-B3F8-4B51-AF1E-FBE76172D34C";//d namespace
                XNamespace externalid = "d77ddae7-ad19-4c4a-b3bf-1e83df82e40f";//d namespace


                foreach (var itm in xmessage.Descendants(xsi + "Body")
                    .Descendants(externalsystemreference + "grantapplication").Descendants(externalid + "grantapplication"))
                {
                    string ss = itm.Element(d + "scheme").Value;
                }

            return true;
        }

但是仍然没有为 ss 选择任何值,有人能看出这有什么问题吗

标签: c#xmlsoap

解决方案


如果我们假设您维护正确的 xml(根据上面的评论),并且您的方法确实收到了有效的“XmlDocument”;然后:

        private static XmlDocument HisXml()
    {
        var xDoc = XDocument.Load("C:\\temp\\HisXml.xml");

        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(xDoc.ToString());
        return xmlDocument;
    }

然后,这是可行的,请注意,我会解决可能的 NULL 等问题,但您可以按照自己的方式处理 :) :(注意,如果您知道您只接收到,则无需通过“方案”节点进行循环一,你需要弄清楚

public static bool SaveContacts(XmlDocument application)
    {
        // COMMENTED CODE IS YOU OLD STUFF
        //XDocument xmessage = XDocument.Parse(application.OuterXml);
        //XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";//Envelop namespace s
        //XNamespace xsd = "http://www.w3.org/2001/XMLSchema";//Envelop namespace s
        //XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";//Envelop namespace s
        //XNamespace d = "http://tempuri.org/";//bookHotelResponse namespace
        //XNamespace externalsystemreference = "0743A61C-B3F8-4B51-AF1E-FBE76172D34C";//d namespace
        //XNamespace externalid = "d77ddae7-ad19-4c4a-b3bf-1e83df82e40f";//d namespace

        XmlNodeList nodeList = application.GetElementsByTagName("scheme");

        string hisStuff;
        foreach (XmlNode n in nodeList)
        {
            hisStuff = n.InnerText;
        }

        //foreach (var itm in xmessage.Descendants(xsi + "Body")
        //    .Descendants(externalsystemreference + "grantapplication").Descendants(externalid + "grantapplication"))
        //{
        //    string ss = itm.Element(d + "scheme").Value;
        //}

        return true;
    }

推荐阅读