إنشاء قسم تكرار الجدول المعين لجزء Xml المخصص
يوضح هذا البرنامج التعليمي كيفية إنشاء جدول يحتوي على قسم متكرر معين لجزء Xml مخصص في مستند Word باستخدام Aspose.Words لـ .NET. يسمح لك قسم التكرار بإضافة صفوف ديناميكيًا استنادًا إلى بيانات XML المخزنة في جزء Xml المخصص.
المتطلبات الأساسية
لمتابعة هذا البرنامج التعليمي، يجب أن يكون لديك ما يلي:
- تم تثبيت Aspose.Words لمكتبة .NET.
- المعرفة الأساسية بـ C# ومعالجة الكلمات باستخدام مستندات Word.
الخطوة 1: إعداد دليل المستندات
ابدأ بإعداد المسار إلى دليل المستندات الخاص بك. يستبدل"YOUR DOCUMENT DIRECTORY"
بالمسار الفعلي إلى الدليل الذي تريد حفظ المستند فيه.
string dataDir = "YOUR DOCUMENT DIRECTORY";
الخطوة 2: إنشاء مستند و DocumentBuilder
إنشاء مثيل جديد لـDocument
فئة و أDocumentBuilder
لبناء محتوى الوثيقة.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
الخطوة 3: إضافة بيانات XML مخصصة إلى CustomXmlPart
إنشاءCustomXmlPart
وإضافة بيانات XML مخصصة إليها. في هذا المثال، قمنا بإنشاء سلسلة XML تمثل مجموعة من الكتب مع عناوينها ومؤلفيها.
CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books",
"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" +
"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" +
"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>");
الخطوة 4: إنشاء جدول وهيكل الجدول
ابدأ في إنشاء جدول باستخدامStartTable
طريقةDocumentBuilder
. إضافة خلايا الجدول والمحتوى باستخدامInsertCell
وWrite
طُرق.
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Title");
builder.InsertCell();
builder.Write("Author");
builder.EndRow();
builder.EndTable();
الخطوة 5: إنشاء قسم التكرار المعين لـ XML المخصص
إنشاءStructuredDocumentTag
معSdtType.RepeatingSection
لتمثيل القسم المتكرر. قم بتعيين تعيين XML للقسم المتكرر باستخدامSetMapping
طريقةXmlMapping
ملكية. في هذا المثال، نقوم بتعيين القسم المكرر إلى/books[1]/book
.
StructuredDocumentTag repeatingSectionSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row);
repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", "");
table.AppendChild(repeatingSectionSdt);
الخطوة 6: إنشاء عنصر القسم المكرر وإضافة خلايا
إنشاءStructuredDocumentTag
معSdtType.RepeatingSectionItem
لتمثيل عنصر القسم المتكرر. قم بإلحاقه كطفل إلى قسم التكرار.
StructuredDocumentTag repeatingSectionItemSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row);
repeatingSectionSdt.AppendChild(repeatingSectionItemSdt);
إنشاءRow
لتمثيل كل عنصر في القسم المكرر وإلحاقه بعنصر القسم المكرر.
Row row = new Row(doc);
repeatingSectionItemSdt.AppendChild(row);
الخطوة 7: إضافة عناصر التحكم في المحتوى داخل قسم التكرار
يخلقStructuredDocumentTag
الكائنات معSdtType.PlainText
لتمثيل عناصر التحكم في محتوى العنوان والمؤلف. قم بتعيين تعيين XML لكل عنصر تحكم محتوى باستخدامSetMapping
طريقةXmlMapping
ملكية. في هذا المثال، نقوم بتعيين عنصر تحكم العنوان إلى/books[1]/book[1]/title[1]
وسيطرة المؤلف على/books[1]/book[1]/author[1]
.
StructuredDocumentTag titleSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", "");
row.AppendChild(titleSdt);
StructuredDocumentTag authorSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", "");
row.AppendChild(authorSdt);
الخطوة 8: احفظ المستند
احفظ المستند المعدل في الدليل المحدد باستخدام الملفSave
طريقة. قم بتوفير اسم الملف المطلوب مع امتداد الملف المناسب. في هذا المثال، نقوم بحفظ المستند باسم “WorkingWithSdt.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx”.
doc.Save(dataDir + "WorkingWithSdt.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx");
مثال على التعليمات البرمجية المصدر لإنشاء قسم مكرر للجدول المعين لجزء Xml مخصص باستخدام Aspose.Words لـ .NET
// المسار إلى دليل المستندات الخاص بك
string dataDir = "YOUR DOCUMENT DIRECTORY";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books",
"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" +
"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" +
"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>");
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Title");
builder.InsertCell();
builder.Write("Author");
builder.EndRow();
builder.EndTable();
StructuredDocumentTag repeatingSectionSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row);
repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", "");
table.AppendChild(repeatingSectionSdt);
StructuredDocumentTag repeatingSectionItemSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row);
repeatingSectionSdt.AppendChild(repeatingSectionItemSdt);
Row row = new Row(doc);
repeatingSectionItemSdt.AppendChild(row);
StructuredDocumentTag titleSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", "");
row.AppendChild(titleSdt);
StructuredDocumentTag authorSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", "");
row.AppendChild(authorSdt);
doc.Save(dataDir + "WorkingWithSdt.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx");
هذا كل شيء! لقد نجحت في إنشاء جدول يحتوي على قسم متكرر تم تعيينه إلى CustomXmlPart في مستند Word الخاص بك باستخدام Aspose.Words for .NET.