我有这段代码:
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;
}
}
我想在XML文件中找到一个节点,其文本以&#34; {{&#34;并以&#34;}}&#34;结束并复制他的父亲。我可以找到那个节点,但我不能复制他的父亲。以下代码是我想要使用的XML:
<?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>
我尝试了一切但我无法在XML文件中添加新节点。我做错了什么?
答案 0 :(得分:1)
您忽略了ancestor_node.Clone();
的返回值。它返回XmlNode
。再次在文档中插入一个。