Xml 如何通过Foreach绑定XAttribute值
_包含完整记录的内容。我需要通过LinQ操作来构建XmlDocument 我知道这是可以实现的,我做到了。 这是我的示例XML,我已经完成了以下工作:Xml 如何通过Foreach绑定XAttribute值,xml,linq,c#-4.0,linq-to-xml,Xml,Linq,C# 4.0,Linq To Xml,_包含完整记录的内容。我需要通过LinQ操作来构建XmlDocument 我知道这是可以实现的,我做到了。 这是我的示例XML,我已经完成了以下工作: var xEle = new XElement("ContentDetails", from emp in _lstContents select new XElement("Contents", new XAttribute("key", emp.K
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)
));
我怎样才能做到这一点
emp.Category是目录列表中的属性列表
我需要创建CategoryName属性的数量,就像在emp.Category中一样
请参考随附的屏幕截图。
多谢各位
您就快到了,只需将类别集合中的项目投影到
类别
元素。它与您如何将\lstContents
中的项目投影到Contents
元素没有太大区别
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))
)
));
我得到了@contents.Category中的“Value不能为null”@contents!我怎么知道chk不是空的?
<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))
)
));
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)
)
)
)
);