我正在尝试读取 xml 文件的版本:
<Order xsi:schemaLocation="urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01 order-v3.xsd urn:schemas-bossfed-co-uk:OP-Order-v1 OP-Order-v1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01">
<OrderHead>
<Schema>
<Version>3.05</Version>
</Schema>
<Parameters>
<Language>en-GB</Language>
<DecimalSeparator>.</DecimalSeparator>
<Precision>12.1</Precision>
</Parameters>
<OrderCurrency>
<Currency Code="GBP">GB Pounds</Currency>
</OrderCurrency>
</OrderHead>
我尝试使用的代码是:
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
'Create the XML Document
m_xmld = New XmlDocument()
'Load the Xml file
m_xmld.Load(fileLocation)
'Show all data in your xml
MessageBox.Show(m_xmld.OuterXml)
'Get the list of name nodes
m_nodelist = m_xmld.SelectNodes("/Order/OrderHead/Schema")
For Each m_node In m_nodelist
GetXmlVersion = m_node.Attributes.GetNamedItem("Version").Value
Next
但是,它不会拉任何东西。
我将其更改为:
Public Shared Function GetXmlVersion (ByVal fileLocation As String) As String
Dim m_xmld As XmlDocument = New XmlDocument()
m_xmld.Load (fileLocation)
GetXmlVersion = m_xmld.SelectSingleNode ("/Order/OrderHead/Schema/Version").InnerText
End Function
我只是得到
NullReferenceException 是未处理的对象引用未设置为对象的实例。
因为 m_xmld.selectsinglenode 为空