如何从使用Visual Basic中的XML文件中提取数据?

问题描述:

我没有用XML太多,我需要一点点帮助。

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

我的.NET应用程序从W3C的公开验证服务器这个XML响应:

My .NET application gets this XML response from the W3C's public 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 from this the following values:

  • 在URI作为字符串
  • 在Checkedby为String
  • 在文档类型为String
  • 在字符集为String
  • 有效期为布尔
  • 在ErrorList为System.Collections.Generic.List(中W3CError)
  • 在WarningList为System.Collections.Generic.List(中W3CError)

这类型W3CError是我具有以下属性创建了一个小型的类:

That type W3CError is a small class I created with the following properties:

  • 线为整数
  • 在山口作为整数
  • 在消息字符串
  • 的MessageId为String
  • 解释为String
  • 来源为String

下面就是我走这么远。但是,这不起作用

Here's what I've go so far. But, this doesn't 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...

我怎么能修复损坏code以上?或者:我是这样了与这条赛道?什么是更好的办法?

How can I fix the broken code above? Or: Am I way off track with this? What's a better way?

试试这个

'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