XML : I can not add a node in a XML file

I have this code:

  XmlDocument doc = new XmlDocument();  doc.Load(file_data);  XmlNamespaceManager namespace_xml = new XmlNamespaceManager(doc.NameTable);    //adicionar todos os namespaces necessarios  foreach (KeyValuePair<string, string> namespace_elem in all_namespaces)  {      namespace_xml.AddNamespace(namespace_elem.Key, namespace_elem.Value);  }    XmlNodeList node_list = doc.DocumentElement.SelectNodes("//w:tr//w:tc//w:p", namespace_xml);  Regex rgx = new Regex(@"^\{\{[a-zA-Z0-9_]+\}\}$");    for (int i = 0; i < node_list.Count; i++)  {      XmlNode node_elem = node_list[i];      Match mtch = rgx.Match(node_elem.InnerText);        if (mtch.Success)      {          //caso seja uma linha que tenha de ser inserida          XmlNode ancestor_node = null;          XmlNode aux_node_elem = node_elem;            while(aux_node_elem.ParentNode != null)          {              if (aux_node_elem.ParentNode.Name.Equals("w:tr"))              {                  ancestor_node = aux_node_elem.ParentNode;                  break;              }                aux_node_elem = aux_node_elem.ParentNode;          }            ancestor_node.Clone();          doc.Save(file_data);          break;      }  }    

I want to find a node, in a XML file, whose text starts with "{{" and finishes with "}}" and duplicate his father. I can find that node but I can not duplicate his father. The following code is the XML that I want to use:

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  <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:w15="http://schemas.microsoft.com/office/word/2012/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 w15 wp14">    <w:body>      <w:tbl>        <w:tblPr>          <w:tblStyle w:val="GridTable4" />          <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="1698" />          <w:gridCol w:w="1699" />          <w:gridCol w:w="1699" />          <w:gridCol w:w="1699" />          <w:gridCol w:w="1699" />        </w:tblGrid>        <w:tr w:rsidR="0090425B" w:rsidTr="0090425B">          <w:trPr>            <w:cnfStyle w:val="100000000000" w:firstRow="1" w:lastRow="0" w:firstColumn="0" w:lastColumn="0" w:oddVBand="0" w:evenVBand="0" w:oddHBand="0" w:evenHBand="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" />          </w:trPr>          <w:tc>            <w:tcPr>              <w:cnfStyle w:val="001000000000" w:firstRow="0" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:oddVBand="0" w:evenVBand="0" w:oddHBand="0" w:evenHBand="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" />              <w:tcW w:w="1698" w:type="dxa" />            </w:tcPr>            <w:p w:rsidR="0090425B" w:rsidRDefault="0090425B">              <w:r>                <w:t>CabeƧalho 1</w:t>              </w:r>            </w:p>          </w:tc>          <w:tc>            <w:tcPr>              <w:tcW w:w="1699" w:type="dxa" />            </w:tcPr>            <w:p w:rsidR="0090425B" w:rsidRDefault="0090425B">              <w:pPr>                <w:cnfStyle w:val="100000000000" w:firstRow="1" w:lastRow="0" w:firstColumn="0" w:lastColumn="0" w:oddVBand="0" w:evenVBand="0" w:oddHBand="0" w:evenHBand="0" w:firstRowFirstColumn="0" w:firstRowLastColumn="0" w:lastRowFirstColumn="0" w:lastRowLastColumn="0" />              </w:pPr>              <w:r>                <w:t>CabeƧalho 2</w:t>              </w:r>            </w:p>          </w:tc>        </w:tr>      </w:tbl>      <w:p w:rsidR="00163F00" w:rsidRDefault="00163F00">        <w:bookmarkStart w:id="0" w:name="_GoBack" />        <w:bookmarkEnd w:id="0" />      </w:p>      <w:sectPr w:rsidR="00163F00">        <w:pgSz w:w="11906" w:h="16838" />        <w:pgMar w:top="1417" w:right="1701" w:bottom="1417" w:left="1701" w:header="708" w:footer="708" w:gutter="0" />        <w:cols w:space="708" />        <w:docGrid w:linePitch="360" />      </w:sectPr>    </w:body>  </w:document>    

I try everything but I can not add a new node in the XML file. What did I make wrong?

No comments:

Post a Comment