2012-07-25 64 views
1
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我所做的这样:如何通过Foreach绑定XAttribute值

<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> 

,但现在我需要的是

<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> 

我试图像这样

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 List中的一个属性列表;

我需要在emp.Category中找到许多类别名称属性。

请参阅附录中的截图。 谢谢

enter image description here

回答

2

就快,你只需要你的项目类别集合中的项目category元素。这与您如何在_lstContentsContents元素中投射项目没有多大区别。

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) 
       ) 
      ) 
     ) 
    ); 
+0

我得到“Value can not be null”@ contents.Category对于某些内容!我如何chk不是null? – 2012-07-25 15:51:30

+0

谢谢!它工作正常...我需要避免null如上所述... – 2012-07-25 16:04:12