I have an XML feed that I get returned as a response to a web service call:
<?xml version="1.0" encoding="utf-8"?>
<CustomerGetResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WHATEVER/webservice">
<UserExists>false</UserExists>
<DisableAccountFlag>false</DisableAccountFlag>
</CustomerGetResult>
I take that response and store it as a string named: strRead. I then attempted to get the values using the following (without success):
Dim XMLString = XDocument.Parse(strRead)
Response.Write("UserExists: " & XMLString.<CustomerGetResult>.<UserExists>.Value)
Response.Write("DisableAccountFlag: " & XMLString.<CustomerGetResult>.<DisableAccountFlag>.Value)
I have also tried other ways without success:
Dim doc As New System.Xml.XmlDocument()
doc.LoadXML(strRead)
dim SymbolText as String = doc.SelectSingleNode("//CustomerGetResult/UserExists").Value
Response.Write(SymbolText)
Can anyone help me at this point? This is inline inside an aspx file.
Namespace is the key:
Dim myNamespace As XNamespace = YourXMLNameSpace
For Each report As XElement In xmlr.Descendants(myNamespace + "CustomerGetResult")
Console.WriteLine(report.Element(myNamespace + "UserExists").Value)
Next
Here are two choices
Dim strRead = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<CustomerGetResult xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://WHATEVER/webservice"">" & _
"<UserExists>false</UserExists>" & _
"<DisableAccountFlag>false</DisableAccountFlag>" & _
"</CustomerGetResult>"
Dim XMLString As XDocument = XDocument.Parse(strRead)
Dim root As XElement = XMLString.Root
Dim ns As XNamespace = root.GetDefaultNamespace()
'Method 1
Console.WriteLine("UserExists: {0}", CType(XMLString.Descendants(ns + "UserExists").FirstOrDefault(), String))
Console.WriteLine("DisableAccountFlag: {0}", CType(XMLString.Descendants(ns + "DisableAccountFlag").FirstOrDefault(), String))
'Method 2
Console.WriteLine("UserExists: {0}", CType(XMLString.Descendants().Where(Function(x) x.Name.LocalName = "UserExists").FirstOrDefault(), String))
Console.WriteLine("DisableAccountFlag: {0}", CType(XMLString.Descendants().Where(Function(x) x.Name.LocalName = "DisableAccountFlag").FirstOrDefault(), String))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.