How to extract data from an XML file using Visual Basic?

I have not used XML too much and I need a little help.

My .NET application receives this XML response from the public W3C validation server:

<?xml version="1.0" encoding="UTF-8" ?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Body> <m:markupvalidationresponse env:encodingStyle="http://www.w3.org/2003/05/soap-encoding" xmlns:m="http://www.w3.org/2005/10/markup-validator"> <m:uri>upload://Form Submission</m:uri> <m:checkedby>http://validator.w3.org/</m:checkedby> <m:doctype>-//W3C//DTD XHTML 1.1//EN</m:doctype> <m:charset>utf-8</m:charset> <m:validity>true</m:validity> <m:errors> <m:errorcount>0</m:errorcount> <m:errorlist /> </m:errors> <m:warnings> <m:warningcount>0</m:warningcount> <m:warninglist /> </m:warnings> </m:markupvalidationresponse> </env:Body> </env:Envelope> 

I want to extract the following values ​​from this:

  • Uri as String
  • Checked as string
  • Doctype as string
  • Charset as string
  • Reality as logical
  • ErrorList as System.Collections.Generic.List (from W3CError)
  • WarningList as System.Collections.Generic.List (from W3CError)

This type of W3CError is a small class created with the following properties:

  • String as a whole
  • Col as integer
  • Message as a String
  • MessageId as String
  • Explanation as string
  • Source as string

That's what I went so far. But this does not work ...

  Dim ResponseReader As Xml.XmlTextReader = New Xml.XmlTextReader (ResponseStream)
 Dim ResponseDocument As New Xml.XPath.XPathDocument (ResponseReader)
 Dim ResponseNavigator As Xml.XPath.XPathNavigator = ResponseDocument.CreateNavigator ()
 Dim ResponseIterator As Xml.XPath.XPathNodeIterator

 'uri
 ResponseIterator = ResponseNavigator.Select ("uri")
 ResponseIterator.MoveNext ()
 _Uri = ResponseIterator.Current.Value

 'checked by
 ResponseIterator = ResponseNavigator.Select ("checkedby")
 ResponseIterator.MoveNext ()
 _Checkedby = ResponseIterator.Current.Value

 ... etc ...

How can I fix the broken code above? Or: Am I okay with this? What is the best way?

0
source share
3 answers

try it

 'Import these Namespaces at the top of your file Imports System.Linq Imports System.Xml.Linq Imports <xmlns:env="http://www.w3.org/2003/05/soap-envelope"> Imports <xmlns:m="http://www.w3.org/2005/10/markup-validator"> 'in a procedure do this Dim doc As XDocument = <?xml version="1.0" encoding="UTF-8" ?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Body> <m:markupvalidationresponse env:encodingStyle="http://www.w3.org/2003/05/soap-encoding" xmlns:m="http://www.w3.org/2005/10/markup-validator"> <m:uri>upload://Form Submission</m:uri> <m:checkedby>http://validator.w3.org/</m:checkedby> <m:doctype>-//W3C//DTD XHTML 1.1//EN</m:doctype> <m:charset>utf-8</m:charset> <m:validity>true</m:validity> <m:errors> <m:errorcount>0</m:errorcount> <m:errorlist /> </m:errors> <m:warnings> <m:warningcount>0</m:warningcount> <m:warninglist /> </m:warnings> </m:markupvalidationresponse> </env:Body> </env:Envelope> _Uri = doc.Root.<env:Body>.<m:markupvalidationresponse>.<m:uri>.Value _Checkedby = doc.Root.<env:Body>.<m:markupvalidationresponse>.<m:checkedby>.Value 'note that the following code assumes you have a class named W3CError _errorList = (From er in doc.Root...<m:errors> _ Select New W3CError With {.Line = CInt(er.<m:line>.Value), .Col = CInt(er.<m:col>.Value), .Message = er.<m:message>.Value, .MessageId = er.<m:messageId>.Value, .Explanation = er.<m:explanation>.Value, .Source = er.<m:source>.Value}).ToList 'do the same for the _warningList as above 'now do what you want with it 
+2
source

Have you heard of XPath?

 XmlDocument doc = new XmlDocument() doc.Load(xml) // set the namspace manager, I don't remember exact syntax .... XmlNode node = doc.SelectSingleNode("//m:checkedby", namespaceManagerThatDeclaresMNamespace); 

The code probably doesn't work because you are ignoring namespaces in xml

+2
source

There is also linq2xml. It is located in System.Xml.Linq. It has a new XDocument class that is easier to work with than the old System.Xml.XmlDocument class.

+1
source

Source: https://habr.com/ru/post/955296/


All Articles