populating object collection property with Linq
Posted
by Benjamin Ortuzar
on Stack Overflow
See other posts from Stack Overflow
or by Benjamin Ortuzar
Published on 2010-03-16T11:13:41Z
Indexed on
2010/03/16
11:16 UTC
Read the original article
Hit count: 555
I have an XML structure that has many doc
nodes, and each node may have zero or more extract paragraphs (paras
).
<doc>
<docitem>3</docitem>
<docid>129826</docid>
<doctitle>sample title</doctitle>
<docdatetime>2009-07-03T16:59:00</docdatetime>
<collectdatetime>2009-07-03T16:59:23</collectdatetime>
<summary>
<summarytext>sample summary</summarytext>
</summary>
<paras>
<paraitemcount>2</paraitemcount>
<para>
<paraitem>1</paraitem>
<paratext>sample text 1</paratext>
</para>
<para>
<paraitem>2</paraitem>
<paratext>sample text 2</paratext>
</para>
</paras>
</doc>
<doc>
...
</doc>
I also has some Linq code to populate some Document objects:
List<Document> documentsList = (from doc in xmlDocument.Descendants("doc")
select new Document
{
DocId = doc.Element("docid").Value,
DocTitle = doc.Element("doctitle").Value,
DocDateTime = DateTime.Parse(doc.Element("docdate").Value),
DocSummary = doc.Element("summary").Value,
DocParas = "" ///missing code to populate List<string>
}
).ToList<Document>();
Is it possible add all the paras nodes into the Document.DocParas List<string>
using Linq and Xpath, or should I do this task in a different way?
Note: I'm using .NET C# 3.5
© Stack Overflow or respective owner