XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements
Go Back  Xtreme Visual Basic Talk > > > XmlDocument Parsing of Node Elements


Reply
 
Thread Tools Display Modes
  #1  
Old 07-09-2008, 10:31 PM
pb61hd pb61hd is offline
Newcomer
 
Join Date: Jul 2008
Posts: 1
Question XmlDocument Parsing of Node Elements


First off, thanks for taking the time to read this and hopefully if you're having the same issue someone can help us both.

I'm pretty new to XML and extracting elements from the the data so please bare with me.

My goal was to create a very simple application that will, upon form load, call a web service which will return an XML of calling plans and the details of each calling plan. I then wanted each calling plan that is found in the response to have it's <name> element added to a listbox. Now, the code below does this ... but I'm not sure if it's the most efficient way or even close.

Now, I would like to display all of the elements of each calling_plan node in some type of datagrid or table.

I'm getting very confused after I convert the byte array response to a string, then load the string into an XmlDocument ... It seems extremely hard to parse the data after this - and the approach that I have coded below doesn't seem "best practice" but it was the only way I could get it to work.

I have included both the VB code and the XmlDocument below. Will someone please let me know how I can extract the elements from the document for each node and display in some table format or even assign the values to controls if I want.

BTW .. This code is being used in a windows application for now, but I will eventually be moving to a web application... just FYI.
Thanks in Advance!!

Code:
*****VB Code*****

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim oWeb As New System.Net.WebClient()

        oWeb.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

        Dim bytArguments As Byte() = System.Text.Encoding.ASCII.GetBytes("api_agent=USER&api_agent_password=PASSWORD&api_command=COMMAND&product_id=8")
        Dim bytRetData As Byte() = oWeb.UploadData("https://WEBURL/agentapi.php", "POST", bytArguments)

        Dim xmlString As String = oWeb.Encoding.GetString(bytRetData)

        Dim xmlDoc As New Xml.XmlDocument

        xmlDoc.LoadXml(xmlString)

        Dim intNCount As Integer = xmlDoc.GetElementsByTagName("calling_plan_id").Count

        Dim nlstName As Xml.XmlNodeList = xmlDoc.GetElementsByTagName("name")
        
        Dim strName As String = ""

        Do While intNCount > 0
            strName = nlstName(intNCount - 1).InnerText
            ListBox1.Items.Add(strName)
            intNCount -= 1
        Loop
    End Sub

End Class
Code:
*****XML Response*****

