2017-09-26 62 views
-2

我已經嘗試過很多次了,我不能。我不知道如何將它在C# 寫我如何可以通過這個值在C#的形式如何將xml傳遞給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

+2

你試過了什麼?你在哪裏失敗?有什麼錯誤?最重要的是,展示一些代碼。否則沒有人可以/會在這裏幫助你。 – Smartis

+0

我不知道如何編碼,請幫助我在任何一個 –

+1

看看這個[鏈接](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question /),其中* jon skeet *解釋如何改善您的問題,並在本網站上予以解答。 – LuckyLikey

回答

-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); 
      } 
     } 
+0

得到錯誤=的XDocument找不到命名空間如何添加命名空間 –

+0

VAR XMLTEXT =「康提中央斯里蘭卡 7.3 80.64亞洲/科倫坡 1506414825 2017-09-2614:03「; XDocument xDocument = XDocument.Parse(xmlText); string region = xDocument.Root.Element(「location」)。Element(「region」)。Value; –

+0

只是複製和過去 –