2015-11-04 50 views
-1

我有一個返回響應的Web服務,我似乎無法找到如何解析一個一個很好的答案:FileContent參數使用PHP或C#解析<一:FileContent>在XML響應參數

響應樣子:

<GetReturnFileResponse xmlns="http://tempuri.org/"> 
<GetReturnFileResult xmlns:a="http://schemas.datacontract.org/2004/07/SigmaAPIService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <a:FileContent>/*Byte[] data type long string looks like 'IkRWTEoxMDI2LkVGQiIsIkVGVCIsIjgxOCIsIlZBTlNQRVlCUk9FQ0siLCIxMC8yNi8xNSIsIjA3Mzk3MjE4MSIsIjcwMDA3ODc5MDY5MDUwJSMDEiLCIxMC8yOC8xNSIsIlBCIiLCIiDQo=' here. **removed some chars just a sample*/</a:FileContent> 
    <a:Response> 
     <a:ResponseCode>1</a:ResponseCode> 
     <a:ResponseMessage>Successfully processed</a:ResponseMessage> 
     <a:ResponseType>Success</a:ResponseType> 
    </a:Response> 
</GetReturnFileResult> 

+0

的 「URL HERE」 值是實際ñ amespaces,它們很重要。您註冊名​​稱空間的別名並在Xpath中使用它們。此外,將問題限制在一個環境並顯示您嘗試的內容是一個好主意。 – ThW

+0

謝謝我添加了這些網址,我的嘗試被限制爲 'byte [] byteArray =「IkRWTEoxMDI2LkVGQiIsIkVGVCIsIjgxOCIsIlZBTlNQRVlCUk9FQ0siLCIxMC8yNi8 =」; 字符串bytetest = System.Text.Encoding.UTF8.GetString(byteArray);' –

+0

我真的找不到起點 –

回答

0

易與XML的LINQ

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string xml = 
       "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + 
       "<GetReturnFileResponse xmlns=\"http://tempuri.org/\">" + 
        "<GetReturnFileResult xmlns:a=\"http://schemas.datacontract.org/2004/07/SigmaAPIService\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" + 
        "<a:FileContent>/*Byte[] data type long string looks like 'IkRWTEoxMDI2LkVGQiIsIkVGVCIsIjgxOCIsIlZBTlNQRVlCUk9FQ0siLCIxMC8yNi8xNSIsIjA3Mzk3MjE4MSIsIjcwMDA3ODc5MDY5MDUwJSMDEiLCIxMC8yOC8xNSIsIlBCIiLCIiDQo=' here. **removed some chars just a sample*/</a:FileContent>" + 
        "<a:Response>" + 
         "<a:ResponseCode>1</a:ResponseCode>" + 
         "<a:ResponseMessage>Successfully processed</a:ResponseMessage>" + 
         "<a:ResponseType>Success</a:ResponseType>" + 
        "</a:Response>" + 
        "</GetReturnFileResult>" + 
       "</GetReturnFileResponse>"; 
      XDocument doc = XDocument.Parse(xml); 

      XElement fileContent = doc.Descendants().Where(x => x.Name.LocalName == "FileContent").FirstOrDefault(); 
      XNamespace ns_a = fileContent.Name.Namespace; 
     } 
    } 
} 
​ 
+0

很好,但我該如何輸出文件的內容?我試過 'Console.WriteLine(fileContent); Console.ReadLine();' 只是爲了得到我正在處理的內容,但並不真正能夠將byte []轉換爲有用的輸出 –

+1

數據被加密或像zip文件一樣打包。請參閱以下網頁:http://www.codeproject.com/Articles/30561/Using-ZIP-content-for-delivery-over-HTTP – jdweng

+0

感謝這就是我需要一種資源來了解發生了什麼。 –