C#Soap获取Atribute

时间:2016-02-01 17:21:13

标签: c# vim soap

我想解析肥皂请求的一个问题,我希望从条目<returnval type="Task">haTask-2-vim.VirtualMachine.powerOn-25</returnval>获取 价值haTask-2-vim.VirtualMachine.powerOn-25和Atribute?任务,解析值是与innerxml不是问题,但我希望得到类型任务,这里是我的肥皂

    <?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<PowerOnVM_TaskResponse xmlns="urn:internalvim25"><returnval type="Task">haTask-2-vim.VirtualMachine.powerOn-25</returnval></PowerOnVM_TaskResponse>
</soapenv:Body>
</soapenv:Envelope>

我的解析代码

  StreamReader objSR = new StreamReader(response.GetResponseStream()); //get the soap from a server
            string strResponse = objSR.ReadToEnd();
              XmlReader reader = XmlReader.Create(new StringReader(strResponse));
            while (reader.Read())
            {

                Console.WriteLine( reader.Value);
            }

我的代码给了我下面的输出

version="1.0" encoding="UTF-8"




haTask-2-vim.VirtualMachine.powerOn-93

2 个答案:

答案 0 :(得分:0)

尝试以下代码

StreamReader objSR = new StreamReader(response.GetResponseStream()); //get the soap from a server
            string strResponse = objSR.ReadToEnd();
              XmlReader reader = XmlReader.Create(new StringReader(strResponse));
            while (reader.Read())
            {
                if(reader.HasAttributes)
                  Console.WriteLine(reader.GetAttribute("Type"));
                Console.WriteLine( reader.Value);
            }

答案 1 :(得分:0)

好吧,我找到了一个“更好”的解决方案,maby它的废话但工作

  StreamReader objSR = new StreamReader(response.GetResponseStream()); //get the soap from a server
            string strResponse = objSR.ReadToEnd();
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(strResponse);
            XmlNodeList name = xDoc.GetElementsByTagName("returnval");
            try
            {
                Console.WriteLine(name[0].InnerText + name[0].Attributes[0].InnerText);
                Console.ReadLine();
            }
            catch { Exception e; }