【问题标题】:C#: how to get xml value in textbox?C#:如何在文本框中获取 xml 值?
【发布时间】:2020-04-04 07:40:37
【问题描述】:

我有一个 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 = ????
}

【问题讨论】:

    标签: c# c#-3.0 c#-2.0


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 2014-04-25
      • 2016-11-20
      • 2017-05-08
      相关资源
      最近更新 更多