Solved

Reading attribute from XML using VB.Net

Posted on 2010-09-23
4
647 Views
Last Modified: 2012-05-10
I need help reading a value from an XML.

Below is the XML I am using.

  <?xml version="1.0" encoding="ISO-8859-1" ?>
- <Documents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ReadsoftInvoice.xsd">
- <Document Version="1.0" Name="XML Invoice" Type="Invoice" DocumentReference="252E00010.TIF" ImageReference="" CertificateReference="">
- <Invoice>
- <Supplier>
  <Name>ox2p</Name>
  <Identifier />
  <Number />
  <Description />
  <CorporateGroupNumber />
  <POBox />
  <Street />
  <StreetSupplement />
  <PostalCode />
  <City />
  <CountryCode />
  <CountryName />
  <TelephoneNumber />
  <FaxNumber />
  <VATRegistrationNumber />
  </Supplier>
- <Buyer>
  <Name>SHIRE</Name>
  <Identifier />
  <Number>1</Number>
  <Description />
  <CorporateGroupNumber>1</CorporateGroupNumber>
  <POBox />
  <Street />
  <StreetSupplement />
  <PostalCode />
  <City />
  <CountryCode>USA</CountryCode>
  <CountryName />
  <TelephoneNumber />
  <FaxNumber />
  <VATRegistrationNumber />
  </Buyer>
- <Fields>
  <Field Name="Debit/Credit" Type="invoicecredit">Debit</Field>
  <Field Name="InvoiceNumber" Type="invoicenumber">461</Field>
  <Field Name="InvoiceDate" Type="invoicedate">20100909</Field>
  <Field Name="Currency" Type="invoicecurrency">GBP</Field>
  <Field Name="PONumber" Type="invoiceordernumber">8000046100</Field>
  <Field Name="ShireLegalEntityName" Type="shire legal entity name">Shire Pharmaceuticals Services Ltd</Field>
  <Field Name="IBAN" Type="iban">GB97LOYD30904201447694</Field>
  <Field Name="SortCode" Type="sortcode">30-90-42</Field>
  <Field Name="UtilityAccountNumber" Type="utility account number" />
  <Field Name="Freight" Type="freight">0.00</Field>
  <Field Name="AdditionalCharges" Type="additional charges">0.00</Field>
  <Field Name="TotalFreight" Type="total freight">0.00</Field>
  <Field Name="BankAccount" Type="supplieraccountnumber1">1447694</Field>
  <Field Name="RemitToZipCode" Type="supplieraddresspostalcode">OX16 9UX</Field>
  <Field Name="NetAmount" Type="invoicetotalvatexcludedamount">8490.00</Field>
  <Field Name="VATAmount" Type="invoicetotalvatamount">1485.75</Field>
  <Field Name="TotalAmount" Type="invoicetotalvatincludedamount">9975.75</Field>
  <Field Name="VatRegNum" Type="vatregnumbers">630 5503 74</Field>
  </Fields>
- <Tables>
- <Table Type="LineItem">
- <TableRow>
  <Field Name="Description" Type="lit_articlename">Part payment for EuSEM Stockholm 2010</Field>
  <Field Name="Quantity" Type="lit_deliveredquantity" />
  <Field Name="UnitPrice" Type="lit_deliveredquantityunitcode" />
  <Field Name="ExtendedAmount" Type="extended amount">8 490.00</Field>
  </TableRow>
  </Table>
  <Table Type="BankAccountTable" />
  <Table Type="VATTable" />
  </Tables>
  </Invoice>
- <System>
  <Field Type="Pages">\\indylab-readsof\Shire\Output\SP_UK\252E00010.000</Field>
  <Field Type="Appendices" />
  <Field Type="BatchDateTime" />
  <Field Type="BatchPrefix" />
  <Field Type="BatchNumber" />
  <Field Type="BatchIndex">0</Field>
  <Field Type="DateTime">20100909 08:59:36</Field>
  <Field Type="Currency">GBP</Field>
  <Field Type="CreditInvoice">false</Field>
  <Field Type="Description" />
  <Field Type="Originator" />
  <Field Type="Title" />
  <Field Type="Identification" />
  <Field Type="ImageFile1">\\indylab-readsof\Shire\Output\SP_UK\252E00010.000</Field>
  <Field Type="ImageFile2">\\indylab-readsof\Shire\Output\SP_UK</Field>
  <Field Type="NumberOfPages">1</Field>
  <Field Type="NumberOfPagesFrontAndBack">1</Field>
  <Field Type="NumberOfInvoicePages">1</Field>
  <Field Type="NumberOfInvoicePagesFrontAndBack">1</Field>
  <Field Type="NumberOfAppendixPages">0</Field>
  <Field Type="NumberOfAppendixPagesFrontAndBack">0</Field>
  <Field Type="ProfileName">SP_UK</Field>
  <Field Type="EndorserNumber" />
  <Field Type="Status">Approved</Field>
  <Field Type="Supplier">ox2p</Field>
  <Field Type="SupplierDescription" />
  <Field Type="UserName">Tiffany Glines</Field>
  <Field Type="UserRemark" />
  <Field Type="GUID">{721CEF15-F6E5-4DA2-BCE1-1C818247D921}</Field>
  </System>
