locked
problem with xml elements using linq RRS feed

  • Question

  • User932259438 posted

    hi,

    -<events>
    
    <name>FeralpiSalò - Alessandria</name>
    
    <homeName>FeralpiSalò</homeName>
    
    <awayName>Alessandria</awayName>
    
    <start>2016-01-15T19:00Z</start>
    
    <group>Lega Pro A</group>
    
    <type>ET_MATCH</type>
    
    <sport>FOOTBALL</sport>
    
    <state>FINISHED</state>
    
    <liveBetOffers>true</liveBetOffers>
    
    <openForLiveBetting>false</openForLiveBetting>
    
    <id>1002897846</id>
    
    <groupId>2000079607</groupId>
    
    <sportId>1000093190</sportId>
    
    
    -<path>
    
    <id>1000093190</id>
    
    <name>Football</name>
    
    <englishName>Football</englishName>
    
    </path>
    
    
    -<path>
    
    <id>1000461745</id>
    
    <name>Italy</name>
    
    <englishName>Italy</englishName>
    
    </path>
    
    
    -<path>

    How can I got <name>Italy</name> in gridview?

    Please help

    Friday, January 15, 2016 7:45 PM

Answers

  • User61956409 posted

    Hi progy85,

    You could try to add an attribute to element <name>.

    <path>
      <id>1000093190</id>
      <name>Football</name>
      <englishName>Football</englishName>
    </path>
    
    
    <path>
      <id>1000461745</id>
      <name type="country">Italy</name>
      <englishName>Italy</englishName>
    </path>
    

    Then you could get countries using this code.

    var items = from p in doc.Descendants("path")
                where p.Element("name").HasAttributes
                select new path
                {
                    id = p.Element("id").Value,
                    name = p.Element("name").Value,
                    englishName = p.Element("englishName").Value,
                };
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 26, 2016 5:04 AM

All replies

  • User2103319870 posted

    How can I got <name>Italy</name> in gridview?

    You can try with the below code

     //Load you xml here
                    XDocument doc = XDocument.Parse(@"<events>
                                                          <name>FeralpiSalò - Alessandria</name>
                                                          <awayName>Alessandria</awayName>
                                                          <start>2016-01-15T19:00Z</start>
                                                          <group>Lega Pro A</group>
                                                          <type>ET_MATCH</type>
                                                          <sport>FOOTBALL</sport>
                                                          <state>FINISHED</state>
                                                          <liveBetOffers>true</liveBetOffers>
                                                          <openForLiveBetting>false</openForLiveBetting>
                                                          <id>1002897846</id>
                                                          <groupId>2000079607</groupId>
                                                          <sportId>1000093190</sportId>
                                                          <path>
                                                            <id>1000093190</id>
                                                            <name>Football</name>
                                                            <englishName>Football</englishName>
                                                          </path>
                                                          <path>
                                                            <id>1000461745</id>
                                                            <name>Italy</name>
                                                            <englishName>Italy</englishName>
                                                          </path>
                                                        </events>");
                    //Read the italy node value
                    var query = from node in doc.Descendants("path")
                                where node.Element("name").Value == "Italy"
                                select new { Name = node.Element("name").Value} ;
    
                    GridView2.DataSource = query.ToList();
                    GridView2.DataBind();

    HTML

      <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" AllowSorting="True">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                </Columns>
            </asp:GridView>

    Friday, January 15, 2016 8:48 PM
  • User61956409 posted

    Hi progy85,

    Please refer to the following sample to populate GridView with the data from XML file.

    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="name" HeaderText="Name" />
            </Columns>
        </asp:GridView>
    </div>
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            XDocument doc = XDocument.Load(Server.MapPath("Data/Events.xml"));
            if (doc != null)
            {
                var items = from p in doc.Descendants("path")
                            select new path{ 
                                id = p.Element("id").Value,
                                name = p.Element("name").Value,
                                englishName = p.Element("englishName").Value,
                            };
    
    
                GridView1.DataSource = items.ToList();
                GridView1.DataBind();
            }
        }
    }
    
    public class path
    {
        public string id { get; set; }
        public string name { get; set; }
        public string englishName { get; set; }
    }
    

    Best Regards,

    Fei Han



    Monday, January 18, 2016 8:14 AM
  • User932259438 posted

    Hi,

    that problem. How can I got italy but not with where, because here I have also other countries?

    Thanks for help

    Sunday, January 24, 2016 8:24 PM
  • User61956409 posted

    Hi progy85,

    You could try to add an attribute to element <name>.

    <path>
      <id>1000093190</id>
      <name>Football</name>
      <englishName>Football</englishName>
    </path>
    
    
    <path>
      <id>1000461745</id>
      <name type="country">Italy</name>
      <englishName>Italy</englishName>
    </path>
    

    Then you could get countries using this code.

    var items = from p in doc.Descendants("path")
                where p.Element("name").HasAttributes
                select new path
                {
                    id = p.Element("id").Value,
                    name = p.Element("name").Value,
                    englishName = p.Element("englishName").Value,
                };
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 26, 2016 5:04 AM