【问题标题】:SOAP XML select NodesSOAP XML 选择节点
【发布时间】:2018-07-05 15:03:32
【问题描述】:

我正在尝试在 C# 对象中获取 SOAP XML 值,但目前我无法选择任何节点,因此没有任何价值。

这是 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>
<AddTwoStr xmlns="http://cpu1147/AB_TestWebApp/">
<InputParameters>
<Parameter>
<DataType>string</DataType>
<Name>String1</Name>
<Value>test</Value>
</Parameter>
<Parameter>
<DataType>string</DataType>
<Name>String2</Name>
<Value>test</Value
></Parameter>
</InputParameters>
</AddTwoStr>
</soap:Body>
</soap:Envelope>

XML 存储在以下 c# 代码中:

// Get raw request body
            Stream receiveStream = HttpContext.Current.Request.InputStream;
            // Move to begining of input stream and read
            receiveStream.Position = 0;
            //Webrequest als StreamReader
            //StreamReader mystream = new StreamReader(receiveStream, Encoding.UTF8);
            StreamReader mystream = new StreamReader(receiveStream);

            string test = mystream.ReadToEnd();

            XmlDocument document = new XmlDocument();
            document.LoadXml(test);

有没有人可以为我选择 String1 和 String2 的值?

感谢和亲切的问候, 克里斯

【问题讨论】:

  • 你说你没有得到任何价值......你遇到了什么错误/问题?
  • String 1 和 String2 本身就是上述 XML 中的值。你能指定你在寻找什么标签值吗?

标签: c# xml soap


【解决方案1】:

根据我的经验,通过网络发送的 SOAP 请求被反序列化为已知类型,这些类型通常由添加服务引用时生成的消费服务引用信息提供。我很欣赏您可能会从其他地方接收此有效负载,但我仍然发现使用强类型类来帮助提取您需要的值通常更容易。您当然可以将 XML 加载到 XPath 文档或其他内容中并遍历其中的节点和元素。

