简体   繁体   中英

Loading XML node inside ListBox and PivotControl (c# & Windows Phone 7)

I have the following XML

<?xml version="1.0" encoding="utf-8" ?>
    <root>
    <Sxoles>
        <sxoli>
            <onoma>name1</onoma>
            <sch>school1</sch>
            <sxoliId>100</sxoliId>
            <mathima>lesson1</mathima>
            <mathima>lesson2</mathima>


        </sxoli>
        <sxoli>
            <onoma>name2</onoma>
            <sch>school2</sch>
            <sxoliId>200</sxoliId>
            <mathima>lesson1</mathima>
            <mathima>lesson2</mathima>

        </sxoli>

    </Sxoles>
    </root>

I want to read mathima node and load inside a listbox (see bellow)

                           <Grid>
                                <ListBox x:Name="testList">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                            <TextBlock Grid.Column="1" 
                                            x:Name="mathima"
                                            Style='{StaticResource                PhoneTextSubtleStyle}'
                                             />
                                      </DataTemplate>
                                    </ListBox.ItemTemplate>
                            </ListBox>
                            </Grid>

The list box is contained in a pivotitem of a pivotcontrol

As result i'd like to see lesson1, lesson2 etc... in the list (only the mathima node).

I created a class

 public class Sxoli
    {
        ...
        [XmlElement("mathima")]
        public string mathima { get; set; }

    }

In back code (.xaml.cs file) i have

mathima.Text = sxoli.mathima;

but i get the following error

"the name mathima does not exist in the current context"

What can i do?

You can get mathima values using LINQ 2 XML

XElement doc=XElement.Load("yourXML.xml");//loads your xml
var mathimaList=doc.Descendants("Sxoles").Descendants("sxoli").Elements("mathima").Select(x=>x.Value);//gets mathima values

mathimaList now contains all the values of mathima ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM