locked
Get data from XML file. RRS feed

  • Question

  •     public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                WebClient adrs = new WebClient();
                adrs.DownloadStringCompleted += new DownloadStringCompletedEventHandler(adrs_DownloadStringCompleted);
                adrs.DownloadStringAsync(new Uri("http://www.adrs.se/uninfo.xml?Data=" + username.Text));
            }
            public class UNitem 
            {
                public string UNnumber { get; set; }
                public string Substance { get; set; }
                public string Description { get; set; }
                public string UNClass { get; set; }
                public string UNClassCode { get; set; }
            }
            void adrs_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                if (e.Error != null)
                    return;
                XElement xmlUNsearch = XElement.Parse(e.Result);
                listBox1.ItemsSource = from Data in xmlUNsearch.Descendants("status")
                                       select new UNitem
                                       {
                                           UNnumber = Data.Element("UNnumber").Value,
                                           Substance = Data.Element("Substance").Value,
                                           Description = Data.Element("Description").Value,
                                           UNClass = Data.Element("UNClass").Value,
                                           UNClassCode = Data.Element("UNClassCode").Value
                                       };
            }
        }
    }

    Hi i have a problem or maybe i dont see what i have done wrong.

    I have a button, textbox and a listbox, i want to display data from the xml depending on what i write in the textbox.

    My code looks like this.

        public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                WebClient adrs = new WebClient();
                adrs.DownloadStringCompleted += new DownloadStringCompletedEventHandler(adrs_DownloadStringCompleted);
                adrs.DownloadStringAsync(new Uri("http://www.mysite.se/uninfo.xml?Data=" + username.Text));
            }
    
            public class UNitem 
            {
                public string UNnumber { get; set; }
                public string Substance { get; set; }
                public string Description { get; set; }
                public string UNClass { get; set; }
                public string UNClassCode { get; set; }
            }
    
            void adrs_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                if (e.Error != null)
                    return;
    
                XElement xmlUNsearch = XElement.Parse(e.Result);
                listBox1.ItemsSource = from Data in xmlUNsearch.Descendants("status")
                                       select new UNitem
                                       {
                                           UNnumber = Data.Element("UNnumber").Value,
                                           Substance = Data.Element("Substance").Value,
                                           Description = Data.Element("Description").Value,
                                           UNClass = Data.Element("UNClass").Value,
                                           UNClassCode = Data.Element("UNClassCode").Value
                                       };
            }
        }
    }


    And my XML file looks like this:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <XML> 
     
    <Data> 
        <UNnumber>0004</UNnumber> 
        <Substance>AMMONIUMPIKRAT</Substance> 
        <Description>torrt eller fuktat med mindre än 10 vikt-% vatten</Description> 
        <UNClass>1</UNClass> 
        <UNClassCode>1.1D</UNClassCode> 
    </Data> 
     
    <Data> 
        <UNnumber>0005</UNnumber> 
        <Substance>PATRONER FÖR VAPEN</Substance> 
        <Description>med sprängladdning</Description> 
        <UNClass>1</UNClass> 
        <UNClassCode>1.1F</UNClassCode> 
    </Data> 
     
    <Data> 
        <UNnumber>0006</UNnumber> 
        <Substance>PATRONER FÖR VAPEN</Substance> 
        <Description>med sprängladdning</Description> 
        <UNClass>1</UNClass> 
        <UNClassCode>1.1E</UNClassCode> 
    </Data> 
    
    </XML>


    And the listbox looks like this:

                <ListBox Height="476" Margin="0,131,0,0" Name="listBox1" VerticalAlignment="Top">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Height="132">
                                <StackPanel Width="370">
                                    <TextBlock Text="{Binding Path=UNnumber}" Foreground="#FFC8AB14" FontSize="28" />
                                    <TextBlock Text="{Binding Path=Substance}" Foreground="#FFC8AB14" FontSize="28" />
                                    <TextBlock Text="{Binding Path=Description}" Foreground="#FFC8AB14" FontSize="28" />
                                    <TextBlock Text="{Binding Path=UNClass}" Foreground="#FFC8AB14" FontSize="28" />
                                    <TextBlock Text="{Binding Path=UNClassCode}" Foreground="#FFC8AB14" FontSize="28" />
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>


    Dont get anything, what have i missed?

    Monday, September 20, 2010 10:06 AM

Answers

  • Hi.

    Erm... your Xml nodes are named "Data":

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <XML>  
    <Data> 
        ...
    </Data> 


    But you're looking for elements named "status" in code:

    from Data in xmlUNsearch.Descendants("status")
    


    Monday, September 20, 2010 10:19 AM

All replies

  • Hi.

    Erm... your Xml nodes are named "Data":

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <XML>  
    <Data> 
        ...
    </Data> 


    But you're looking for elements named "status" in code:

    from Data in xmlUNsearch.Descendants("status")
    


    Monday, September 20, 2010 10:19 AM
  • Thanks for that.

    Is it possible to make the data you get when you search to be clickable so you can get more information

    about each post.

    Monday, September 20, 2010 10:34 AM
  • I'm not sure if I understand that. Do you mean here in the forum? The search links should be clickable right away?

    Monday, September 20, 2010 11:13 AM
  • If I understood right, you need a hyperlink when you click one of the items on the list, you can do that registering the event on XAML markup on the _MouseLeftButtonDown event and then on code you can do this:

    System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("http://myapplication/mydocs/theinfo",UriKind.Absolute)); 

    Hope it helps!!

    Monday, September 20, 2010 11:37 AM
  • oki. MY XML file is generated from my MySQL database and i have som more tables that not are XML and the question is if i click on the post in my app i want to get the information from the table in the mysql dont need to be direct from the mysql. how do i get this =).

     

    Tuesday, September 21, 2010 6:54 AM
  • I have a little problem. When i write in what i want to search in my textbox then i dont really get the post i want just. If i leave it empty i get loads of posts.

    But if i search an exact number like 0073 i dont get that i just all diffrent post.

    Friday, September 24, 2010 6:44 AM
  • Hi pginvest,

    Maybe  I could help, but I definitely need more info about your question, do you mean you dont get data in the viewstate?, do you want to pass parameters to the xap file from aspx? how does this relates to the XML files you were asking for the first time?

    Friday, September 24, 2010 9:25 AM
  • Oki, if we skip the whole MySQL thing and just focus on the xml.

    I have the XML file as it looks in the first post.

    Now each post in there have more information than i have set inside the <data></data> field for all items.

    My question is now, how do i get the specific information for each post when i press one of the items in the view.

    If i for example search for item 0073 and then i get the additional information, and then when i press on the item in the viewstate i get some more information. How do i do that, can i do it all in the xml ?

    Sunday, September 26, 2010 9:55 PM