【问题标题】:Load list with linq to xml使用 linq 加载列表到 xml
【发布时间】:2012-10-30 20:26:30
【问题描述】:

我正在尝试从 xml 加载列表。每个节点都有几个节点。我知道可以在如下图所示的 foreach 循环中执行此操作,但我想避免使用循环。

这是我不想要的,但没有循环:

我看到了这个例子,但它只适用于一个节点“id”。 How can I transform XML into a List<string> or String[]?

谢谢。

【问题讨论】:

    标签: c# xml linq linq-to-xml


    【解决方案1】:
    var contacts = from c in xdoc.Descendants("contact")
                   select new Contact()
                   {
                      GUID = (string)c.Element("Guid"),
                      Name = (string)c.Element("Name"),
                      Email = (string)c.Element("Email"),
                      PhoneNumber = (string)c.Element("PhoneNumber")
                   };
    

    其中xdocXDocument 类的实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 2012-10-02
      相关资源
      最近更新 更多