C# 肥皂获取属性

本文关键字:属性 获取 肥皂 | 更新日期: 2023-09-27 18:32:55

我想解析肥皂请求的答案,我想从条目中获取<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

C# 肥皂获取属性

尝试以下代码

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);
}

好吧,我找到了一个"更好"的解决方案,也许它是废话但可以工作

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; }