c# - 打开 xml 2.5 查找具有特定字段代码的特定占位符。C#
问题描述
enter code here
我有个问题。我在 word 中创建了一个名为 OrderLines 的特定属性,它包含一行三个单元格。我想克隆该行,然后我想进入克隆并更改所有三个具有 Text 属性的表格单元格 FieldCode。在该属性中,我想将 n1 更改为 n2。但只有克隆线。我的代码在所有包含 n1 的行上都发生了变化。我究竟做错了什么?
Body bod = doc.MainDocumentPart.Document.Body;
Table tables = bod.Descendants<Table>().First();
TableRow row = tables.Descendants<TableRow>().Last();
TableCell rowCell = new TableCell();
IEnumerable<FieldCode> placeHolder = mainPart.Document.Body.Descendants<FieldCode>();
// Denna tar fram TableProperties med namnet OrdeLines.
IEnumerable<TableProperties> tableProperties = mainPart.Document.Body.Descendants<TableProperties>().Where(tp => tp.TableCaption != null);
foreach (TableProperties tProp in tableProperties)
{
if (tProp.TableCaption.Val.Equals("OrderLines")) // see comment, this is actually StringValue
{
// do something for table with myCaption
Table table = (Table)tProp.Parent;
//Klonar sista raden
var cloneRow = table.ChildElements[3].CloneNode(true);
row.InsertAfterSelf(cloneRow);
foreach (TableCell tableCell in cloneRow)
{
foreach (FieldCode code in placeHolder)
{
//var test = cloneRow.LastOrDefault().Where(x => x.InnerText == code.InnerText);
var testo = tableCell.Parent.Descendants<FieldCode>().LastOrDefault().Where(X => X.InnerText == code.InnerText);
if (testo != null)
{
code.Text = code.Text.Replace("n2_", "n1_");
}
}
}
}
}
1):DOCPROPERTY "MFiles_PGB5E4D5DC229C4027AB19B40D0203C01Cn1_PG3E2BB7EBC49E4C8C825CCAE0AEBA9A06" * MERGEFORMAT
2):
FieldCode fieldCode1 = new FieldCode(){ Space = SpaceProcessingModeValues.Preserve };
fieldCode1.Text = " DOCPROPERTY \"MFiles_PGB5E4D5DC229C4027AB19B40D0203C01Cn1_PG3E2BB7EBC49E4C8C825CCAE0AEBA9A06\" \\* MERGEFORMAT ";
解决方案
推荐阅读
- npm - NPM Parallelshell 不工作,显示错误
- c# - Unity PlayerMovement 延迟停止
- angular - 将角度从 8 升级到 9 后,错误抛出“无法将值“未定义”分配给模板变量“限制”
- http - Windows 10 IoT 核心版 - 未找到 netsh http
- javascript - 计算百分比和分割值
- ruby-on-rails - 如何将体面的代码分解为经典的 Rails 方式?
- nlp - 如何在 SpaCy 的 NER 中为新定义的标签添加解释/描述?
- ansible - 无法使用 ansible s3_sync 模块同步两个 S3 存储桶
- reactjs - 如何在reactjs中将值从输入框传递到reducer
- javascript - 通过同一个应用的 API 改变一个 React 应用的状态