将对象列表转换为字符串

本文关键字:字符串 转换 列表 对象 | 更新日期: 2023-09-27 17:58:47

有人能告诉我如何转换一个"系统集合。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控件中。但我看到的唯一结果是剪贴板。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;            
        }
    }       

问题解决了!这些都没有给出答案。。。无论如何,谢谢你的回复!

将对象列表转换为字符串

你的问题质量很低,但我想你想要的是:

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

我不是Silverlight开发人员,但我猜您需要在Tag类中重写ToString。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
}

希望这能有所帮助。