2012-02-29 22 views
1

通過代碼將節點插入現有的XDocument。通過代碼將節點插入現有的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

我可以恭敬地提出,如果你打算改變OOXML文檔的內部結構,那你看看專門爲此設計的SDK:http://msdn.microsoft.com/en-us/library/bb448854.aspx? 我個人不會覺得很舒服,對這個非常複雜的XMl文檔進行如此低級別的操縱,而沒有一定的限制和業務規則。 OpenXML SDK就是這樣做的。 請參閱http://msdn.microsoft.com/en-us/library/bb608618.aspx瞭解如何操作Word文檔中的自定義XML部件

相關問題