有没有更好的方法来处理SOAP POST响应 - 遍历XML

时间:2013-04-29 12:19:04

标签: c# soap xml-parsing

我继承了C#代码,在基本的XML处理方面遇到了一些麻烦。我更习惯在Java中使用JDOM。我还阅读了许多不同的教程,到目前为止,没有人帮助过。

代码行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;
    }
}

任何指针都会非常感激。

1 个答案:

答案 0 :(得分: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) { .... }
相关问题