• 使用XmlDocument.SelectNodes遍历xml元素遇到的一个XPathException


    使用XmlDocument类时候报错:

    未处理的XPathException:
    需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。

    需要使用XmlNamespaceManager类:Resolves, adds, and removes namespaces to a collection and provides scope management for these namespaces.

    这是因为使用的soap文档含有自己的命名空间。相同问题:http://bbs.csdn.net/topics/350101606

    最终修改后的代码:

     string soap = "";
                        soap += "<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">";
                        soap += "<SOAP-ENV:Body>";
                        soap += "<AP-MSG:SubscriptionRequest xmlns:AP-MSG="http://www.vestas.dk/2001/04/ap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">";
                        soap += "<RequestBase ClientRequestHandle="41" ReturnItemTime="false" ReturnItemName="true" CallbackURL="10.116.21.222:1055" CallbackRate="PT10S"/>";
                        soap += "<ItemRequestSpec MaxAge="PT0.1S"/>";
                        soap += "<ItemList>";
                        foreach (IoTag tag in Tags)
                        {
                            soap += string.Format("<Item ItemName="{0}"/>", tag.Name);
                        }
                        soap += "</ItemList>";
                        soap += "</AP-MSG:SubscriptionRequest>";
                        soap += "</SOAP-ENV:Body>";
                        soap += "</SOAP-ENV:Envelope>";
                        string subscriptionRequest = "";
                        subscriptionRequest += "POST * HTTP/1.1
    ";
                        subscriptionRequest += "Content-Type:text/xml;charset=ISO-8859-1
    ";
                        subscriptionRequest += "Connection:Keep-Alive
    ";
                        subscriptionRequest += "SOAPAction:http://www.vestas.dk/2001/04/ap#SubscriptionRequest
    ";
                        subscriptionRequest += string.Format("Content-Length:{0}
    ", soap.Length);
                        subscriptionRequest += "
    ";
                        subscriptionRequest += soap;
    
                        XmlDocument xDoc = new XmlDocument();
                        xDoc.LoadXml(soap);
                        XmlNamespaceManager nsMgr = new XmlNamespaceManager(xDoc.NameTable);
                        nsMgr.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
                        nsMgr.AddNamespace("AP-MSG", "http://www.vestas.dk/2001/04/ap");
                        nsMgr.AddNamespace("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
                        XmlElement xe = xDoc.DocumentElement;
                        XmlNodeList xnl = xDoc.SelectNodes("/SOAP-ENV:Envelope/SOAP-ENV:Body/AP-MSG:SubscriptionRequest/ItemList/Item", nsMgr);

                foreach (XmlNode item in xnl) { string s = item.Attributes["ItemName"].Value; }
  • 相关阅读:
    迷失第一季/全集Lost 1迅雷下载
    星际之门SG1第一至十季/全集Stargate SG-1迅雷下载
    广告狂人第一至七季/全集Mad Men迅雷下载
    唐顿庄园第一至五季/全集Downton Abbey迅雷下载
    越狱第一至五季/全集迅雷下载
    邪恶力量第一至九季/全集Supernatural迅雷下载
    豪斯医生第一季/全集House M.D 1迅雷下载
    绝命毒师第五季/全集Breaking Bad迅雷下载
    绝命毒师第一季/全集Breaking Bad迅雷下载
    冰血暴第一季/全集Fargo迅雷下载
  • 原文地址:https://www.cnblogs.com/jumahe/p/4540733.html
Copyright © 2020-2023  润新知