Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 711
  • Last Modified:

Reading attribute from XML using VB.Net

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
dthillsr
Asked:
dthillsr
  • 3
1 Solution
 
nepaluzCommented:
To verify the supplier name in the second section, try:
Your value should be ngd(0).tostring

0
 
nepaluzCommented:
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
 
nepaluzCommented:
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
 
dthillsrAuthor Commented:
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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now