在下面的示例中,我采用了您的 xml 并创建了反序列化所需的类。

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
    public partial class Envelope
    {

        private EnvelopeBody bodyField;

        /// <remarks/>
        public EnvelopeBody Body
        {
            get
            {
                return this.bodyField;
            }
            set
            {
                this.bodyField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public partial class EnvelopeBody
    {

        private AddTwoStr addTwoStrField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://cpu1147/AB_TestWebApp/")]
        public AddTwoStr AddTwoStr
        {
            get
            {
                return this.addTwoStrField;
            }
            set
            {
                this.addTwoStrField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://cpu1147/AB_TestWebApp/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://cpu1147/AB_TestWebApp/", IsNullable = false)]
    public partial class AddTwoStr
    {

        private AddTwoStrParameter[] inputParametersField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("Parameter", IsNullable = false)]
        public AddTwoStrParameter[] InputParameters
        {
            get
            {
                return this.inputParametersField;
            }
            set
            {
                this.inputParametersField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://cpu1147/AB_TestWebApp/")]
    public partial class AddTwoStrParameter
    {

        private string dataTypeField;

        private string nameField;

        private string valueField;

        /// <remarks/>
        public string DataType
        {
            get
            {
                return this.dataTypeField;
            }
            set
            {
                this.dataTypeField = value;
            }
        }

        /// <remarks/>
        public string Name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        public string Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

接下来我们使用 XMLSerializer 将 xml 反序列化为类的实例。 在下面的示例中,我从本地文件加载了您的 xml。

Envelope data;
var xml = File.ReadAllText(@"C:\test.xml");

 using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
 {
      XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
      data = (Envelope)serializer.Deserialize(stream);
 }

 //Access the parameters here via an index.
 var val = data.Body.AddTwoStr.InputParameters[0].Value;

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    非常感谢您的帮助。现在我可以输入 XML。下一个问题是响应。我必须将其设置为 XML 并尝试以下操作

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.IO;
    using System.Xml;
    using System.Text;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Soap;
    using System.Web.Script.Services;
    using System.Xml.Serialization;
    
    namespace AB_TestWebApp
    {
        /// <summary>
        /// Zusammenfassungsbeschreibung für WebService1
        /// </summary>
        [WebService(Namespace = "http://cpu1147/AB_TestWebApp/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(true)]
        // Wenn der Aufruf dieses Webdiensts aus einem Skript zulässig sein soll, heben Sie mithilfe von ASP.NET AJAX die Kommentarmarkierung für die folgende Zeile auf. 
        [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
    
    
            [WebMethod]
            [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
            public string AddTwoStr()
            {
                //--//
                // Get raw request body
                Stream receiveStream = HttpContext.Current.Request.InputStream;
                // Move to begining of input stream and read
                receiveStream.Position = 0;
                //Webrequest als StreamReader
                //StreamReader mystream = new StreamReader(receiveStream, Encoding.UTF8);
                //StreamReader mystream = new StreamReader(receiveStream);
    
                //string test = mystream.ReadToEnd();
    
                //XmlDocument document = new XmlDocument();
                //document.LoadXml(test);
                //XElement xdocument = XElement.Parse(test);
    
                Envelope data;
                //var xml = document.Value;
    
                //using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(receiveStream.ToString())))
                using (StreamReader stream = new StreamReader(receiveStream, Encoding.UTF8))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
                    data = (Envelope)serializer.Deserialize(stream);
                }
    
                //Access the parameters here via an index.
                string String1 = data.Body.AddTwoStr.InputParameters[0].Value;
                string String2 = data.Body.AddTwoStr.InputParameters[1].Value;
    
                //--//
    
                XElement sofon_response = new XElement("Parameter",
                new XElement("DataType", "String"),
                new XElement("Name", "WebResponse"),
                new XElement("Value", string.Concat(String1, String2))
                );
    
                return sofon_response.ToString();
            }
    
    
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }
    
    
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
            [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
            public partial class Envelope
            {
    
                private EnvelopeBody bodyField;
    
                /// <remarks/>
                public EnvelopeBody Body
                {
                    get
                    {
                        return this.bodyField;
                    }
                    set
                    {
                        this.bodyField = value;
                    }
                }
            }
    
            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
            public partial class EnvelopeBody
            {
    
                private AddTwoStrPar addTwoStrField;
    
                /// <remarks/>
                [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://cpu1147/AB_TestWebApp/")]
                public AddTwoStrPar AddTwoStr
                {
                    get
                    {
                        return this.addTwoStrField;
                    }
                    set
                    {
                        this.addTwoStrField = value;
                    }
                }
            }
    
            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://cpu1147/AB_TestWebApp/")]
            [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://cpu1147/AB_TestWebApp/", IsNullable = false)]
            public partial class AddTwoStrPar
            {
    
                private AddTwoStrParameter[] inputParametersField;
    
                /// <remarks/>
                [System.Xml.Serialization.XmlArrayItemAttribute("Parameter", IsNullable = false)]
                public AddTwoStrParameter[] InputParameters
                {
                    get
                    {
                        return this.inputParametersField;
                    }
                    set
                    {
                        this.inputParametersField = value;
                    }
                }
            }
    
            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://cpu1147/AB_TestWebApp/")]
            public partial class AddTwoStrParameter
            {
    
                private string dataTypeField;
    
                private string nameField;
    
                private string valueField;
    
                /// <remarks/>
                public string DataType
                {
                    get
                    {
                        return this.dataTypeField;
                    }
                    set
                    {
                        this.dataTypeField = value;
                    }
                }
    
                /// <remarks/>
                public string Name
                {
                    get
                    {
                        return this.nameField;
                    }
                    set
                    {
                        this.nameField = value;
                    }
                }
    
                /// <remarks/>
                public string Value
                {
                    get
                    {
                        return this.valueField;
                    }
                    set
                    {
                        this.valueField = value;
                    }
                }
            }
        }
    }
    

    我现在的问题是 XML 现在格式不正确,请参阅此示例:

    <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AddTwoStrResponse xmlns="http://cpu1147/AB_TestWebApp/"><AddTwoStrResult>&lt;Parameter&gt;
      &lt;DataType&gt;String&lt;/DataType&gt;
      &lt;Name&gt;WebResponse&lt;/Name&gt;
      &lt;Value&gt;HalloChristian&lt;/Value&gt;
    &lt;/Parameter&gt;</AddTwoStrResult></AddTwoStrResponse></soap:Body></soap:Envelope>
    

    我需要回馈的是:

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
            <WebMethodResponse xmlns="http://tempuri.org/">
                <WebMethodResult>
                    <Parameter>
                        <DataType>String</DataType>
                        <Name>WebResponse</Name>
                        <Value>Hallo World</Value>
                    </Parameter>
                </WebMethodResult>
            </WebMethodResponse>
            <soap:Fault>
                <faultcode/>
                <faultstring/>
                <faultactor/>
                <detail/>
            </soap:Fault>
        </soap:Body>
    </soap:Envelope>
    

    我尝试了多次反序列化,但总是收到错误消息“XML 中的字符错误”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多