I am trying to read through a directory of xml files and read values from them if they exist. Here is an example XML file:
<?xml version="1.0" encoding="utf-16"?>
<SegmentRequirement xmlns:xsd="http://ift.tt/tphNwY" xmlns:xsi="http://ift.tt/ra1lAU" xmlns="Manufacturing.MESProcessApp.S95Entities">
<ID>97_Transmission Fluid Fill</ID>
<Segment>
<ID>Transmission Fluid Fill</ID>
</Segment>
<ProductionParameters>
<ProductionParameter>
<Name>Transmission_FillMaxVolumeWarning</Name>
<Description>Screen Warning if above this level</Description>
<Value>15.0</Value>
<ValueUnitOfMeasure>qt.</ValueUnitOfMeasure>
</ProductionParameter>
<ProductionParameter>
<Name>Transmission_FillMinVolumeWarning</Name>
<Description>Screen Warning if below this level</Description>
<Value>11.0</Value>
<ValueUnitOfMeasure>qt.</ValueUnitOfMeasure>
</ProductionParameter>
<ProductionParameter>
<Name>Transmission_FillSpecName</Name>
<Description>Name for users to identify specification</Description>
<Value>Eaton-Fuller Standar</Value>
<ValueUnitOfMeasure></ValueUnitOfMeasure>
</ProductionParameter>
</ProductionParameters>
</SegmentRequirement>
And here is the code I'm trying to use.
Module Module1
Sub Main()
Dim Transmission_FillSpecName As XmlNode
Dim m_xmld As XmlDocument
Dim di As New DirectoryInfo("C:\MESChassisXmlFiles\MESChassisXmlFiles\Transmission Fluid Fill\")
' Get a reference to each file in that directory.
Dim fiArr As FileInfo() = di.GetFiles()
' Display the names of the files.
Dim fri As FileInfo
For Each fri In fiArr
m_xmld = New XmlDocument()
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(m_xmld.NameTable)
nsmgr.AddNamespace(String.Empty, "Manufacturing.MESProcessApp.S95Entities")
nsmgr.AddNamespace("xsi", "http://ift.tt/ra1lAU")
nsmgr.AddNamespace("xsd", "http://ift.tt/tphNwY")
m_xmld.Load(fri.FullName.ToString)
Console.WriteLine("Loaded: " + fri.Name.ToString)
Dim root As XmlElement = m_xmld.DocumentElement
Try
Transmission_FillSpecName = root.SelectSingleNode("/SegmentRequirement/ProductionParameters/ProductionParameter[Name='Transmission_FillSpecName']/Value", nsmgr)
Console.WriteLine(Transmission_FillSpecName.InnerText)
Catch ex As Exception
Console.WriteLine("Not Found!")
End Try
Next fri
Console.WriteLine()
Console.Write("Press enter to continue...")
Console.ReadLine()
End Sub
End Module
The problem I'm having is that it returns nothing for SelectSingleNode. I've read a lot about using to namespace manager but still haven't gotten it to work.
I have test the xpath using XPath Visualizer and it select the correct node but the vb code can't find it. I'd prefer to use xpath like this because the position of this element might not always be the same and it might not be included at all.
No comments:
Post a Comment