Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

Windows Phone and RSS Reader Feed

Windows Phone and RSS Reader Feed บทความการใช้ Windows Phone ทำการ Feed ข้อมูลที่อยู่ในรูปแบบของ XML ที่ถูกส่งมาจาก PHP กับ MySQL ในฝั่งของ Web Server โดยฝั่ง Web Server จะทำหน้าที่อ่านข้อมูลจาก MySQL Database ด้วย PHP และ Windows Phone ทำหน้าที่เป็น Client เพื่อ Request ข้อมูลและจะได้ XML กลับมา และนำ XML ที่ได้ทำการ Feed ข้อมูลผ่านการ Binding บน ListBox

Windows Phone and RSS Reader

Windows Phone and RSS Reader Feed


ในการ Feed ข้อมูลที่อยู่ในรูปแบบของ XML สามารถใช้ NameSpace ของ System.Xml.Linq อ่านและแปลงให้อยู่ในรูปแบบของ Object และนำไปใช้กับ Data Binding

Windows Phone and RSS Reader

เพิ่ม NameSpace ที่มีชื่อว่า System.Xml.Linq เข้ามาใน Proejct

Windows Phone and RSS Reader

Library ของ System.Xml.Linq ถูกเพิ่มเข้ามาใน Project

Web Server สร้างไฟล์ที่ Web Server เพื่อไว้ส่ง XML ให้กับ Client ที่เข้ามา Request โดยใช้ PHP กับ MySQL

