locked
get xml data from silverlight RRS feed

  • Question


  • <NewDataSet>
      <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table4" msdata:Locale="">
          <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="Table4">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="AdvRECID" msdata:DataType="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" minOccurs="0" />
                    <xs:element name="FileType" type="xs:string" minOccurs="0" />
                    <xs:element name="UploadedFile" type="xs:string" minOccurs="0" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:schema>
      <Table4>
        <AdvRECID>9a2ab55a-b5c2-40da-91e8-cdc0fd876985</AdvRECID>
        <FileType>V</FileType>
        <UploadedFile>Admin\uploads\Advertisement\9A2AB55A-B5C2-40DA-91E8-CDC0FD876985\TomAndJerryTales_HQ.wmv</UploadedFile>
      </Table4>
      <Table4>
        <AdvRECID>f35a8897-2eb9-458f-9f8f-cd3249974c40</AdvRECID>
        <FileType>I</FileType>
        <UploadedFile>Admin\uploads\Advertisement\F35A8897-2EB9-458F-9F8F-CD3249974C40\how-to-draw-an-anime-cartoon-puppy-step-5.jpg</UploadedFile>
      </Table4>
      <Table4>
        <AdvRECID>8c562639-7c20-4701-b894-509e2b0f4dd5</AdvRECID>
        <FileType>I</FileType>
        <UploadedFile>Admin\uploads\Advertisement\8C562639-7C20-4701-B894-509E2B0F4DD5\productD-1.jpg</UploadedFile>
      </Table4>
    </NewDataSet>


    Public Sub New()
            InitializeComponent()
            loadXML()
    End Sub
    
    Public Sub loadXML()
    
       Dim url As New Uri("advBranch.xml", UriKind.Relative)
       Dim xmlClient As New WebClient()
    
       AddHandler xmlClient.DownloadStringCompleted, AddressOf DownloadStringCompletedEventHandler
       xmlClient.DownloadStringAsync(url)
    
    End Sub
    
    Private Sub DownloadStringCompletedEventHandler(sender As Object, e As DownloadStringCompletedEventArgs)
    
       DataGrid1.ItemsSource = GetItemList(e.Result)
    
    End Sub
    
    Private Function GetItem(el As XElement) As AdvBrcgSetting
       Dim s As New AdvBrcgSetting
       s.AdvertisementRECID = el.Attribute("AdvRECID").Value
       s.flType = el.Attribute("FileType").Value
       s.ulFile = el.Attribute("UploadedFile").Value
       Return s
    End Function
    
    Public Function GetItemList(xmlcontent As String) As List(Of AdvBrcgSetting)
       Dim itemList As New List(Of AdvBrcgSetting)()
    
       Dim doc As XElement = XElement.Parse(xmlcontent)
    
       itemList = (From el In doc.Descendants("Table4")
       Select GetItem(el)).ToList()
    
       Return itemList
    End Function



    The error cause at the "GetItem" Function

    Sunday, May 15, 2011 9:53 PM

Answers

  • In the GetItem method, you're attempting to get AdvRECID, FileType, and UploadedFile as attributes when in fact they're elements. Here's a snippet that works.

    MainPage XAML:

    <UserControl x:Class="XMLData2.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <sdk:DataGrid x:Name="DataGrid1" />
        </Grid>
    </UserControl>
    

    MainPage code-behind:

    Imports System.Xml.Linq
    
    Partial Public Class MainPage
        Inherits UserControl
    
        Public Sub New()
            InitializeComponent()
            loadXML()
        End Sub
    
        Public Sub loadXML()
    
            Dim url As New Uri("advBranch.xml", UriKind.Relative)
            Dim xmlClient As New WebClient()
    
            AddHandler xmlClient.DownloadStringCompleted, AddressOf DownloadStringCompletedEventHandler
            xmlClient.DownloadStringAsync(url)
    
        End Sub
    
        Private Sub DownloadStringCompletedEventHandler(sender As Object, e As DownloadStringCompletedEventArgs)
    
            DataGrid1.ItemsSource = GetItemList(e.Result)
    
        End Sub
    
        Private Function GetItem(el As XElement) As AdvBrcgSetting
            Dim s As New AdvBrcgSetting
            s.AdvertisementRECID = el.Element("AdvRECID").Value
            s.flType = el.Element("FileType").Value
            s.ulFile = el.Element("UploadedFile").Value
            Return s
        End Function
    
        Public Function GetItemList(xmlcontent As String) As List(Of AdvBrcgSetting)
            Dim itemList As New List(Of AdvBrcgSetting)()
    
            Dim doc As XElement = XElement.Parse(xmlcontent)
    
            itemList = (From el In doc.Descendants("Table4")
            Select GetItem(el)).ToList()
    
            Return itemList
        End Function
    
    End Class
    
    Public Class AdvBrcgSetting
        Private advRECID As String
        Private UploadedFile As String
        Private fileType As String
    
        Public Property AdvertisementRECID() As String
            Get
                Return advRECID
            End Get
            Set(value As String)
                advRECID = value
            End Set
        End Property
    
        Public Property ulFile() As String
            Get
                Return UploadedFile
            End Get
            Set(value As String)
                UploadedFile = value
            End Set
        End Property
    
        Public Property flType() As String
            Get
                Return fileType
            End Get
            Set(value As String)
                fileType = value
            End Set
        End Property
    End Class

    This is a very similar question to the last two you asked recently here:

    http://forums.silverlight.net/forums/p/228851/556283.aspx#556283

    http://forums.silverlight.net/forums/p/228853/556266.aspx#556266

    Brice

    Sunday, May 15, 2011 10:36 PM

