locked
How to manage two elements of xml file RRS feed

  • Question

  • I am trying to display some data for each item of the xml

    for example i need to display the father name for every name in my data base.How to connect the name and the corresponding father name

    <name>zack</name>

    <fathername><tony></fathername>

    <name><lily></name>

    <fathername><john></fathername>

    Sunday, July 28, 2013 6:57 AM

Answers

  • It would be easier to make the structure of your xml file reflect the relationship between elements.  I suggest something like the following:

    <?xml version="1.0" encoding="utf-8" ?>
    <People>
      <Person>
        <Name>John</Name>
        <FathersName>Alfred</FathersName>
      </Person>
      <Person>
        <Name>Paul</Name>
        <FathersName>Jim</FathersName>
      </Person>
      <Person>
        <Name>George</Name>
        <FathersName>Harold</FathersName>
      </Person>
      <Person>
        <Name>Ringo</Name>
        <FathersName>Richard</FathersName>
      </Person>
    </People>

    Then it is easy to extract the data preserving the relationships.

                var xdocument = XDocument.Load("People.xml");
                var people = from p in xdocument.Descendants("Person")
                             select new
                             {
                                 Name = p.Element("Name").Value,
                                 FathersName = p.Element("FathersName").Value
                             };

    Now the people variable is an enumerable with properties Name and FathersName so, for example,

    p[0].Name = "John" and p[0].FathersName = "Alfred" etc.

    You can make people an ItemsSource for a ListView or GridView for display.


    • Edited by jrboddie Sunday, July 28, 2013 11:57 AM grammar
    • Marked as answer by Sai Kiran Reddy Sunday, July 28, 2013 2:20 PM
    Sunday, July 28, 2013 11:56 AM

All replies

  • It would be easier to make the structure of your xml file reflect the relationship between elements.  I suggest something like the following:

    <?xml version="1.0" encoding="utf-8" ?>
    <People>
      <Person>
        <Name>John</Name>
        <FathersName>Alfred</FathersName>
      </Person>
      <Person>
        <Name>Paul</Name>
        <FathersName>Jim</FathersName>
      </Person>
      <Person>
        <Name>George</Name>
        <FathersName>Harold</FathersName>
      </Person>
      <Person>
        <Name>Ringo</Name>
        <FathersName>Richard</FathersName>
      </Person>
    </People>

    Then it is easy to extract the data preserving the relationships.

                var xdocument = XDocument.Load("People.xml");
                var people = from p in xdocument.Descendants("Person")
                             select new
                             {
                                 Name = p.Element("Name").Value,
                                 FathersName = p.Element("FathersName").Value
                             };

    Now the people variable is an enumerable with properties Name and FathersName so, for example,

    p[0].Name = "John" and p[0].FathersName = "Alfred" etc.

    You can make people an ItemsSource for a ListView or GridView for display.


    • Edited by jrboddie Sunday, July 28, 2013 11:57 AM grammar
    • Marked as answer by Sai Kiran Reddy Sunday, July 28, 2013 2:20 PM
    Sunday, July 28, 2013 11:56 AM
  • Thank you sir

    I was trying to use the father name in other member function but as people is of var data type i wasn't able to access it could u suggest an alternative...thank you

    Sunday, July 28, 2013 2:43 PM
  • Sorry, I don't understand the access problem  Can you give me an example of what you mean?
    Sunday, July 28, 2013 3:18 PM
  • <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <GridView SelectionChanged="gv1_selection_changed" Name="gv1" HorizontalAlignment="Left" Margin="66,51,0,0" VerticalAlignment="Top" Width="1241" Height="643" > <GridView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <Grid> <Rectangle Fill="Blue" Tapped="Rectangle_Tapped"></Rectangle> <TextBlock FontSize="25" Width="200" Height="200" Text="{Binding name}"></TextBlock> </Grid> </StackPanel> </DataTemplate> </GridView.ItemTemplate> </GridView>

    <PopUp Name="pop" isOpen="False">

    <TextBlock Name="tblock"/> </PopUp> </Grid>

    protected async override void OnNavigatedTo(NavigationEventArgs e) { Windows.Storage.StorageFolder storagefolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Common"); Windows.Storage.StorageFile storagefile = await storagefolder.GetFileAsync("XMLFile1.xml"); var stream = await storagefile.OpenAsync(Windows.Storage.FileAccessMode.Read); XmlDocument doc = await XmlDocument.LoadFromFileAsync(storagefile); XDocument xdoc = XDocument.Parse(doc.GetXml()); var item = from n in xdoc.Descendants("item") select new { namd= n.Element("name").Value, fathername=n.Element("fathername").Value }; gv1.ItemsSource = item; }

    gv1_selection_changed(Object Sender,TappedRoutedEventArgs e))

    {

    int i=gv1.SelectedIndex();

    pop.isOpen=true;

    tblock.text=n[i].fathername;

    }


    I want to display a pop up which displays the father name when we select a name. So tried the above code but its not working.So how can i do it?


    Sunday, July 28, 2013 6:33 PM
  • Your code has a few typos.

    Here is an example that works.

    First, we need to add a  new class that will define our data item.  As in your code, I am using the properties word and meaning.

        public class Person
        {
            public string word { get; set; }
            public string meaning { get; set; }
            public Person(string myName, string myFathersName)
            {
                word = myName;
                meaning = myFathersName;
            }
        }

    Now the code to extract the data from the xml file is a little different so we can get a list of Person objects instead of the anonymous type as before.  Also the file access can be simplified from your code.

                var xdocument = XDocument.Load("Common/People.xml");
                var people = from p in xdocument.Descendants("Person")
                             select new Person
                             (
                                 p.Element("Name").Value,
                                 p.Element("FathersName").Value
                             );
                gv1.ItemsSource = people;

    There were a couple of typos in your xaml so here is something that works.

        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <GridView SelectionChanged="gv1_selection_changed" Name="gv1" HorizontalAlignment="Left" Margin="66,51,0,0" VerticalAlignment="Top" Width="1241" Height="643" >
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical">
                            <Grid>
                                <Rectangle Fill="Blue" Tapped="Rectangle_Tapped"></Rectangle>
                                <TextBlock  FontSize="25" Width="200" Height="200" Text="{Binding word}"></TextBlock>
    
                            </Grid>
                        </StackPanel>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
            
            <Popup Name="pop" IsOpen="False">
                <TextBlock Name="tblock"/>
            </Popup>
        </Grid>

    Now for the selection changed method.

            private void gv1_selection_changed(object sender, SelectionChangedEventArgs e)
            {
                pop.IsOpen = true;
                var selected = (sender as GridView).SelectedItem;
                tblock.Text = (selected as Person).meaning;
            }
    The xml file is the same as my original sample.  You may have to modify it if you are going to use different element names (names vs Names and fathersname vs FathersName etc.)


    • Edited by jrboddie Sunday, July 28, 2013 8:28 PM typo
    Sunday, July 28, 2013 8:28 PM
  • Thank you :)
    Monday, July 29, 2013 3:22 AM