Basically I have an XML file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<sender sndid="FT">
<val n="name">Oy Finntack Ltd</val>
<val n="address1">Keskikankaantie 29-31</val>
<val n="zipcode">15860</val>
<val n="city">Hollola</val>
<val n="country">FI</val>
<partner parid="POSTI">
<val n="iban">FI2515193000110201</val>
<val n="bic">NDEAFIHH</val>
<val n="postgiro">15193000110201</val>
<val n="custno">632368</val>
</partner>
</sender>
<receiver rcvid="109480">
<val n="name">Petra Kern</val>
<val n="address1">Ziegelgasse 14</val>
<val n="zipcode">73525</val>
<val n="city">Schwabisch Gmund</val>
<val n="country">DE</val>
</receiver>
<shipment orderno="635023">
<val n="reference"/>
<val n="from">FT</val>
<val n="to">109480</val>
<service srvid="IT14">
<addon adnid="mprc"/>
<val n="returnlabel">no</val>
</service>
<container measure="parcel" type="parcel">
<val n="reference">PCK00000160</val>
<val n="copies">1</val>
<val n="weight">0.3</val>
<val n="contents"/>
<val n="packagecode">pc</val>
<val n="marking">horze-finntack</val>
</container>
</shipment>
I am writing a Windows service that checks a folder for XML files at a given interval, parses them one at a time for some specific information (to address, from address, and shipment contents) this data is then send via a proprietary method to be rated, shipped, and tracked. All this code works fine, my problem is accessing XML in VB .Net (I am a PHP programmer, and could have this done in 10 minutes if this was a web app). Anyways, here is some code. This Sub searches Path
for all files that end in Ext
, then for each file found will grab certain information from the XML file and send it in for processing.
Sub GetXMLFiles()
//declare some variables
Dim fName As String
Dim Files = New ArrayList
Dim I As Integer = 1
Dim dirInfo As New DirectoryInfo(Path)
Dim fileInfos() As FileInfo = dirInfo.GetFiles(Ext)
For Each file_info As FileInfo In fileInfos
fName = file_info.FullName
Try
Dim xDoc As XPathDocument = New XPathDocument(fName)
Dim xNav As XPathNavigator = xDoc.CreateNavigator()
//from here until the end of this Try statement is just a test.
Dim sender As XPathNodeIterator
sender = xNav.Select("/data/sender")
While (sender.MoveNext())
Console.WriteLine(sender.Current.Value)
End While
//end test
Catch ex As Exception
End Try
Files.Add(Path & fName)
Next file_info
End Sub
In the end this will be threaded, as the XML results will be transmitted via REST and the time it takes to process each request is indeterminable.
So here is my question/problem. After I create the Navigator, I could use XPath expressions like in the test to select a certain node. But after I select that node, I am unable to access its children by name. If you notice the XML the attributes for the <sender>
element are not named properly they are <val n="name">
What I need returned is each <val>
that is a direct child of <sender>
(the <partner>
section is omitted for my purposes). This same thing applies to the <receiver>
element. The <shipment>
element will possibly contain multiple <container>
elements, but I can deal with that once I can figure out how to return everything correctly. What I need for each <val>
in <sender>
is the n="[NAME]"
and the actual value of <val>
.
If you look at first <val>
in the <sender>
block of the provided XML I would want a to have two variables Name
and Value
that for the first example would be name
,Oy FinnTack Ltd
.
I know I am close! Sorry for the long post but I attempt to answer too many questions on stackoverflow where the person is very vague on what they are trying to do.