- <ProcessLog>
- <ProcessMessage>
  <TimeStamp>2010-09-09 08:59:50</TimeStamp>
  <Type>System</Type>
  <Module>Interpret</Module>
  <Action>Interpreted</Action>
  <Owner>administrator</Owner>
  <sMessage />
  </ProcessMessage>
- <ProcessMessage>
  <TimeStamp>2010-09-09 09:37:55</TimeStamp>
  <Type>System</Type>
  <Module>Verify</Module>
  <Action>Approved</Action>
  <Owner>Tiffany Glines</Owner>
  <sMessage />
  </ProcessMessage>
  </ProcessLog>
  </Document>
  </Documents>

I am able to read the name value from this section;

- <Invoice>
- <Supplier>
  <Name>ox2p</Name>

using this cose;

Dim doc As New XmlDocument
        doc.Load("C:\Shire\UK\UK\1D789FA5_C7DD_4192_81B1_98BDECBBA861.xml")
        For Each node As XmlNode In doc.SelectNodes("//Invoice")
            Dim SupplierName As String = ""
            Dim supplier As XmlNode = node.SelectSingleNode("Supplier/Name")
            If Not supplier Is Nothing Then
                SupplierName = supplier.InnerText()
            End If
            spl = SupplierName
        Next node

However I also need to be able to verify the supplier name in the following section and I cannot figure out how to do it.

<System>
  <Field Type="Pages">\\indylab-readsof\Shire\Output\SP_UK\252E00010.000</Field>
  <Field Type="Appendices" />
  <Field Type="BatchDateTime" />
  <Field Type="BatchPrefix" />
  <Field Type="BatchNumber" />
  <Field Type="BatchIndex">0</Field>
  <Field Type="DateTime">20100909 08:59:36</Field>
  <Field Type="Currency">GBP</Field>
  <Field Type="CreditInvoice">false</Field>
  <Field Type="Description" />
  <Field Type="Originator" />
  <Field Type="Title" />
  <Field Type="Identification" />
  <Field Type="ImageFile1">\\indylab-readsof\Shire\Output\SP_UK\252E00010.000</Field>
  <Field Type="ImageFile2">\\indylab-readsof\Shire\Output\SP_UK</Field>
  <Field Type="NumberOfPages">1</Field>
  <Field Type="NumberOfPagesFrontAndBack">1</Field>
  <Field Type="NumberOfInvoicePages">1</Field>
  <Field Type="NumberOfInvoicePagesFrontAndBack">1</Field>
  <Field Type="NumberOfAppendixPages">0</Field>
  <Field Type="NumberOfAppendixPagesFrontAndBack">0</Field>
  <Field Type="ProfileName">SP_UK</Field>
  <Field Type="EndorserNumber" />
  <Field Type="Status">Approved</Field>
  <Field Type="Supplier">ox2p</Field>
  <Field Type="SupplierDescription" />
  <Field Type="UserName">Tiffany Glines</Field>
  <Field Type="UserRemark" />
  <Field Type="GUID">{721CEF15-F6E5-4DA2-BCE1-1C818247D921}</Field>
  </System>

I appreciate any help you can give me on this.
0
Comment
Question by:dthillsr
  • 3
4 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 33744286
To verify the supplier name in the second section, try:
Your value should be ngd(0).tostring

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33744291
heres the code:
Dim StockDoc As XDocument = XDocument.Load("C:\Shire\UK\UK\1D789FA5_C7DD_4192_81B1_98BDECBBA861.xml")

Dim ngd = From pk In StockDoc.Root.Descendants("Field") Where pk.@<Type> = "Supplier" Select pk.Value

Open in new window

0
 
LVL 17

Accepted Solution

by:
nepaluz earned 50 total points
ID: 33744436
Both your queries could be resolved with the following:

Both ngf(0).ToString and ngd(0).ToString produce a value of ox2p
PS. I had to remove the encoding="ISO-8859-1" from the xml header to continue.
 

Dim StockDoc As XDocument = XDocument.Load("C:\Shire.xml")

Dim ngf = From kp In StockDoc.Root.Descendants("Supplier") Select kp.<Name>.Value

Dim ngd = From pk In StockDoc.Root.Descendants("Field") Where pk.@<Type> = "Supplier" Select pk.Value

Open in new window

0
 

Author Closing Comment

by:dthillsr
ID: 33746500
The answer by nepaluz answered my question. The others did not because I failed to mention on my part that I will have to look through several hundred xml's so I would not be able to load the exact xml name. I appreciate everyones help in this matter
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 111
maven archtype selection in eclipse 1 53
sameEnds challenge 25 83
maxMirror challenge 10 115
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now