2017-05-30 49 views
0

我有我得到返回给Web服务调用的响应XML提要:内嵌VB解析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> 

我采取的响应,并将其存储为一个名为字符串:strRead。然后我试图得到使用以下的值(没有成功):

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

我也尝试过其他方法都没有成功:

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

谁能帮助我在这一点?这是内联在一个aspx文件中。

+0

您需要使用的命名空间。 – SLaks

回答

0

这里有两个选择

 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

命名空间是关键:

Dim myNamespace As XNamespace = YourXMLNameSpace 

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