Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading attribute from XML using VB.Net

Posted on 2010-09-23
4
Medium Priority
?
695 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 200 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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.
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

661 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