locked
how to get matchClock data using linq in xml RRS feed

Answers

  • User36583972 posted

    Hi progy85,

    From your description, you want to get the information you want from the node in the XML. You should make sure your XML format is correct. I have made a sample on my side, I can read the node information (if the structure is complex, you can nested query). The following code in my project for your reference.

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <Employees>
      <Employee type="Permanent">
        <ID>A</ID>
        <FirstName>Satheesh</FirstName>
        <LastName>&lt;b&gt;Babu</LastName>
        <Dept>IT</Dept>
      </Employee>
      <Employee type="Temporary">
        <ID>B</ID>
        <FirstName>Peters</FirstName>
        <LastName>Laurence</LastName>
        <Dept>Development</Dept>
      </Employee>
      <Employee type="Permamnet">
        <ID>C</ID>
        <FirstName>Rick</FirstName>
        <LastName>Anderson</LastName>
        <Dept>Sales</Dept>
      </Employee>
      <Employee type="Temporary">
        <ID>D</ID>
        <FirstName>Ramesh</FirstName>
        <LastName>Kumar</LastName>
        <Dept>HR</Dept>
      </Employee>
    </Employees>

    aspx.cs:

    protected void Button2_Click(object sender, EventArgs e)
            {
                string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase+ "test.xml";
                XElement xe = XElement.Load(str);
                IEnumerable<XElement> elements = from ele in xe.Elements("Employee")
                                                 select ele;
                showInfoByElements(elements);
            }
    
    
            private void showInfoByElements(IEnumerable<XElement> elements)
            {
                List<Employee> modelList = new List<Employee>();
                foreach (var ele in elements)
                {
                    Employee model = new Employee();
                    model.type = ele.Attribute("type").Value;
                    model.ID = ele.Element("ID").Value;
                    model.FirstName = ele.Element("FirstName").Value;
                    model.LastName = ele.Element("LastName").Value;
                    model.Dept = ele.Element("Dept").Value;
                    modelList.Add(model);
                }
                GridView1.DataSource = modelList;
                GridView1.DataBind();
                Response.Write("Count Nodes:"+modelList.Count.ToString());
            }
    
    

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 9, 2016 8:07 AM