English 中文(简体)
采用C#
原标题:Parsing a SOAP Response with C#

我曾试图利用从APICA中获取的数据,但我无法阅读XML的答复。

其形式如下:

    <?xml version="1.0" standalone="no"?>
        <SOAP-ENV:Envelope xmlns:SOAPSDK1="http://www.w3.org/2001/XMLSchema" xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAPSDK3="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <SOAPSDK4:GetStoreProductsResponse xmlns:SOAPSDK4="http://www.externalwebservice.com/message/">
                <StoreProducts>
                    <StoreID></StoreID>
                    <Products></Products>
                </StoreProducts>
            </SOAPSDK4:GetStoreProductsResponse></SOAP-ENV:Body>
        </SOAP-ENV:Envelope>

我所需要的是Products(现在)。

我正在尝试使用。 C# to parse a SOAP Response (and others to not Hum this) without results.

我的法典:

    XDocument tst = XDocument.Load("Response.xml");
    XNamespace xmlns = "http://schemas.xmlsoap.org/soap/envelope/";
    var tstr = from result in tst.Descendants(xmlns + "StoreProducts") select result.Element("Products").Value;

I am almost sure that I am missing something basic.

任何术语都将得到真正的赞赏。

谢谢。

最佳回答

在您的XML中,Storeproducts不在XML名称空间之内,仅作以下改动:

var tstr = from result in tst.Descendants("StoreProducts") 
           select result.Element("Products").Value;

如果在XML内这样做的话,你所举的例子守则会取得成功:

  <SOAP-ENV:StoreProducts>
    <StoreID></StoreID>
    <Products></Products>
  </SOAP-ENV:StoreProducts>
问题回答

Are you sure you need to parse XML ? .NET is very efficient to handle SOAP using c# proxy.

Have you looked to svcutil.exe to generate a proxy ?

在我的情况下,我需要读一下在员额请求中发出的xml。

        // read the raw request
        Request.InputStream.Seek(0, SeekOrigin.Begin);
        string xmlPayload = new StreamReader(Request.InputStream).ReadToEnd();
        XDocument doc = XDocument.Parse(xmlPayload);

        XNamespace xmlns = "urn:sobject.enterprise.soap.sforce.com";
        item.sfId = doc.Descendants(xmlns + "Id").First().Value;
        item.AccountId = doc.Descendants(xmlns + "AccountId").First().Value;
        item.FirstName = doc.Descendants(xmlns + "FirstName").First().Value;
        item.LastName = doc.Descendants(xmlns + "LastName").First().Value;
        item.XmlPayload = xmlPayload;

为了得到XML的响应(您可以跳出这一步骤)

string strXml; 
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))     {
    strXml = rd.ReadToEnd(); 
}

为了从对应指示中提取产品(SstrXml)

Regex regex = new Regex("<Products>(.*?)</Products>");
var regMatch = regex.Match(strXml);
string strProductValue = regMatch.Groups[1].ToString();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签