|
-
December 13th, 2011, 02:22 PM
#1
What does this code do?
From here:
http://blogs.msdn.com/b/ericwhite/ar...word-2007.aspx
This is the code in question:
Code:
using (WordprocessingDocument doc = WordprocessingDocument.Open("c:\\Test.docx", false))
{
var build = doc.MainDocumentPart
.GetXDocument()
.Descendants(w + "sdt")
.Where(e => ((string)e.Elements(w + "sdtPr")
.Elements(w + "alias")
.Attributes(w + "val")
.FirstOrDefault()).ToLower() == "build")
.Select(b => GetTextFromContentControl(b));
foreach (var b in build)
textBox1.Text += b + "\n";
}
From the blog title, I believe this is the LINQ part. The .Select(b => GetTextFromContentControl(b)); is confusing to me because it passes b (an XMLElement) into GetTextFromContentControl, but b hasn't been declared at this point yet. And then the follow on foreach statement confuses me even more. I guess b was assigned a value in that "b =>" part??? And since there is a foreach, it lends me to believe that there could be more than one b in the document, but when I added more document controls to the test doc, it still only printed out one time.
More questions: what are the statements that begin with dots called? I see that the var build line doesn't end until the semi colon in the select line. I am used to functions that look like myFunc(param1, param2), but I don't get what these dot statements do. I'll be happy to look them up and read about them myself, but I don't know what they are called. Although if someone wants to give me a little insight here, I'll be even happier 
Ultimately, I would like this code to parse through all of the content controls in a document, reading the control name and assigning a value to them. I could also use a clue as to how to modify the code here so that it could handle multiple controls within a document. Again, I don't need the full answer, but just a little nudge in the right direction.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|