Parse XML Response String And Get Usable Variables

Pjgoodi

Member
Joined
Jan 31, 2013
Messages
6
Programming Experience
Beginner
Hi,

I'm new to VB.net and totally new to XML. I am building a program that receives a response to a XML inquiry from hwresponse.GetResponseStream. So far I have been able to submit the request and receive a response. I now need take that response (in the form of a string) and convert it to a format which will allow me to make decisions based on it. Perhaps by creating dataset/datatable and passing to variables.
My rough idea would be to create a new row for each lineid corresponding to an articleid with dates etc then loop.

I've spent days Googling an can't find anything in VB.net that fits. I know this is down to my lack of knowledge and a tiny brain. I would be eternally grateful to anyone who could help.
The response is a reply to a stock enquiry:

<?xml version="1.0" encoding="UTF-8" ?>
<ew:quote_A2 xmlns:ew="www.reifen.net">
<DocumentID>A2</DocumentID>
<Variant>5</Variant>
<ErrorHead>
<ErrorCode>0</ErrorCode>
</ErrorHead>
<BuyerParty>
<PartyID>305026</PartyID>
<AgencyCode>91</AgencyCode>
</BuyerParty>
<OrderLine>
<LineID>10</LineID>
<OrderedArticle>
<ArticleIdentification>
<ManufacturersArticleID>520654</ManufacturersArticleID>
<EANUCCArticleID>5452000877154</EANUCCArticleID>
</ArticleIdentification>
<ArticleDescription>
<ArticleDescriptionText>205/55R16 91H OPTIGRIP FP</ArticleDescriptionText>
</ArticleDescription>
<Availability>3</Availability>
<RequestedQuantity>
<QuantityValue>200</QuantityValue>
</RequestedQuantity>
<Error>
<ErrorCode>0</ErrorCode>
</Error>
<ScheduleDetails>
<DeliveryDate>9999-12-31</DeliveryDate>
<AvailableQuantity>
<QuantityValue>200</QuantityValue>
</AvailableQuantity>
</ScheduleDetails>
</OrderedArticle>
</OrderLine>
<OrderLine>
<LineID>10</LineID>
<OrderedArticle>
<ArticleIdentification>
<ManufacturersArticleID>522794</ManufacturersArticleID>
<EANUCCArticleID>5452001082922</EANUCCArticleID>
</ArticleIdentification>
<ArticleDescription>
<ArticleDescriptionText>205/55R16 91H UG 8 MS FP</ArticleDescriptionText>
</ArticleDescription>
<Availability>2</Availability>
<RequestedQuantity>
<QuantityValue>200</QuantityValue>
</RequestedQuantity>
<Error>
<ErrorCode>0</ErrorCode>
</Error>
<ScheduleDetails>
<DeliveryDate>2013-02-04</DeliveryDate>
<AvailableQuantity>
<QuantityValue>5</QuantityValue>
</AvailableQuantity>
</ScheduleDetails>
<ScheduleDetails>
<DeliveryDate>9999-12-31</DeliveryDate>
<AvailableQuantity>
<QuantityValue>195</QuantityValue>
</AvailableQuantity>
</ScheduleDetails>
</OrderedArticle>
</OrderLine>
</ew:quote_A2>
 
Hi,

If you are new to VB and new to XML then I am afraid that, after a quick look at this, you have got your work cut out for you.

I quickly read this file into a DataSet which creates eleven tables based on the structure of the XML file. After linking these eleven tables to DataGridViews the next thing you realise is that these eleven tables should really be 3 tables, one table for a Header Record, one table for multiple Detail records and one table for multiple Delivery records related to each Detail record.

On the basis that there is currently NO information to relate the Delivery records to the Detail records then you are going to have to manually create a relationship between these two record types when importing the data into your project.

To demonstrate what you are dealing with add eleven DataGridViews to a new form and replace the code with the code below:-

VB.NET:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  Dim myDataSet As New DataSet
  Dim myXMLString As String
 
  'You can ignore this. I am just reading your XML data from a file into a string
  Using myStream As New StreamReader(Application.StartupPath & "\XMLFile1.xml")
    myXMLString = myStream.ReadToEnd
    myStream.Close()
  End Using
 
  'Now you need to read the string variable through a stream reader to be able to
  'add this XML data to the DataSet
  Using myXMLReader As New StringReader(myXMLString)
    myDataSet.ReadXml(myXMLReader)
    myXMLReader.Close()
  End Using
 
  DataGridView1.DataSource = myDataSet.Tables(0)
  DataGridView2.DataSource = myDataSet.Tables(1)
  DataGridView3.DataSource = myDataSet.Tables(2)
  DataGridView4.DataSource = myDataSet.Tables(3)
  DataGridView5.DataSource = myDataSet.Tables(4)
  DataGridView6.DataSource = myDataSet.Tables(5)
  DataGridView7.DataSource = myDataSet.Tables(6)
  DataGridView8.DataSource = myDataSet.Tables(7)
  DataGridView9.DataSource = myDataSet.Tables(8)
  DataGridView10.DataSource = myDataSet.Tables(9)
  DataGridView11.DataSource = myDataSet.Tables(10)
End Sub

The way to build the Three tables that you ultimately need you are going to have the read the XML string into an XMLDocument object and then iterate through the Nodes to build that tables correctly. Here is how you can read your string into an XMLDocument and then start to iterate that document to pull out the nodes you need:-

VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Dim myXMLString As String
  Dim myXMLDoc As New XmlDocument
 
  'You can ignore this. I am just reading your XML data from a file into a string
  Using myStream As New StreamReader(Application.StartupPath & "\XMLFile1.xml")
    myXMLString = myStream.ReadToEnd
    myStream.Close()
  End Using
 
  Using myXMLReader As New StringReader(myXMLString)
    myXMLDoc.Load(myXMLReader)
  End Using
 
  For Each RootNode As XmlNode In myXMLDoc.ChildNodes
    For Each SecondLevelNode As XmlNode In RootNode.ChildNodes
      'Create For Loops as you need to drill down to the data that you need
      MsgBox(SecondLevelNode.Name)
    Next
  Next
End Sub

To finish then, and depending what you want to end up with, you are going to need to learn how to use the XMLDocument object, DataSets and DataTables.

Hope that helps.

Cheers,

Ian
 
Back
Top