Solved Help reading an XML document

Microsoft Visual studio professional 201...
April 2, 2013 at 16:38:25
Specs: Windows 7, 4.17Ghz 8GB ram
I have an XML Document that looks like this:

<PayField>
	<PayFieldNumber>0</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id"></PayFieldAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>1</JobPayFieldNumber>
	<JobID xsi:type="paw:id">0000-01,B-Stuff</JobID>
	<JobPayFieldHours>10.00</JobPayFieldHours>
	<JobPayFieldAmount>10.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>

When I try to read the PayField it does not show the element "JobID" Im sure that has something to do with "xsi:type" but I cant figure out how to get vb.net to read it right. Currently I have this

Dim doc As New System.Xml.XmlDocument
        doc.Load("C:\Users\User\teststuff.xml")
        Dim list = doc.GetElementsByTagName("PayField")
        ListView1.Columns.Add("Test")
        For Each item As System.Xml.XmlElement In list
            ListView1.Items.Add(item("JobPayFieldHours").InnerText)
        Next

If i change "JobPayFieldHours" to "JobID" I get an error: "Object reference not set to an instance of an object."

Any help would be greatly appreciated.

If there is any way to inner join to JobID that would work best I think.


See More: Help reading an XML document

Report •

✔ Best Answer
April 7, 2013 at 10:56:18
I'm not entirely sure why I'm writing your XML parser for you, but here you go. Pass empJobIDs around as needed.
Dim empJobIDs = New Dictionary(Of String, List(Of String))
For Each emp As XmlNode In xmlDoc.GetElementsByTagName("PAW_PayrollEntry")
    Dim empID = emp.SelectSingleNode("EmployeeID/text()").Value
    If Not empJobIDs.ContainsKey(empID) Then _
        empJobIDs(empID) = New List(Of String)
    Dim jobList = empJobIDs(empID)
    For Each job As XmlNode In emp.SelectNodes(".//JobID/text()")
        jobList.Add(job.Value)
    Next
Next

How To Ask Questions The Smart Way



#1
April 3, 2013 at 10:59:07
I have an XML Document that looks like this:
Well obviously not: Exception calling "LoadXml" with "1" argument(s): "'xsi' is an undeclared namespace. Line 3, position 19."

If i change "JobPayFieldHours" to "JobID" I get an error
So towards the end of that function, add something like:

For Each item As System.Xml.XmlElement In list(0).ChildNodes
  Debug.Print(item.Name)
Next
Then check the Immediate window after that function runs, and that'll tell you what you should be using.

How To Ask Questions The Smart Way


Report •

#2
April 4, 2013 at 08:30:17
Sorry that was a snip it of the document the top part if fairly big but here it is:

