Имеется xml:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<InvokeMethodResponse xmlns="http://sameurl.com/gateway">
<InvokeMethodResult>
<DictionaryItem>
<Name>dbresult</Name>
<Value>0</Value>
</DictionaryItem>
<DictionaryItem>
<Name>result</Name>
<Value>300_300</Value>
</DictionaryItem>
<DictionaryItem>
<Name>customer</Name>
<Value>vasya</Value>
</DictionaryItem>
<DictionaryItem>
<Name>age</Name>
<Value>26</Value>
</DictionaryItem>
</InvokeMethodResult>
</InvokeMethodResponse>
</soap:Body>
</soap:Envelope>
пытаюсь выбрать
/soap:Envelope/soap:Body/InvokeMethodResponse/InvokeMethodResult/DictionaryItem[Name="dbresult"]/Value
но возвращается null
Код:
XmlDocument readData = XmlDocument ;
readData.Load(response.GetResponseStream());
XmlNamespaceManager nsmgr = new XmlNamespaceManager(readData.NameTable);
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.PushScope();
nsmgr.AddNamespace("imr", "http://sameurl.com/gateway");
MessageBox.Show(readData.SelectSingleNode("/soap:Body/imr:InvokeMethodResponse/imr:InvokeMethodResult/imr:DictionaryItem[Name=\"dbresult\"]/imr:Value", nsmgr).Value);
Помогите разобраться.
Name тоже нужно указывать пространство имен./text()./soap:Envelope/soap:Body/imr:InvokeMethodResponse/imr:InvokeMethodResult/imr:DictionaryItem[imr:Name='dbresult']/imr:Value/text()
То же самое через Linq2Xml делается несколько проще:
var imr = XNamespace.Get("http://sameurl.com/gateway");
var xml = XDocument.Load(response.GetResponseStream());
var dbresult = xml.Descendants(imr + "DictionaryItem")
.SingleOrDefault(x => (string)x.Element(imr + "Name") == "dbresult");
MessageBox.Show((string)dbresult?.Element(imr + "Value"));
Современные инструменты для криптотрейдинга: как технологии помогают принимать решения
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости