2011-06-10 47 views
-6

有人能解釋我如何將「System.Collections.Generic」轉換爲List<string>? 從List<Tag> lsTag = new List<Tag>();List<string> list = new List<string>();將對象列表轉換爲字符串

和標籤是一類。提前致謝。

我想的是:

.ToList<string>stringbuilder

我讀了.xml文件,我試圖從我List<Tag> lsTag = new List<Tag>();項目添加到一個Silverlight ListBox控件。但我看到的唯一結果是Clipboard.Tag(我班的名字)。希望現在更清楚..

更新

這是我的.xml文件類:

namespace Clipboard { 
public class Tag { 
    public string name { get; set; } 
    public List<CodeFragments> lsTags = new List<CodeFragments>(); 
} 

這是對其他類的.xml文件:

public class CodeFragments { 
    public string name { get; set; } 
    public string tagURL { get; set; } 
    public string titel { get; set; } 
    public string body { get; set; } 
} 

這是我的.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<CodeFragments> 
    <Tag name="codeFrag1"> 
<oFragments tagURL="fragm1-1" titel="signatuur1-1" body="public static void main(String args[])" /> 
<oFragments tagURL="fragm1-2" titel="signatuur1-2" body="public static void main(String args[])" /> 

<Tag name="codeFrag2"> 
<oFragments tagURL="fragm2-1" titel="signatuur2-1" body="public static void main(String args[])" /> 
<oFragments tagURL="fragm2-2" titel="signatuur2-2" body="public static void main(String args[])" /> 

</CodeFragments> 

那我的類讀取.xml文件:

public void LoadXMLFile() { 
     WebClient xmlClient = new WebClient(); 
     xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded); 
     xmlClient.DownloadStringAsync(new Uri("codeFragments.xml", UriKind.RelativeOrAbsolute)); 
    } 
    public void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e) { 
     if (e.Error == null) { 
      string xmlData = e.Result; 
      XDocument xDoc = XDocument.Parse(xmlData); 
      var tagsXml = from c in xDoc.Descendants("Tag") select c.Attribute("name"); 
      List<Tag> lsTags = new List<Tag>(); 
      List<string> list = new List<string>(); 
      foreach (string tagName in tagsXml) { 
       Tag oTag = new Tag(); 
       oTag.name = tagName; 
       var tags = from d in xDoc.Descendants("Tag") 
          where d.Attribute("name").Value == tagName 
          select d.Elements("oFragments"); 
       var tagXml = tags.ToArray()[0]; 

       foreach (var tag in tagXml) { 
        CodeFragments oFragments = new CodeFragments(); 
        oFragments.tagURL = tag.Attribute("tagURL").Value; 
        oFragments.body = tag.Attribute("body").Value; 
        oFragments.titel = tag.Attribute("titel").Value; 
        oTag.lsTags.Add(oFragments); 
       }      
       lsTags.Add(oTag); 
      } 
      //list = lsTags.Select(x => x.ToString()).ToList(); 
      lsBox.ItemsSource = lsTags;    
     } 
    }  

問題解決了!沒有這些答案......謝謝你的回覆!

回答

5

你的問題是質量非常低,但我猜你想要的是這樣的:

List<string> list = lsTag.Select(x => x.Name.ToString()).ToList(); 
1

我不是Silverlight的開發者,但我的猜測是,你需要在你的Tag類重寫ToString 。 Silverlight控件可能會在每個標籤項上調用ToString。默認情況下,ToString輸出大多數複雜類型的類的名稱。所以你只需要做這樣的事情:

public class Tag { 

    //just guessing on your implementation that you have 
    //a private variable that you want displayed in the list 
    private String _tagName; 

    //your implementation here 

    public override String ToString(){ 
    //what you want the Tag to display 
    return _tagName; 
    } 

    //more implementation 
} 

希望這會有所幫助。

+0

我也試過。我會給你更多的信息。等一下。 – user 2011-06-10 12:32:55