有人可以解释一下如何将“System.Collections.Generic”转换为List<string>
吗?
从List<Tag> lsTag = new List<Tag>();
到List<string> list = new List<string>();
Tag是一个班级。提前谢谢。
我尝试的是:
.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;
}
}
问题解决了!没有这些给出的答案......无论如何,谢谢你的回复!
答案 0 :(得分:5)
你的问题质量非常低,但我猜你想要的是这个:
List<string> list = lsTag.Select(x => x.Name.ToString()).ToList();
答案 1 :(得分:1)
我不是Silverlight开发人员,但我猜您需要覆盖ToString
课程中的Tag
。 Silverlight控件可能在每个Tag项上调用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
}
希望这有帮助。