2013-04-29 37 views
0

我已經繼承了C#代碼,並且在處理基本的XML時遇到了一些麻煩。我更習慣於在Java中使用JDOM。我也讀過很多不同的教程,到目前爲止,沒有任何幫助。有沒有更好的方法來處理SOAP POST響應 - 遍歷XML

代碼行Console.WriteLine("at id "+xmlReader.Value.ToString());將輸出id兩次,首先輸入值,然後再輸入null。

at id 10805 
at id 

XmlTextReader不是這份工作的最佳工具?它的行爲似乎與我的期望不同,即當我使用xmlReader.Read()時,我期望它讀取完整的XML元素。

對於JDOM,我使用例如String userEmail = messagePackage.getRootElement().getChildTextTrim("USEREMAIL");在C#中有類似的東西嗎?

響應:

<addAgentResponse xmlns="http://www.**********.com/soa/oi/ccma/service"> 
    <addAgentResult>true</addAgentResult> 
    <agentLocalID>10805</agentLocalID> 
    <errorMsg/> 
</addAgentResponse> 

的代碼處理所述響應:

response = request.GetResponse() as HttpWebResponse; 

XmlTextReader xmlReader = new XmlTextReader(response.GetResponseStream()); 
while (xmlReader.Read()) 
{ 
    switch (xmlReader.Name) 
    { 
     case "errorMsg": 
      xmlReader.Read(); 
      Console.WriteLine("at error"); 
      if (xmlReader.Value.ToString() != "") 
      { 
       errorText = xmlReader.Value.ToString(); 
       failures = true; 
      } 
      break; 
     case "agentLocalID": 
      xmlReader.Read(); 
      Console.WriteLine("at id "+xmlReader.Value.ToString()); 
      break; 
    } 
} 

任何指針將不勝感激。

回答

0

您可以改爲使用XmlDocument。我通常走這條路,除非我需要很快閱讀大量文件。

http://msdn.microsoft.com/en-us/library/e48zttz7.aspx將爲您提供從流中加載xml的語法。

一旦加載文檔,你可以使用XPath查詢XML http://www.w3schools.com/xpath/xpath_syntax.asp

前。

XmlDocument doc = new XmlDocument; 
    doc.Load(response.GetResponseStream); 

    XmlNode node = doc.SelectChildNode("/AddAgentResponse/agentLocalID"); 
    if(node != null) { .... } 
+0

你讓我走上正軌,歡呼聲。 – 2013-04-30 13:49:49

相關問題