Solved

Reading attribute from XML using VB.Net

Posted on 2010-09-23
4
676 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

751 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