我正在开发一个应用程序,我在其中从Microsoft Dynamics CRM访问word文档xml,并希望替换通过XML Mapping窗格添加的字段。
下面是通过OpenXML检索的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:sdt><w:sdtPr><w:id w:val="1519887787"/><w:placeholder><w:docPart w:val="DefaultPlaceholder_1081868574"/></w:placeholder><w:dataBinding w:prefixMappings="xmlns:ns0='urn:microsoft-crm/document-template/new_candidate/10006/' " w:xpath="/ns0:DocumentTemplate[1]/new_candidate[1]/new_candidatename[1]" w:storeItemID="{2D909ED6-F428-47C5-93D4-89B5709D4B5C}"/><w:text/></w:sdtPr><w:sdtContent><w:p w:rsidR="0055490F" w:rsidRDefault="00F5486B"><w:r><w:t>new_candidatename</w:t></w:r></w:p></w:sdtContent></w:sdt><w:bookmarkStart w:id="0" w:name="_GoBack" w:displacedByCustomXml="prev"/><w:bookmarkEnd w:id="0" w:displacedByCustomXml="prev"/><w:sectPr w:rsidR="0055490F"><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1701" w:right="1134" w:bottom="1701" w:left="1134" 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中,有一个名为new_candidatename的字段,我想用自定义值替换它。
任何人都可以建议我如何使用OpenXML替换值?
答案 0 :(得分:0)
如果您使用的是c#,则没有理由使用openxml。您可以使用xml linq库。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement t = doc.Descendants().Where(x => x.Name.LocalName == "t").FirstOrDefault();
t.Value = "New Value";
doc.Save(FILENAME);
}
}
}