首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过Foreach绑定XAttribute值

如何通过Foreach绑定XAttribute值
EN

Stack Overflow用户
提问于 2012-07-25 23:27:28
回答 1查看 200关注 0票数 1
代码语言:javascript
复制
var xEle = new XElement("ContentDetails",
            from emp in _lstContents
            select new XElement("Contents",
                        new XAttribute("key", emp.Key),
                        new XAttribute("PublishedDate", emp.PublishedDate),
                        new XAttribute("FilePathURL", emp.FilePathURL),
                        new XAttribute("ID", emp.TitleID),
                        new XAttribute("ContentName", emp.Name)
                        ));

包含完整记录的_lstContents。我需要通过LinQ操作来构建XmlDocument,我知道这是可以实现的,我做到了。这是我所做的示例XML:

代码语言:javascript
复制
<ContentDetails>
  <Contents ContentName="Sample Project Plan SOW" ID="3"
        FilePathURL="http://192.168.30.59/contentraven/Uploads/Custom_View_LLC/EncryptedFile/zsg34g45tfblrkvzjh0cdlvs_17_7_2012_19_24_3.doc"
        PublishedDate="2012-07-10T14:37:02.073" key="310-072012-A5CDE"/>
</ContentDetails>

但现在我需要的是

代码语言:javascript
复制
<ContentDetails>
  <Contents ContentName="Sample Project Plan SOW" ID="3"
        FilePathURL="http://192.168.30.59/contentraven/Uploads/Custom_View_LLC/EncryptedFile/zsg34g45tfblrkvzjh0cdlvs_17_7_2012_19_24_3.doc"
        PublishedDate="2012-07-10T14:37:02.073" key="310-072012-A5CDE"/>
   <categories>
      <category id="1" categoryname="Category-1" contentid="3"/>
      <category id="2" categoryname="Category-2" contentid="3"/>
      <category id="3" categoryname="Category-3" contentid="3"/>
  </categories>
</ContentDetails>

我正在尝试这样的东西

代码语言:javascript
复制
var xEle = new XElement("ContentDetails",
            from emp in _lstContents
            select new XElement("Contents",
                        new XAttribute("key", emp.Key),
                        new XAttribute("PublishedDate", emp.PublishedDate),
                        new XAttribute("FilePathURL", emp.FilePathURL),
                        new XAttribute("ID", emp.TitleID),
                        new XAttribute("ContentName", emp.Name),
                            new XElement("Categories",
                                new XElement("Category",
                                    new XAttribute("ID", emp.Category.ForEach(_P => _P.CategoryID ),
                                    new XAttribute("CategoryName", emp.Category.ForEach(_P => _P.CategoryName))
                                )

                        ));

我怎样才能做到这一点呢?

emp.Category是_lstContents列表中的属性列表;

我需要创建许多在emp.Category中找到的CategoryName属性。

请参考附件中的截图。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-25 23:41:49

现在就差不多了,您只需要将类别集合中的项投影到category元素。这与将_lstContents中的项映射到Contents元素的方式没有太大不同。

代码语言:javascript
复制
var contentDetails =
    new XElement("ContentDetails",
        from contents in _lstContents
        select new XElement("Contents",
            new XAttribute("ContentName", contents.Name),
            new XAttribute("ID", contents.TitleID),
            new XAttribute("FilePathURL", contents.FilePathURL),
            new XAttribute("PublishedDate", contents.PublishedDate),
            new XAttribute("key", contents.Key),
            new XElement("categories",
                from category in contents.Category
                select new XElement("category",
                    new XAttribute("id", category.CategoryID),
                    new XAttribute("categoryname", category.CategoryName),
                    new XAttribute("contentid", category.ContentID)
                )
            )
        )
    );
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11653227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档