【问题标题】:How do I extract data from an XML file with Visual Basic?如何使用 Visual Basic 从 XML 文件中提取数据?
【发布时间】:2009-03-05 21:57:41
【问题描述】:

我没有过多地使用 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 作为字符串
  • Checkedby 作为字符串
  • 文档类型为字符串
  • CharSet 为字符串
  • 布尔值的有效性
  • ErrorList as System.Collections.Generic.List(Of W3CError)
  • WarningList as System.Collections.Generic.List(Of W3CError)

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

  • 行为整数
  • Col 作为整数
  • 字符串形式的消息
  • MessageId 作为字符串
  • 字符串解释
  • 来源为字符串

这是我到目前为止所做的。但是,这行不通...

Dim ResponseReader As Xml.XmlTextReader = New Xml.XmlTextReader(ResponseStream) 将 ResponseDocument 变暗为新的 Xml.XPath.XPathDocument(ResponseReader) 将 ResponseNavigator 调暗为 Xml.XPath.XPathNavigator = ResponseDocument.CreateNavigator() Dim ResponseIterator As Xml.XPath.XPathNodeIterator 乌里 ResponseIterator = ResponseNavigator.Select("uri") ResponseIterator.MoveNext() _Uri = ResponseIterator.Current.Value '通过检查 ResponseIterator = ResponseNavigator.Select("checkedby") ResponseIterator.MoveNext() _Checkedby = ResponseIterator.Current.Value ...ETC...

如何修复上面的损坏代码?或者:我是否偏离了轨道?有什么更好的方法?

【问题讨论】:

  • 您有可用的 WSDL 吗? VS 应该能够为您自动为 Web 服务公开的数据类型生成类,因此您不必手动解析 SOAP 输出。

标签: .net xml vb.net soap xpath


【解决方案1】:

试试这个

'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】:

您听说过 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 中的命名空间

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多