All replies

  • In the GetItem method, you're attempting to get AdvRECID, FileType, and UploadedFile as attributes when in fact they're elements. Here's a snippet that works.

    MainPage XAML:

    <UserControl x:Class="XMLData2.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <sdk:DataGrid x:Name="DataGrid1" />
        </Grid>
    </UserControl>
    

    MainPage code-behind:

    Imports System.Xml.Linq
    
    Partial Public Class MainPage
        Inherits UserControl
    
        Public Sub New()
            InitializeComponent()
            loadXML()
        End Sub
    
        Public Sub loadXML()
    
            Dim url As New Uri("advBranch.xml", UriKind.Relative)
            Dim xmlClient As New WebClient()
    
            AddHandler xmlClient.DownloadStringCompleted, AddressOf DownloadStringCompletedEventHandler
            xmlClient.DownloadStringAsync(url)
    
        End Sub
    
        Private Sub DownloadStringCompletedEventHandler(sender As Object, e As DownloadStringCompletedEventArgs)
    
            DataGrid1.ItemsSource = GetItemList(e.Result)
    
        End Sub
    
        Private Function GetItem(el As XElement) As AdvBrcgSetting
            Dim s As New AdvBrcgSetting
            s.AdvertisementRECID = el.Element("AdvRECID").Value
            s.flType = el.Element("FileType").Value
            s.ulFile = el.Element("UploadedFile").Value
            Return s
        End Function
    
        Public Function GetItemList(xmlcontent As String) As List(Of AdvBrcgSetting)
            Dim itemList As New List(Of AdvBrcgSetting)()
    
            Dim doc As XElement = XElement.Parse(xmlcontent)
    
            itemList = (From el In doc.Descendants("Table4")
            Select GetItem(el)).ToList()
    
            Return itemList
        End Function
    
    End Class
    
    Public Class AdvBrcgSetting
        Private advRECID As String
        Private UploadedFile As String
        Private fileType As String
    
        Public Property AdvertisementRECID() As String
            Get
                Return advRECID
            End Get
            Set(value As String)
                advRECID = value
            End Set
        End Property
    
        Public Property ulFile() As String
            Get
                Return UploadedFile
            End Get
            Set(value As String)
                UploadedFile = value
            End Set
        End Property
    
        Public Property flType() As String
            Get
                Return fileType
            End Get
            Set(value As String)
                fileType = value
            End Set
        End Property
    End Class

    This is a very similar question to the last two you asked recently here:

    http://forums.silverlight.net/forums/p/228851/556283.aspx#556283

    http://forums.silverlight.net/forums/p/228853/556266.aspx#556266

    Brice

    Sunday, May 15, 2011 10:36 PM
  • thanks for help...because recently i cannot get the proper answer from xml...so repeat and repeat again

    Sunday, May 15, 2011 10:45 PM
  • i want to ask if i want to check value

    exp: if 

    If (s.flType = el.Element("FileType").Value) = "Image" Then
    image++;
    end if


    something like that....
    but it just can check true or false, cannot have other value, if i want to check , how to write the code??????
    Sunday, May 15, 2011 11:32 PM
  • Where do you want to evaluate this statement? If it's inside the GetItem method, the check for the flType property in your model against what's in the XML source is not needed since you already assign it. It'd then be something like this:

    If (s.flType = "Image") Then
        ' do stuff
    End If


    Here are the docs for comparison operators in VB.NET:

    http://msdn.microsoft.com/en-us/library/cey92b0t.aspx

    Brice

    Monday, May 16, 2011 9:29 AM