2015-10-01 70 views
0

我再次遇到XML文檔問題,我通過網絡請求(HP Service Manager)收到回覆。如何正確處理此XML響應(SOAP:Envelope)以獲取至少一個DataView/DataTable?

這是XML響應:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
<SOAP-ENV:Body> 
<RetrieveSDToDoListResponse 
message="Success" returnCode="0" 
schemaRevisionDate="2015-09-24" 
schemaRevisionLevel="0" status="SUCCESS" 
xmlns="http://schemas.hp.com/SM/7" 
xmlns:cmn="http://schemas.hp.com/SM/7/Common" 
xmlns:xmime="http://www.w3.org/2005/05/xmlmime" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://schemas.hp.com/SM/7 /SDToDo.xsd"> 
<instance query="" recordid="CM00106862 - cm3r" uniquequery="record.id=&quot;CM00106862&quot; and itemType=&quot;cm3r&quot;"> 
<TodoNumber type="String">CM00106862</TodoNumber> 
<ToDoModule type="String">cm3r</ToDoModule> 
<ToDoStatus type="String">submitted</ToDoStatus> 
<ToDoDescription type="String">Portactivation Server SCHWARTZ3 C6000 Request and Network Configuration Request</ToDoDescription> 
<ToDoAssignmentGroup type="String">XL/IOC GERMANY</ToDoAssignmentGroup> 
<ToDoAssigneeName type="String">p591392</ToDoAssigneeName> 
</instance> 
<instance query="" recordid="IM02086084 - probsummary" uniquequery="record.id=&quot;IM02086084&quot; and itemType=&quot;probsummary&quot;"> 
<TodoNumber type="String">IM02086084</TodoNumber> 
<ToDoModule type="String">probsummary</ToDoModule> 
<ToDoStatus type="String">In Charge</ToDoStatus> 
<ToDoDescription type="String">WBI-FN Operating rights for XL10033 expired?</ToDoDescription> 
<ToDoAssignmentGroup type="String">XL/IOC GERMANY</ToDoAssignmentGroup> 
<ToDoAssigneeName type="String">p707112</ToDoAssigneeName> 
</instance> 
<instance query="" recordid="IM02135674 - probsummary" uniquequery="record.id=&quot;IM02135674&quot; and itemType=&quot;probsummary&quot;"> 
<TodoNumber type="String">IM02135674</TodoNumber> 
<ToDoModule type="String">probsummary</ToDoModule> 
<ToDoStatus type="String">In Charge</ToDoStatus> 
<ToDoDescription type="String">G00PSMP1: Event ID: 432, FILE, ERROR =&gt; SollShares =&gt; 430 =&gt; 432</ToDoDescription> 
<ToDoAssignmentGroup type="String">XL/IOC GERMANY</ToDoAssignmentGroup> 
<ToDoAssigneeName type="String">p518731</ToDoAssigneeName> 
</instance> 
<instance query="" recordid="TM00059273 - cm3t" uniquequery="record.id=&quot;TM00059273&quot; and itemType=&quot;cm3t&quot;"> 
<TodoNumber type="String">TM00059273</TodoNumber> 
<ToDoModule type="String">cm3t</ToDoModule> 
<ToDoStatus type="String">submitted</ToDoStatus> 
<ToDoDescription type="String">Tributum Server Downtime - Tivoli Task</ToDoDescription> 
<ToDoAssignmentGroup type="String">XL/IOC GERMANY</ToDoAssignmentGroup> 
<ToDoAssigneeName type="String">xl10033</ToDoAssigneeName> 
</instance> 
<instance query="" recordid="TM00059274 - cm3t" uniquequery="record.id=&quot;TM00059274&quot; and itemType=&quot;cm3t&quot;"> 
<TodoNumber type="String">TM00059274</TodoNumber> 
<ToDoModule type="String">cm3t</ToDoModule> 
<ToDoStatus type="String">submitted</ToDoStatus> 
<ToDoDescription type="String">Tributum disable mainframe ressource</ToDoDescription> 
<ToDoAssignmentGroup type="String">XL/IOC GERMANY</ToDoAssignmentGroup> 
<ToDoAssigneeName type="String">xl10033</ToDoAssigneeName> 
</instance> 
<instance query="" recordid="TM00059278 - cm3t" uniquequery="record.id=&quot;TM00059278&quot; and itemType=&quot;cm3t&quot;"> 
<TodoNumber type="String">TM00059278</TodoNumber> 
<ToDoModule type="String">cm3t</ToDoModule> 
<ToDoStatus type="String">submitted</ToDoStatus> 
<ToDoDescription type="String">Tributum Server Downtime - Tivoli Task</ToDoDescription> 
<ToDoAssignmentGroup type="String">XL/IOC GERMANY</ToDoAssignmentGroup> 
<ToDoAssigneeName type="String">xl10033</ToDoAssigneeName> 
</instance> 
<instance query="" recordid="TM00059280 - cm3t" uniquequery="record.id=&quot;TM00059280&quot; and itemType=&quot;cm3t&quot;"> 
<TodoNumber type="String">TM00059280</TodoNumber> 
<ToDoModule type="String">cm3t</ToDoModule> 
<ToDoStatus type="String">submitted</ToDoStatus> 
<ToDoDescription type="String">Tributum disable mainframe ressource</ToDoDescription> 
<ToDoAssignmentGroup type="String">XL/IOC GERMANY</ToDoAssignmentGroup> 
<ToDoAssigneeName type="String">xl10033</ToDoAssigneeName> 
</instance> 
<instance query="" recordid="TM00059504 - cm3t" uniquequery="record.id=&quot;TM00059504&quot; and itemType=&quot;cm3t&quot;"> 
<TodoNumber type="String">TM00059504</TodoNumber> 
<ToDoModule type="String">cm3t</ToDoModule> 
<ToDoStatus type="String">submitted</ToDoStatus> 
<ToDoDescription type="String">Tributum disable mainframe ressource</ToDoDescription> 
<ToDoAssignmentGroup type="String">XL/IOC GERMANY</ToDoAssignmentGroup> 
<ToDoAssigneeName type="String">p568003</ToDoAssigneeName> 
</instance> 
</RetrieveSDToDoListResponse> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 


