有人能解釋我如何將「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;
}
}
問題解決了!沒有這些答案......謝謝你的回覆!
我也試過。我會給你更多的信息。等一下。 – user 2011-06-10 12:32:55