My problem is I want to be able to read through this xml document and populate a winform before validating it and then storing it into a table. This is a snippet of the xml document, it's way too big to put it all here.

- <Applicant>
+ <common:PersonName>
  <pdt:PersonNameTitle>Mrs</pdt:PersonNameTitle> 
  <pdt:PersonGivenName>Helen</pdt:PersonGivenName> 
  <pdt:PersonFamilyName>Smith</pdt:PersonFamilyName> 
  </common:PersonName>
  <common:OrgName>Royal Borough of Kingston</common:OrgName> 
+ <common:ExternalAddress>
- <common:InternationalAddress>
  <apd:IntAddressLine>Learning & Children's Services</apd:IntAddressLine> 
  <apd:IntAddressLine>High Road</apd:IntAddressLine> 
  <apd:IntAddressLine>Noth London</apd:IntAddressLine> 
  <apd:IntAddressLine>Edmonton</apd:IntAddressLine> 
  <apd:IntAddressLine /> 
  <apd:Country /> 
  <apd:InternationalPostCode>JT1 1EU</apd:InternationalPostCode> 
  </common:InternationalAddress>
  </common:ExternalAddress>
- <common:ContactDetails PreferredContactMedium="Telephone">
- <common:Email EmailPreferred="no" EmailUsage="work">
  <apd:EmailAddress>helen.smith@someemail.com</apd:EmailAddress> 
  </common:Email>
- <common:Telephone TelMobile="no" TelPreferred="yes" TelUse="work">
  <apd:TelNationalNumber>020 888 8888</apd:TelNationalNumber> 
  </common:Telephone>
- <common:Telephone TelMobile="yes" TelPreferred="no" TelUse="work">
  <apd:TelNationalNumber /> 
  </common:Telephone>
- <common:Fax FaxMobile="no" FaxPreferred="no" FaxUse="work">
  <apd:FaxNationalNumber /> 
  </common:Fax>
  </common:ContactDetails>
  </Applicant>
<FileAttachments>
+ <common:FileAttachment>
  <common:Identifier>3893714</common:Identifier> 
  <common:Size>128705</common:Size> 
  <common:MimeType>application/pdf</common:MimeType> 
  <common:FileName>5254_091211-hp-nl.pdf</common:FileName> 
- <common:PrintInformation>
- <common:PaperSize>
  <common:StandardSize>A4</common:StandardSize> 
  </common:PaperSize>
  <common:HasBeenPrinted>true</common:HasBeenPrinted> 
  <common:HasScale>false</common:HasScale> 
  </common:PrintInformation>
  <common:Reference>5254/091211-hp-nl</common:Reference> 
  </common:FileAttachment>
+ <common:FileAttachment>
  <common:Identifier>3893394</common:Identifier> 
  <common:Size>237683</common:Size> 
  <common:MimeType>application/pdf</common:MimeType> 
  <common:FileName>5254_1000a_091214170715217.pdf</common:FileName> 
- <common:PrintInformation>
- <common:PaperSize>
  <common:StandardSize>A3</common:StandardSize> 
  </common:PaperSize>
  <common:HasBeenPrinted>true</common:HasBeenPrinted> 
  <common:HasScale>true</common:HasScale> 
  </common:PrintInformation>
  <common:Reference>5254/1000a</common:Reference> 
  </common:FileAttachment>
+ <common:FileAttachment>

So far to retrieve the code this is how I have been doing it

