Solved

Reading attribute from XML using VB.Net

Posted on 2010-09-23
4
686 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article will show, step by step, how to integrate R code into a R Sweave document
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

615 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