通过代码将节点插入现有XDocument。
我使用WordprocessingDocument打开嵌入了xml架构的word文档,并通过XmlReader更改为XDocument
XDocument如下
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
<w:body>
<w:customXml w:uri="http://tempuri.org/CustomSchema.xsd" w:element="UserDetails">
<w:p w:rsidR="00FE0E5B" w:rsidRDefault="00FE0E5B" />
<w:tbl>
<w:tblPr>
<w:tblStyle w:val="TableGrid" />
<w:tblW w:w="0" w:type="auto" />
<w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1" />
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="4788" />
<w:gridCol w:w="4788" />
</w:tblGrid>
<w:tr w:rsidR="0099743C" w:rsidTr="005B2968">
<w:tc>
<w:tcPr>
<w:tcW w:w="4788" w:type="dxa" />
</w:tcPr>
<w:p w:rsidR="0099743C" w:rsidRDefault="0099743C" w:rsidP="005B2968">
<w:r>
<w:t>User Name</w:t>
</w:r>
</w:p>
</w:tc>
<w:customXml w:uri="http://tempuri.org/CustomSchema.xsd" w:element="UserName">
<w:tc>
<w:tcPr>
<w:tcW w:w="4788" w:type="dxa" />
</w:tcPr>
<w:p w:rsidR="0099743C" w:rsidRDefault="0099743C" w:rsidP="005B2968">
<w:r>
<w:t xml:space="preserve"> </w:t>
</w:r>
</w:p>
</w:tc>
</w:customXml>
</w:tr>
//////////new node to be appended here
</w:tbl>
<w:p w:rsidR="00FE0E5B" w:rsidRDefault="00FE0E5B" />
<w:p w:rsidR="003978D7" w:rsidRDefault="009A3CD3" />
</w:customXml>
<w:sectPr w:rsidR="003978D7">
<w:pgSz w:w="12240" w:h="15840" />
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0" />
<w:cols w:space="720" />
<w:docGrid w:linePitch="360" />
</w:sectPr>
</w:body>
</w:document>
我想将节点附加为
<w:tr w:rsidR="007C2DEB" w14:paraId="14C244F6" w14:textId="77777777" w:rsidTr="005B2968" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:tc>
<w:tcPr>
<w:tcW w:w="4788" w:type="dxa" />
</w:tcPr>
<w:p w14:paraId="08782D94" w14:textId="06E7BC60" w:rsidR="007C2DEB" w:rsidRDefault="007C2DEB" w:rsidP="005B2968">
<w:r>
<w:t xml:space="preserve">Password </w:t>
</w:r>
</w:p>
</w:tc>
<w:customXml w:uri="http://tempuri.org/CustomSchema.xsd" w:element="Password">
<w:tc>
<w:tcPr>
<w:tcW w:w="4788" w:type="dxa" />
</w:tcPr>
<w:p w14:paraId="05792D0F" w14:textId="2205EB25" w:rsidR="007C2DEB" w:rsidRDefault="007C2DEB" w:rsidP="005B2968">
<w:r>
<w:t xml:space="preserve"> </w:t>
</w:r>
</w:p>
</w:tc>
<w:bookmarkStart w:id="0" w:name="_GoBack" w:displacedByCustomXml="next" />
<w:bookmarkEnd w:id="0" w:displacedByCustomXml="next" />
</w:customXml>
</w:tr>
紧跟在“用户名”节点之后,然后保存合并的文档。
答案 0 :(得分:0)
我是否可以尊重地建议,如果您要更改OOXML文档的内部结构,请查看专门为此目的设计的SDK:http://msdn.microsoft.com/en-us/library/bb448854.aspx? 在没有一些限制和业务规则的情况下,我个人不愿意对非常复杂的XMl文档进行这种低级操作。 OpenXML SDK就是这样做的。 有关如何在Word文档中操作自定义XML部件的信息,请参阅http://msdn.microsoft.com/en-us/library/bb608618.aspx