[英]Inline VB for Parsing XML
我有一个XML提要,它作为对Web服务调用的响应而返回:
<?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文件中内联。
命名空间是关键:
Dim myNamespace As XNamespace = YourXMLNameSpace
For Each report As XElement In xmlr.Descendants(myNamespace + "CustomerGetResult")
Console.WriteLine(report.Element(myNamespace + "UserExists").Value)
Next
这是两个选择
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.