customer
CREATE TABLE `customer` (
`CustomerID` varchar(4) NOT NULL,
`Name` varchar(50) NOT NULL,
`Email` varchar(50) NOT NULL,
`CountryCode` varchar(2) NOT NULL,
`Budget` double NOT NULL,
`Used` double NOT NULL,
PRIMARY KEY (`CustomerID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table `customer`
-- 

INSERT INTO `customer` VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);
INSERT INTO `customer` VALUES ('C002', 'John Smith', '[email protected]', 'EN', 2000000, 800000);
INSERT INTO `customer` VALUES ('C003', 'Jame Born', '[email protected]', 'US', 3000000, 600000);
INSERT INTO `customer` VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);


Windows Phone and RSS Reader

โครงสร้างของตาราง MySQL Database








getXML.php
<?php
header("Content-type:text/xml; charset=UTF-8");              
header("Cache-Control: no-store, no-cache, must-revalidate");             
header("Cache-Control: post-check=0, pre-check=0", false);   
echo '<?xml version="1.0" encoding="utf-8"?>';

$objConnect = mysql_connect("localhost","root","root");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer WHERE 1  ";
$objQuery = mysql_query($strSQL);
?>
<customers>
	<?
	while($obResult = mysql_fetch_array($objQuery))
	{
	?>
		<customer>
			<CustomerID><?php echo $obResult["CustomerID"];?></CustomerID>
			<Name><?php echo $obResult["Name"];?></Name>
			<Email><?php echo $obResult["Email"];?></Email>
			<CountryCode><?php echo $obResult["CountryCode"];?></CountryCode>
			<Budget><?php echo $obResult["Budget"];?></Budget>
			<Used><?php echo $obResult["Used"];?></Used>
		</customer>
	<?
	}
	?>
</customers>
<?
	mysql_close($objConnect);
?>

ไฟล์ php ที่ทำหน้าที่อ่านข้อมูลจาก MySQL และส่งค่ากลับไปในรุปแบบของ XML

xml Result
<?xml version="1.0" encoding="utf-8"?>
<customers>
		<customer>
			<CustomerID>C001</CustomerID>
			<Name>Win Weerachai</Name>
			<Email>[email protected]</Email>
			<CountryCode>TH</CountryCode>
			<Budget>1000000</Budget>
			<Used>600000</Used>
		</customer>
		<customer>
			<CustomerID>C002</CustomerID>
			<Name>John  Smith</Name>
			<Email>[email protected]</Email>
			<CountryCode>EN</CountryCode>
			<Budget>2000000</Budget>
			<Used>800000</Used>
		</customer>
		<customer>
			<CustomerID>C003</CustomerID>
			<Name>Jame Born</Name>
			<Email>[email protected]</Email>
			<CountryCode>US</CountryCode>
			<Budget>3000000</Budget>
			<Used>600000</Used>
		</customer>
		<customer>
			<CustomerID>C004</CustomerID>
			<Name>Chalee Angel</Name>
			<Email>[email protected]</Email>
			<CountryCode>US</CountryCode>
			<Budget>4000000</Budget>
			<Used>100000</Used>
		</customer>
	</customers>

ตัวอย่างไฟล์ XML ที่ได้

Windows Phone and RSS Reader

ไฟล์ XML ที่ผ่านการเรียกด้วย URL


Example ทดสอบการอ่าน XML Feed จาก URL ของเว็บไซต์แบบง่าย ๆ

MainPage.xaml

Windows Phone and XML

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox Margin="0,0,-12,0" x:Name="CustomerList">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding CustomerID}"  TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                                <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                <TextBlock Text="{Binding Email}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                <TextBlock Text="{Binding CountryCode}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                <TextBlock Text="{Binding Budget}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                <TextBlock Text="{Binding Used}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Grid>


MainPage.xaml.vb (VB.NET)
Imports System.Runtime.Serialization
Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Text
Imports System.Xml.Linq

Partial Public Class MainPage
    Inherits PhoneApplicationPage

    ' Constructor
    Public Sub New()
        InitializeComponent()

        AddHandler Loaded, AddressOf MainPage_Loaded

    End Sub

    Dim client As WebClient
    Dim prog As ProgressIndicator

    Private Sub MainPage_Loaded(sender As Object, e As System.Windows.RoutedEventArgs)

        Dim url As String = "http://localhost/myphp/getXML.php"
        Dim uri As New Uri(url)
        client = New WebClient()
        client.AllowReadStreamBuffering = True

        AddHandler client.DownloadStringCompleted, AddressOf client_DownloadStringCompleted
        AddHandler client.DownloadProgressChanged, AddressOf client_DownloadProgressChanged

        client.DownloadStringAsync(uri)

        '*** SystemTray ProgressBar ***'
		prog = New ProgressIndicator()
		prog.IsVisible = True
		prog.IsIndeterminate = True
		prog.Text = "Downloading...."
		SystemTray.SetProgressIndicator(Me, prog)

    End Sub

    Private Sub client_DownloadStringCompleted(sender As Object, e As DownloadStringCompletedEventArgs)

        If e.Cancelled = False AndAlso e.Error Is Nothing Then


            Dim myCustomer As New List(Of Customer)
            Dim loadData As XDocument = XDocument.Parse(e.Result)

            Dim data = From query In loadData.Descendants("customer")
                        Select New Customer With { _
                            .CustomerID = query.Element("CustomerID"), _
                            .Name = query.Element("Name"), _
                            .Email = query.Element("Email"), _
                            .CountryCode = query.Element("CountryCode"), _
                            .Budget = query.Element("Budget"), _
                            .Used = query.Element("Used")
                        }

            Me.CustomerList.ItemsSource = data

            prog.IsVisible = False
        End If

    End Sub

    Private Sub client_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)

    End Sub

    <DataContract()> _
    Public Class Customer

        Sub New()
            ' TODO: Complete member initialization 
        End Sub

        <DataMember()> _
        Public Property CustomerID() As String
            Get
                Return m_CustomerID
            End Get
            Set(value As String)
                m_CustomerID = value
            End Set
        End Property
        <DataMember()> _
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set(value As String)
                m_Name = value
            End Set
        End Property
        <DataMember()> _
        Public Property Email() As String
            Get
                Return m_Email
            End Get
            Set(value As String)
                m_Email = value
            End Set
        End Property
        <DataMember()> _
        Public Property CountryCode() As String
            Get
                Return m_CountryCode
            End Get
            Set(value As String)
                m_CountryCode = value
            End Set
        End Property
        <DataMember()> _
        Public Property Budget() As String
            Get
                Return m_Budget
            End Get
            Set(value As String)
                m_Budget = value
            End Set
        End Property
        <DataMember()> _
        Public Property Used() As String
            Get
                Return m_Used
            End Get
            Set(value As String)
                m_Used = value
            End Set
        End Property

        Private m_CustomerID As String
        Private m_Name As String
        Private m_Email As String
        Private m_CountryCode As String
        Private m_Budget As String
        Private m_Used As String

        Public Sub New(ByVal strCustomerID As String,
                       ByVal strName As String,
                       ByVal strEmail As String,
                       ByVal strCountryCode As String,
                       ByVal strBudget As String,
                       ByVal strUsed As String)
            Me.CustomerID = strCustomerID
            Me.Name = strName
            Me.Email = strEmail
            Me.CountryCode = strCountryCode
            Me.Budget = strBudget
            Me.Used = strUsed

        End Sub

    End Class


End Class









MainPage.xaml.cs (C#)
using System;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using Microsoft.Phone.Shell;

namespace PhoneApp
{
    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            Loaded += MainPage_Loaded;
        }

        WebClient client;
        ProgressIndicator prog;

        private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            string url = "http://localhost/myphp/getXML.php";
            Uri uri = new Uri(url);
            client = new WebClient();
            client.AllowReadStreamBuffering = true;

            client.DownloadStringCompleted += client_DownloadStringCompleted;
            client.DownloadProgressChanged += client_DownloadProgressChanged;

            client.DownloadStringAsync(uri);

            //*** SystemTray ProgressBar ***'
            prog = new ProgressIndicator();
            prog.IsVisible = true;
            prog.IsIndeterminate = true;
            prog.Text = "Downloading....";
            SystemTray.SetProgressIndicator(this, prog);

        }

        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {

	        if (e.Cancelled == false && e.Error == null) {


               List<Customer> myCustomer = new List<Customer>();
                    XDocument loadData = XDocument.Load("XML/myXML.xml");

                    var data = from query in loadData.Descendants("customer")
                               select new Customer ()
                               {
                                   CustomerID = (string)query.Element("CustomerID"),
                                   Name = (string)query.Element("Name"),
                                   Email = (string)query.Element("Email"),
                                   CountryCode = (string)query.Element("CountryCode"),
                                   Budget = (string)query.Element("Budget"),
                                   Used = (string)query.Element("Used")
                               };

                    this.CustomerList.ItemsSource = data;
                }

            prog.IsVisible = false;

        }

        private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {

        }
    }


    public class Customer
    {
        public string CustomerID { get; set; }

        public string Name { get; set; }

        public string Email { get; set; }

        public string CountryCode { get; set; }

        public string Budget { get; set; }

        public string Used { get; set; }

    }
}

ในตัวอย่างนี้มี Code ทั้งที่เป็น VB.NET และ C# และสามารถดาวน์โหลด All Code ทั้งหมดได้จากส่วนท้ายของบทความ (Login สมาชิกก่อน)

Screenshot

Windows Phone and RSS Reader

แสดง Feed ข้อมูลจาก XML บน ListBox ผ่านการ Binding

อ่านเพิ่มเติม
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh487167(v=vs.92)


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-09-15 12:44:50 / 2017-03-25 22:23:24
  Download : Download  Windows Phone and RSS Reader Feed
 Sponsored Links / Related

 
Windows Phone and JSON
Rating :

 
Windows Phone and XML Parser
Rating :

 
Windows Phone Get Retrieving Data from Web Server (PHP & MySQL and JSON)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่