.net 如何使用Visual Basic从XML文件中提取数据?
我没有太多地使用XML,我需要一些帮助 My.NET应用程序从W3C的公共验证服务器获取以下XML响应:.net 如何使用Visual Basic从XML文件中提取数据?,.net,xml,vb.net,soap,xpath,.net,Xml,Vb.net,Soap,Xpath,我没有太多地使用XML,我需要一些帮助 My.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
<?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>
upload://Form 服从
http://validator.w3.org/
-//W3C//DTD XHTML 1.1///EN
utf-8
真的
0
0
我想从中提取以下值:
- Uri作为字符串
- 作为字符串被选中
- Doctype作为字符串
- 字符集为字符串
- 布尔有效性
- 错误列表为System.Collections.Generic.List(W3CError的)
- 警告列表作为System.Collections.Generic.List(W3CError的)
- 作为整数的行
- 列为整数
- 消息作为字符串
- MessageId作为字符串
- 解释为字符串
- 源作为字符串
如何修复上面的坏代码?或者:我是否偏离了轨道?有什么更好的方法吗?您听说过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中的名称空间。您听说过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中的名称空间,而且还有linq2xml。它位于System.Xml.Linq中。它有一个新的XDocument类,比旧的System.Xml.XmlDocument类更易于使用。还有一个linq2xml。它位于System.Xml.Linq中。它有一个新的XDocument类,比旧的System.Xml.XmlDocument类更容易使用。试试这个
'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
“在文件顶部导入这些名称空间
导入系统
导入System.Xml.Linq
进口
进口
“在程序中,请执行此操作
作为XDocument的Dim doc=
upload://Form 服从
http://validator.w3.org/
-//W3C//DTD XHTML 1.1///EN
utf-8
真的
0
0
_Uri=doc.Root…值
_Checkedby=doc.Root…值
'注意,下面的代码假定您有一个名为W3CError的类
_errorList=(来自doc.Root中的er_
选择带有{.Line=CInt(er..Value),.Col=CInt(er..Value),.Message=er..Value、.MessageId=er..Value、.explainion=er..Value、.Source=er..Value})的新W3CError。ToList
'对上述警告列表执行相同操作
“现在你想用它做什么就做什么
试试这个
'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
“在文件顶部导入这些名称空间
导入系统
导入System.Xml.Linq
进口
进口
“在程序中,请执行此操作
作为XDocument的Dim doc=
upload://Form 服从
http://validator.w3.org/
-//W3C//DTD XHTML 1.1///EN
utf-8
真的
0
0
_Uri=doc.Root…值
_Checkedby=doc.Root…值
'注意,下面的代码假定您有一个名为W3CError的类
_errorList=(来自doc.Root中的er_
选择带有{.Line=CInt(er..Value),.Col=CInt(er..Value),.Message=er..Value、.MessageId=er..Value、.explainion=er..Value、.Source=er..Value})的新W3CError。ToList
'对上述警告列表执行相同操作
“现在你想用它做什么就做什么
谢谢。这正是我需要的。谢谢。这正是我需要的。您有可用的WSDL吗?VS应该能够自动为web服务公开的数据类型生成类,这样您就不必手动解析SOAP输出。您有可用的WSDL吗?VS应该能够自动为web服务公开的数据类型生成类,这样您就不必手动解析SOAP输出。