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

时间:2009-03-05 21:57:42

标签: .net xml vb.net soap xpath

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

我的.NET应用程序从W3C的公共验证服务器获取此XML响应:

<?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 as String
  • Checkedby as String
  • Doctype as String
  • CharSet as String
  • 有效性为布尔值
  • ErrorList as System.Collections.Generic.List(Of W3CError)
  • WarningList as System.Collections.Generic.List(Of W3CError)

该类型W3CError是我使用以下属性创建的一个小类:

  • Line as Integer
  • Col as Integer
  • 消息为字符串
  • MessageId as String
  • 字符串解释
  • Source as String

这是我到目前为止所做的事情。但是,这不起作用 ......

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...

如何解决上面破坏的代码?或者:我是否会偏离轨道?什么是更好的方式?

3 个答案:

答案 0 :(得分:2)

你听说过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);

您的代码可能无法正常工作,因为您忽略了xml中的命名空间

答案 1 :(得分:2)

试试这个

'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 :(得分:1)

还有linq2xml。它位于System.Xml.Linq中。它有一个新的XDocument类,比旧的System.Xml.XmlDocument类更容易使用。