<?xml version="1.0" encoding="utf-8"?>
<ebs_api>
  <response>
    <code>0</code>
    <message>Successfull</message>
  </response>
  <request>
    <transaction_id>EFA0EE06-1F756E0D-321903EA-9CF18390</transaction_id>
    <transaction_datetime>2008-07-09 04:34:24 PM</transaction_datetime>
    <remote_ip>192.168.1.1</remote_ip>
    <remote_host>
    </remote_host>
    <parameters>
      <api_agent>USER</api_agent>
      <api_command>103</api_command>
      <product_id>8</product_id>
    </parameters>
  </request>
  <output>
    <calling_plans>
      <node_0>
        <calling_plan_id>42</calling_plan_id>
        <display_Order>0</display_Order>
        <product_id>8</product_id>
        <name>Economy+Package</name>
        <cost>5.50</cost>
        <allow_out_plan_calls>1</allow_out_plan_calls>
        <description>
        </description>
        <free_did>0</free_did>
        <destination_groups>
          <group_1>
            <group_name>US+%26+Canada</group_name>
            <allowed_minutes>100</allowed_minutes>
            <group_credit>5.5</group_credit>
            <destination_rates>
              <node_0>
                <destination_prefix>1</destination_prefix>
                <destination_name>USA+%26+Canada</destination_name>
                <rate_per_minute>0.055</rate_per_minute>
                <buy_rate>0.0100</buy_rate>
              </node_0>
            </destination_rates>
          </group_1>
        </destination_groups>
        <fees>
        </fees>
        <enterpriseplan>
          <node_0>0</node_0>
        </enterpriseplan>
        <remainingminutes>
          <node_0>100</node_0>
        </remainingminutes>
        <plan_type>1</plan_type>
        <allow_cp_signup_charge>1</allow_cp_signup_charge>
      </node_0>
      <node_1>
        <calling_plan_id>41</calling_plan_id>
        <display_Order>0</display_Order>
        <product_id>8</product_id>
        <name>Value+Package</name>
        <cost>24.50</cost>
        <allow_out_plan_calls>1</allow_out_plan_calls>
        <description>
        </description>
        <free_did>0</free_did>
        <destination_groups>
          <group_1>
            <group_name>US+%26++Canada</group_name>
            <allowed_minutes>500</allowed_minutes>
            <group_credit>24.5</group_credit>
            <destination_rates>
              <node_0>
                <destination_prefix>1</destination_prefix>
                <destination_name>USA+%26+Canada</destination_name>
                <rate_per_minute>0.049</rate_per_minute>
                <buy_rate>0.0100</buy_rate>
              </node_0>
            </destination_rates>
          </group_1>
        </destination_groups>
        <fees>
        </fees>
        <enterpriseplan>
          <node_0>0</node_0>
          <node_1>0</node_1>
        </enterpriseplan>
        <remainingminutes>
          <node_0>100</node_0>
          <node_1>500</node_1>
        </remainingminutes>
        <plan_type>1</plan_type>
        <allow_cp_signup_charge>1</allow_cp_signup_charge>
      </node_1>
      <node_2>
        <calling_plan_id>40</calling_plan_id>
        <display_Order>0</display_Order>
        <product_id>8</product_id>
        <name>Value+Plus+Package</name>
        <cost>87.50</cost>
        <allow_out_plan_calls>1</allow_out_plan_calls>
        <description>
        </description>
        <free_did>0</free_did>
        <destination_groups>
          <group_1>
            <group_name>US+%26+Canada</group_name>
            <allowed_minutes>2500</allowed_minutes>
            <group_credit>87.5</group_credit>
            <destination_rates>
              <node_0>
                <destination_prefix>1</destination_prefix>
                <destination_name>USA+%26+Canada</destination_name>
                <rate_per_minute>0.035</rate_per_minute>
                <buy_rate>0.0100</buy_rate>
              </node_0>
            </destination_rates>
          </group_1>
        </destination_groups>
        <fees>
        </fees>
        <enterpriseplan>
          <node_0>0</node_0>
          <node_1>0</node_1>
          <node_2>0</node_2>
        </enterpriseplan>
        <remainingminutes>
          <node_0>100</node_0>
          <node_1>500</node_1>
          <node_2>2500</node_2>
        </remainingminutes>
        <plan_type>1</plan_type>
        <allow_cp_signup_charge>1</allow_cp_signup_charge>
      </node_2>
    </calling_plans>
  </output>
</ebs_api>

Last edited by pb61hd; 07-09-2008 at 10:46 PM. Reason: adding vB code
Reply With Quote
  #2  
Old 07-10-2008, 09:01 PM
Qua's Avatar
QuaXmlDocument Parsing of Node Elements Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

I'm not confident that the approach I use in the following code is seen upon as good code, but it does the trick, and it's very easy to get a overview of the code:

Code:
With XmlReader Do While .Read If .NodeType = Xml.XmlNodeType.Element Then '-- BASIC CHAR INFO If .LocalName = "something" Then If .MoveToAttribute("sdf") AndAlso .ReadContentAsString().ToLower = CharacterName.ToLower Then ReturnCharacter.Name = .GetAttribute("sdf") If .MoveToAttribute("sdf") Then ReturnCharacter.Realm = .ReadContentAsString If .MoveToAttribute("sfasf") Then ReturnCharacter.Class_ = CType([Enum].Parse(GetType(Character.enumClass), .ReadContentAsString), Character.enumClass) If .MoveToAttribute("asfd") Then ReturnCharacter.Gender = CType([Enum].Parse(GetType(Character.enumGender), .ReadContentAsString), Character.enumGender) If .MoveToAttribute("adf") Then ReturnCharacter.Guild = .ReadContentAsString If .MoveToAttribute("level") Then ReturnCharacter.Level = .ReadContentAsInt If .MoveToAttribute("race") Then ReturnCharacter.Race = CType([Enum].Parse(GetType(Character.enumRace), .ReadContentAsString.Replace(" ", "")), Character.enumRace) If .MoveToAttribute("lastModified") Then ReturnCharacter.LastModified = DateTime.Parse(.ReadContentAsString) If .MoveToAttribute("faction") Then ReturnCharacter.Faction = CType([Enum].Parse(GetType(Character.enumFaction), .ReadContentAsString), Character.enumFaction) End If End If
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements
 
XmlDocument Parsing of Node Elements
XmlDocument Parsing of Node Elements
 
-->