我試圖與下面的代碼,但我通過循環通現有節點是在末端。 我知道應該有一個簡單的方法來從節點「實例」和所有子項目獲取所有項目。

最後,我需要一個DataView或一個DataTable繼續我的應用程序。

public static string gettextfromWebserviceItems(string result) 
    { 
     StringBuilder builder = new StringBuilder(); 


     var doc = new XmlDocument(); 
     doc.LoadXml(result); 
     var ns = new XmlNamespaceManager(doc.NameTable); 
     ns.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"); 
     //XmlNodeList nodes = doc.SelectNodes("/SOAP-ENV:Envelope/SOAP-ENV:Body/RetrieveSDToDoListResponse/instance", ns); 
     XmlNodeList nodes = doc.GetElementsByTagName("instance"); 

     List<string> elementNamesList = new List<string>(); 
     elementNamesList.Add("TodoNumber"); 
     elementNamesList.Add("ToDoModule"); 
     elementNamesList.Add("ToDoStatus"); 
     elementNamesList.Add("ToDoDescription"); 
     elementNamesList.Add("ToDoAssignmentGroup"); 
     elementNamesList.Add("ToDoAssigneeName"); 

     foreach (XmlNodeList node in nodes) 
     { 

      //foreach (XmlAttribute v in node.Attributes) 
      //{ 
      // var value = v; 
      // //Do the processing 

      //} 


     } 

如果沒有與像SAX或任何一個外部DLL一個簡單的解決方案,所以讓我知道...

謝謝你在前進, ,距離慕尼黑

+0

你嘗試_add服務Reference_的項目,並自動生成一組類的?這很容易,不需要手動分析肥皂信封。 –

回答

0

的XSD最良好的祝願文件可以相對容易地轉換成C#類的定義。您發佈的XML具有以下屬性:xsi:schemaLocation="http://schemas.hp.com/SM/7 /SDToDo.xsd"

有了這個,您可以使用xsd.exe將XSD解析爲C#,以便與XmlSerializer(一個糟糕的想法,我不會這樣做)一起使用,或者您可以使用C#類作爲工作的基礎來創建您的自己的模型和翻譯器,而不使用XmlSerializer(使用XNode而不是使用XmlDocument API)。

一旦它處於對象形式,它將變得相對容易變爲DataTableDataView(或者您可以直接進行,無論您喜歡什麼)。

0

與前面的發言者不太一致。是的,它將以對象形式存在,但將對象數組轉換爲DataTable並不那麼容易。 在你的情況,你很幸運,你的信封,將XML數據表中的XML格式的確切表示,你可以使用ReadXml方法

// your code, practically unchanged 
XmlDocument doc = new XmlDocument(); 
doc.LoadXml(result); 
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); 
ns.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"); 
ns.AddNamespace("XZ", "http://schemas.hp.com/SM/7"); 
XmlNode node = doc.SelectSingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/XZ:RetrieveSDToDoListResponse", ns); 

if(node!=null) 
{ 
    //define your table 
    DataTable dt = new DataTable(); 
    dt.Columns.Add("TodoNumber", typeof(string)); 
    dt.Columns.Add("ToDoModule", typeof(string)); 
    dt.Columns.Add("ToDoStatus", typeof(string)); 
    dt.Columns.Add("ToDoDescription", typeof(string)); 
    dt.Columns.Add("ToDoAssignmentGroup", typeof(string)); 
    dt.Columns.Add("ToDoAssigneeName", typeof(string)); 
    dt.TableName = "instance"; 

    string content = "<DocumentElement>" + 
     //DIRTY HACK TO REMOVE NAMESPACE 
      node.InnerXml.Replace("xmlns=\"http://schemas.hp.com/SM/7\"", "") 
     + "</DocumentElement>"; 

    //this code makes the trick 
    using(MemoryStream ms=new MemoryStream(Encoding.UTF8.GetBytes (content))) 
    { 
     dt.ReadXml(ms);  
    } 

}   
+0

我在很多方面嘗試過......但這對我來說非常合適! 非常感謝vitalygolub – Typus2