SOAP XML消息

时间:2018-06-15 15:01:23

标签: c# xml soap

我试图阅读这篇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>

这是我的approch

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选择任何价值 有人可以看到这个错误

1 个答案:

答案 0 :(得分:1)

如果我们假设您维护了正确的xml(根据上面的注释),并且您的方法确实正在接收有效的&#34; XmlDocument&#34 ;;然后:

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

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

然后,这个工作,谨慎,我会解决可能的NULL等等,但你可以处理你想要的:) :(注意,不需要循环通过&#34;方案&#34;节点,如果你知道你只收到一个,你需要弄清楚

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;
    }