我有一个我创建的Sub,它以XML格式将特定信息返回给我。但是,有时当我将一个值传入Sub时,它返回错误的节点/元素。我想跳过这些错误节点/元素或者在它进入表之前脱离它。怎么能实现这一目标?以下是我的代码:
returnXML = getXml(ISBN)
'Assuming this is where I'm supposed to write a for each loop before going into the table to detect the error nodes?
dtReturnXMl(returnXML)
Public Sub dtReturnXML(getReturnXML As String)
dsReturn.Read(New XmlTextReader(New StringReader(getReturnXML)))
dvReturn = dsReturn.Tables("Offer").DefaultView
dvReturn.Sort = "Merchant, Condition ASC"
dtReturn = dvRturn.ToTable("Offer")
End Sub
来自returnXML的XML:
<?xml version="1.0" encoding="UTF-8" ?>
<Response>
<Errors>
<Error>
<ErrorMessage>One or more ISBNS not found</ErrorMessage>
</Error>
<Error>
<ErrorMessage>One or more ISBNS not found</ErrorMessage>
</Error>
</Errors>
</Response>
答案 0 :(得分:0)
试试这个
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Sub Main()
Dim returnXML As XDocument = getXml("ISBN")
Dim errors As String() = returnXML.Descendants("ErrorMessage").Select(Function(x) x.Value).ToArray
End Sub
Function getXml(ISBN As String) As XDocument
Dim xml As String = _
"<?xml version=""1.0"" encoding=""UTF-8""?>" & _
"<Response>" & _
"<Errors>" & _
"<Error>" & _
"<ErrorMessage>One or more ISBNS not found</ErrorMessage>" & _
"</Error>" & _
"<Error>" & _
"<ErrorMessage>One or more ISBNS not found</ErrorMessage>" & _
"</Error>" & _
"</Errors>" & _
"</Response>"
Return XDocument.Parse(xml)
End Function
End Module