<PAW_PayrollEntries xmlns:paw="urn:schemas-peachtree-com/paw8.02-datatypes"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2000/10/XMLSchema-datatypes">
<PAW_PayrollEntry xsi:type="paw:payrollentry">
	<EmployeeID xsi:type="paw:id">Test</EmployeeID>
	<EmployeeName>Test</EmployeeName>
	<Date xsi:type="paw:date">3/21/13</Date>
	<CashAccount xsi:type="paw:id">1010</CashAccount>
	<CashAmount>0.00</CashAmount>
	<PayPeriodEnd xsi:type="paw:date">3/21/13</PayPeriodEnd>
	<WeeksInPayPeriod>1</WeeksInPayPeriod>
	<Hours1Salary1Amount>0.00</Hours1Salary1Amount>
	<Hours2Salary2Amount>0.00</Hours2Salary2Amount>
	<Hours3Salary3Amount>0.00</Hours3Salary3Amount>
	<Hours4Salary4Amount>0.00</Hours4Salary4Amount>
	<Hours5Salary5Amount>0.00</Hours5Salary5Amount>
	<Hours6Salary6Amount>0.00</Hours6Salary6Amount>
	<Hours7Salary7Amount>0.00</Hours7Salary7Amount>
	<Hours8Salary8Amount>0.00</Hours8Salary8Amount>
	<Hours9Salary9Amount>0.00</Hours9Salary9Amount>
	<Hours10Salary10Amount>0.00</Hours10Salary10Amount>
	<Hours11Salary11Amount>0.00</Hours11Salary11Amount>
	<Hours12Salary12Amount>0.00</Hours12Salary12Amount>
	<Hours13Salary13Amount>0.00</Hours13Salary13Amount>
	<Hours14Salary14Amount>0.00</Hours14Salary14Amount>
	<Hours15Salary15Amount>0.00</Hours15Salary15Amount>
	<Hours16Salary16Amount>0.00</Hours16Salary16Amount>
	<Hours17Salary17Amount>0.00</Hours17Salary17Amount>
	<Hours18Salary18Amount>0.00</Hours18Salary18Amount>
	<Hours19Salary19Amount>0.00</Hours19Salary19Amount>
	<Hours20Salary20Amount>0.00</Hours20Salary20Amount>
	<IsBeginnigBalanceTransaction>FALSE</IsBeginnigBalanceTransaction>
	<EmployeeFirstName>Test</EmployeeFirstName>
	<PayrollPayMethod>8</PayrollPayMethod>
	<NumberOfLaborDistributions>3</NumberOfLaborDistributions>
	<Number_of_Distributions>11</Number_of_Distributions>
	<PayFields>
	<PayField>
	<PayFieldNumber>1</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id">4400</PayFieldAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobPayFieldHours>0.00</JobPayFieldHours>
	<JobPayFieldAmount>0.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>2</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id">4400</PayFieldAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobPayFieldHours>0.00</JobPayFieldHours>
	<JobPayFieldAmount>0.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>3</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id">4400</PayFieldAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobPayFieldHours>0.00</JobPayFieldHours>
	<JobPayFieldAmount>0.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>22</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id">2210</PayFieldAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobPayFieldHours>0.00</JobPayFieldHours>
	<JobPayFieldAmount>0.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>23</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id">2210</PayFieldAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobPayFieldHours>0.00</JobPayFieldHours>
	<JobPayFieldAmount>0.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>24</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id">2210</PayFieldAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobPayFieldHours>0.00</JobPayFieldHours>
	<JobPayFieldAmount>0.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>25</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id">2210</PayFieldAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobPayFieldHours>0.00</JobPayFieldHours>
	<JobPayFieldAmount>0.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>61</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id">2210</PayFieldAccount>
	<PayFieldExpenseAccount xsi:type="paw:id">4510</PayFieldExpenseAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobPayFieldHours>0.00</JobPayFieldHours>
	<JobPayFieldAmount>0.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>62</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id">2210</PayFieldAccount>
	<PayFieldExpenseAccount xsi:type="paw:id">4510</PayFieldExpenseAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobPayFieldHours>0.00</JobPayFieldHours>
	<JobPayFieldAmount>0.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>63</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id">2210</PayFieldAccount>
	<PayFieldExpenseAccount xsi:type="paw:id">4510</PayFieldExpenseAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobPayFieldHours>0.00</JobPayFieldHours>
	<JobPayFieldAmount>0.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>64</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id">2210</PayFieldAccount>
	<PayFieldExpenseAccount xsi:type="paw:id">4510</PayFieldExpenseAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobPayFieldHours>0.00</JobPayFieldHours>
	<JobPayFieldAmount>0.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>0</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id"></PayFieldAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>0</JobPayFieldNumber>
	<JobID xsi:type="paw:id">0000-01,B-HSM</JobID>
	<JobPayFieldHours>10.00</JobPayFieldHours>
	<JobPayFieldAmount>20.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>0</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id"></PayFieldAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>1</JobPayFieldNumber>
	<JobID xsi:type="paw:id">0000-01,B-Mold</JobID>
	<JobPayFieldHours>10.00</JobPayFieldHours>
	<JobPayFieldAmount>10.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
	<PayField>
	<PayFieldNumber>0</PayFieldNumber>
	<PayFieldAccount xsi:type="paw:id"></PayFieldAccount>
	<PayFieldAmount>0.00</PayFieldAmount>
	<PayFieldMemoAmount>0.00</PayFieldMemoAmount>
	<JobPayFieldNumber>2</JobPayFieldNumber>
	<JobID xsi:type="paw:id">0000-01,B-Project</JobID>
	<JobPayFieldHours>10.00</JobPayFieldHours>
	<JobPayFieldAmount>20.00</JobPayFieldAmount>
	<LaborBurdenPercent>0.00</LaborBurdenPercent>
	<LaborBurdenAmount>0.00</LaborBurdenAmount>
	<UsedForReimbursableExpense>FALSE</UsedForReimbursableExpense>
	</PayField>
</PayFields></PAW_PayrollEntry>
</PAW_PayrollEntries>



Report •

#3
April 4, 2013 at 13:23:20
You do realize only the last three PayField elements have a JobID element, right?

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
April 4, 2013 at 14:49:26
Yes I do. It can vary between 1 and 10+ pay fields. What I need from it is the employee I'd and any pay fields with jobID

Report •

#5
April 4, 2013 at 16:09:29
So you're looking for PayFields with JobIDs? Easy enough to do. Just use System.Xml.XmlDocument.SelectNodes("//PayFields//JobID/.."). If you want all PayFields, you'll have to check for the existence of a JobID child node before you attempt to use JobID's value.

How To Ask Questions The Smart Way


Report •

#6
April 4, 2013 at 17:49:13
That makes a little bit more sense. But I'm still unsure on how to get the EmployeeID the JobID associated with it. I'm trying to output it into a listview.

I see how to get them separate. But I'm not sure how to get what JobID's go with what employee.

'Note the xml document has multiple "PAW_PayrollEntry" fields.


Report •

#7
April 7, 2013 at 10:56:18
✔ Best Answer
I'm not entirely sure why I'm writing your XML parser for you, but here you go. Pass empJobIDs around as needed.
Dim empJobIDs = New Dictionary(Of String, List(Of String))
For Each emp As XmlNode In xmlDoc.GetElementsByTagName("PAW_PayrollEntry")
    Dim empID = emp.SelectSingleNode("EmployeeID/text()").Value
    If Not empJobIDs.ContainsKey(empID) Then _
        empJobIDs(empID) = New List(Of String)
    Dim jobList = empJobIDs(empID)
    For Each job As XmlNode In emp.SelectNodes(".//JobID/text()")
        jobList.Add(job.Value)
    Next
Next

How To Ask Questions The Smart Way


Report •

#8
April 9, 2013 at 12:45:50
Thank you, That worked and I wasn't really looking for you to just give me the answer i just wasn't sure how to loop through all the EmployeeID nodes then loop through all the JobID nodes for that employee. Mainly my issue was i wasn't sure how to work the select single node. But I have it now and I see my self using this a lot in the future.

Thank you


Report •

#9
April 9, 2013 at 13:23:39
For an idea of what I'm doing, look up XPath.

How To Ask Questions The Smart Way


Report •

Ask Question