naresh gautam
naresh gautam

Reputation: 15

C#: how to get xml value in textbox?

I've got an XML file

<current>
<city>
<country>JAPAN</country>
</city>
<temperature value="307.07" min="307.07" max="307.07" unit="kelvin"/>
</current>

I only want temperature value in Textbox,

private void button1_Click(object sender, EventArgs e)
 {
            string url = string.Format("http://xxx/xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(url);
            textbox1.text = ????
}

Upvotes: 1

Views: 881

Answers (1)

jdweng
jdweng

Reputation: 34429

Using xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);

            XDocument doc = XDocument.Parse(xml);

            decimal temperature = (decimal)doc.Descendants("temperature").First().Attribute("value");
        }
    }
}

Upvotes: 2

Related Questions