如何将Xml传递给c#表单

时间:2017-09-26 09:02:51

标签: c# xml

我已经多次尝试过我无法做到的事。我不知道怎么写c# 我怎样才能在c#表格中传递这个值

这是XML数据

<root>
<location>
<name>Kandy</name>
<region>Central</region>
<country>Sri Lanka</country>
<lat>7.3</lat>
<lon>80.64</lon>
<tz_id>Asia/Colombo</tz_id>
<localtime_epoch>1506414825</localtime_epoch>
<localtime>2017-09-26 14:03</localtime>
</location>
</root>

此数据如何传递到c#文本框

enter image description here

1 个答案:

答案 0 :(得分:-1)

var xmlText = "<root><location><name>Kandy</name><region>Central</region><country>Sri Lanka</country><lat>7.3</lat><lon>80.64</lon><tz_id>Asia/Colombo</tz_id><localtime_epoch>1506414825</localtime_epoch><localtime>2017-09-2614:03</localtime></location></root>";
        XDocument xDocument = XDocument.Parse(xmlText);
        string region = xDocument.Root.Element("location").Element("region").Value;
regionTextBox.Text=region;



 but it will be nice  solution to create model then you can use this snippet

        public T Deserialize<T>(string input) where T : class
        {
            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));

            using (StringReader sr = new StringReader(input))
            {
                return (T)ser.Deserialize(sr);
            }
        }