2017-05-30 2 views
0

J'ai un flux XML que je suis retourné en réponse à un appel de service Web:Inline VB pour parser XML

<?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> 

Je prends cette réponse et le stocker sous forme de chaîne nommée: strRead. Je me suis alors tenté d'obtenir les valeurs en utilisant les éléments suivants (sans succès):

Dim XMLString = XDocument.Parse(strRead) 
Response.Write("UserExists: " & XMLString.<CustomerGetResult>.<UserExists>.Value) 
Response.Write("DisableAccountFlag: " & XMLString.<CustomerGetResult>.<DisableAccountFlag>.Value) 

J'ai aussi essayé d'autres moyens sans succès:

Dim doc As New System.Xml.XmlDocument() 
doc.LoadXML(strRead) 
dim SymbolText as String = doc.SelectSingleNode("//CustomerGetResult/UserExists").Value 
Response.Write(SymbolText) 

quelqu'un peut me aider à ce point? Ceci est en ligne dans un fichier aspx.

+0

Vous devez utiliser l'espace de noms. – SLaks

Répondre

0

Voici deux choix

 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)) 
0

Namespace est la clé:

Dim myNamespace As XNamespace = YourXMLNameSpace 

For Each report As XElement In xmlr.Descendants(myNamespace + "CustomerGetResult") 
    Console.WriteLine(report.Element(myNamespace + "UserExists").Value) 
Next