Dim elm = _doc.DocumentElement
        Dim nl = elm.ChildNodes


        Dim rootElement As XmlElement, xmlList As XmlNodeList
        Dim nsmr As New XmlNamespaceManager(_doc.NameTable)
        nsmr.AddNamespace("common", "http://www.mywebsite")
        nsmr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
        nsmr.AddNamespace("apd", "http://www.Mywebsite/AddressAndPersonalDetails")
        nsmr.AddNamespace("bs7666", "http://www.Mywebsite/people/bs7666")
        nsmr.AddNamespace("core", "http://www.Mywebsite/core")
        nsmr.AddNamespace("glm", "http://www.Mywebsite/gml")
        nsmr.AddNamespace("mdc", "http://www.Mywebsite/mdces-2006")
        nsmr.AddNamespace("org", "www.Mywebsite/financial/OrganisationIdentifiers")
        nsmr.AddNamespace("xlink", "http://www.w3.org/1999/xlink")

    If (_doc.GetElementsByTagName("ProposalDescription").Count > 0) Then
            Dim nlAppData = _doc.GetElementsByTagName("ProposalDescription")
            txtPropDesc.Text = nlAppData(0).ChildNodes(0).FirstChild.InnerText
            txtPropStarted.Text = nlAppData(0).ChildNodes(1).FirstChild.InnerText
            txtPropCompleted.Text = nlAppData(0).ChildNodes(1).ChildNodes(0).FirstChild.InnerText()

        End If

What I want to do is something like
dim faList as List of common:FileAttachment
For Each Dim cfa as common:FileAttachment in FileAttachments
faList.add(cfa)
next

'Then

DataGridView1.Datasource = faList

Please, please please help guide me in the right direction

Recommended Answers

All 2 Replies

Look into LINQ to XML!

You can use it to load XML files or parse XML strings (with XML namespaces) into collections of defined or anonymous types.

Example:

Imports System.Linq
Imports System.Xml.Linq

Module Module1

    Sub Main()

        Dim xml As String = "<?xml version=""1.0"" encoding=""utf-8"" ?>" & _
"<Cars>" & _
  "<Car>" & _
    "<Make>Ford</Make>" & _
    "<Model>Fusion</Model>" & _
    "<Color>Blue</Color>" & _
    "<Year>2010</Year>" & _
  "</Car>" & _
  "<Car>" & _
    "<Make>Toyota</Make>" & _
    "<Model>Prius</Model>" & _
    "<Color>White</Color>" & _
    "<Year>2009</Year>" & _
  "</Car>" & _
  "<Car>" & _
    "<Make>Honda</Make>" & _
    "<Model>Accord</Model>" & _
    "<Color>Red</Color>" & _
    "<Year>2010</Year>" & _
  "</Car>" & _
"</Cars>"

        Dim document As XDocument = XDocument.Parse(xml)

        Dim query = From car In document.Descendants("Car") _
                    Select New With _
                    { _
                        .Make = car.Element("Make").Value, _
                        .Model = car.Element("Model").Value, _
                        .Color = car.Element("Color").Value, _
                        .Year = Int32.Parse(car.Element("Year").Value) _
                    }

        For Each car In query
            Console.WriteLine("{0}{4}{1}{4}{2}{4}{3}", car.Make, car.Model, car.Color, car.Year, vbTab)
        Next

        Console.Read()

    End Sub

End Module

Just to say a big thanks as this set me on the right path here's my solution

'Load my xml file
        Dim xDoc As XDocument = XDocument.Load("C:\temp\Test2\Application.xml")
'Declare my namespace 
        Dim ns1 As XNamespace = XNamespace.Get("http://www.MyWebsite/OneAppCommon-2006")

'use linq to retrieve results, note the ns1 + "FileAttachment"
        Dim Attachments = From attachment In xDoc.Descendants(ns1 + "FileAttachment") _
                    Select New With { _
                   .FileName = attachment.Element(ns1 + "FileName").Value, _
                   .Size = attachment.Element(ns1 + "Size").Value, _
                   .MimeType = attachment.Element(ns1 + "MimeType").Value, _
                   .PaperSize = attachment.Descendants(ns1 + "StandardSize").Value, _
                   .HasBeenPrinted = attachment.Descendants(ns1 + "HasBeenPrinted").Value, _
                    .Reference = attachment.Element(ns1 + "Reference").Value}
'Bind to datagridview
        Me.dgvAttachments.DataSource = Attachments.ToList()
        Me.dgvAttachments.Columns(0).Width = 210
        Me.dgvAttachments.Columns(5).Width = 230
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.