簡體   English   中英

C#:如何在文本框中獲取 xml 值?

[英]C#: how to get xml value in textbox?

我有一個 XML 文件

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

我只想要文本框中的溫度值,

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

使用 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");
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM