Sequence contains no Elements at System.Linq.Enumerable.Single[Tsource](IEnumerable'1 source)
using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PopulateContentsOpenXml { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string myfile = @"C:\Users\Emeka.Iwuagwu\Desktop\DTestDoc.docx"; string[] writeDocData = new string[] {FirstName.Text,FileNumber.Text,IDNumber.Text,LastName.Text }; WriteDataToContentControl(myfile, writeDocData); } private void WriteDataToContentControl(string filename,string[]data) { try { using (WordprocessingDocument doc = WordprocessingDocument.Open(filename, true)) { MainDocumentPart mainPart = doc.MainDocumentPart; foreach (string text in data) { SdtElement text_block = mainPart.Document.Body.Descendants<SdtElement>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == text).Single(); Text to = text_block.Descendants<Text>().Single(); to.Text = text; mainPart.Document.Save(); MessageBox.Show("Ok i am fine now!"); } } } catch (Exception ex) { MessageBox.Show("Error: " + ex.ToString()); } } } }
SdtElement text_block = mainPart.Document.Body.Descendants<SdtElement>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == text).Single();
Single()
SingleOrDefault()
Enumerable.Single
Enumerable.SingleOrDefault
null
SdtElement text_block = ...
Descendants
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)