locked
read xml attribute in the asp.net c# RRS feed

  • Question

  • User-1904220520 posted

    <br>
    hi&lt;br&gt;<br>
    I want to read an xml file. And to all its attribute I need.&lt;<br> and Number time rage=36 to 39.
    I do not want Number 12.
    High Time tag is repeated 36 times<br>
    xml file <br>
    http://www.yr.no/place/Iran/East_Azarbaijan/Bost%C4%81n%C4%81b%C4%81d/forecast.xml
    Wednesday, September 28, 2016 3:55 PM

Answers

  • User-484054684 posted

    If it's dynamic and you want to execute some logic, store it in a variable and apply your logic.

    See the snippet below:

    	XElement root = XElement.Load("http://www.yr.no/place/Iran/East_Azarbaijan/Bost%C4%81n%C4%81b%C4%81d/forecast.xml");
    		
    // Use Descendants to get all time objects from root var times = root.Descendants("time"); // 37 objects will come here
    // Iterate over the time objects received foreach(var time in times) {
    // Process each time object here - You can read attributes / elements / descendants etc of time object var period = time.Attribute("period");

    // You can do additional logic with period here..
    // You can even get time's descendants or Elements
    var precipitationValue = time.Elements("precipitation").First().Attribute("value");

    // For testing purpose, writing it's Value - You can use the obj.Value syntax to access it's value Console.WriteLine(period.Value); Console.WriteLine(precipitationValue.Value); }

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 29, 2016 10:18 AM

All replies

  • User-484054684 posted

    You can do it with LINQ to easily read XML, nodes & attributes.

    An article as example: http://www.c-sharpcorner.com/article/reading-and-writing-xml-using-xlinq/ 

    Wednesday, September 28, 2016 4:05 PM
  • User-654786183 posted

    You can do something like this

    XElement root = XElement.Load("Path to uour XML File here");
    IEnumerable<XElement> time =
        from el in root.Elements("time")
        where (string)el.Attribute("period") == "2"
        select el;
    foreach (XElement t in time)
        //your logic here to iterate the time element

    Please refer the following link for more info

    https://msdn.microsoft.com/en-us/library/mt693115.aspx

    Wednesday, September 28, 2016 5:10 PM
  • User-1904220520 posted

    thanks but peroid is a dynamic value.
    Wednesday, September 28, 2016 8:55 PM
  • User36583972 posted

    Hi igifar,
     
    Community members already give you some solutions, you can try it on your side.

    thanks but peroid is a dynamic value.

    In order to be able to better help you, hope you could explain the meaning of the period is dynamic value.

    Best Regards,  

    Yohann Lu

    Thursday, September 29, 2016 9:29 AM
  • User-484054684 posted

    If it's dynamic and you want to execute some logic, store it in a variable and apply your logic.

    See the snippet below:

    	XElement root = XElement.Load("http://www.yr.no/place/Iran/East_Azarbaijan/Bost%C4%81n%C4%81b%C4%81d/forecast.xml");
    		
    // Use Descendants to get all time objects from root var times = root.Descendants("time"); // 37 objects will come here
    // Iterate over the time objects received foreach(var time in times) {
    // Process each time object here - You can read attributes / elements / descendants etc of time object var period = time.Attribute("period");

    // You can do additional logic with period here..
    // You can even get time's descendants or Elements
    var precipitationValue = time.Elements("precipitation").First().Attribute("value");

    // For testing purpose, writing it's Value - You can use the obj.Value syntax to access it's value Console.WriteLine(period.Value); Console.WriteLine(precipitationValue.Value); }

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 29, 2016 10:18 AM
  • User-1904220520 posted
    very very thanks.my friend
    Thursday, September 29, 2016 2:31 PM
  • User-1904220520 posted

    Why not accept this code in asp.net core?

    Friday, October 7, 2016 1:20 PM