C# 如何在jlinq中从xml节点获取值
我正在使用DocumentFormat.OpenXml.Wordprocessing在C#中合并文档 xml节点如下所示:C# 如何在jlinq中从xml节点获取值,c#,linq,visual-studio-2013,C#,Linq,Visual Studio 2013,我正在使用DocumentFormat.OpenXml.Wordprocessing在C#中合并文档 xml节点如下所示: <w:instrText xml:space="preserve"> MERGEFIELD ORDERType \* MERGEFORMAT </w:instrText> 有人能告诉我如何使用xml.linq获取“MERGEFIELD ORDERType*MERGEFORMAT”的值吗。提前感谢在您的示例中,XMLinstrText是一个元素,
<w:instrText xml:space="preserve"> MERGEFIELD ORDERType \* MERGEFORMAT </w:instrText>
有人能告诉我如何使用xml.linq获取“MERGEFIELD ORDERType*MERGEFORMAT”的值吗。提前感谢在您的示例中,XML
instrText
是一个元素,而不是一个属性。这可能就是它不起作用的原因。@MarcinJuraszek,谢谢。我可以使用以下代码获取值:IList mailMergeFields=(从newBody.substands(XMLNS.GetName(“instrText”))中的el选择el.ToList();
public XNamespace XMLNS { get { return XNamespace.Get(WORDMLNS); } }
public const string WORDMLNS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
// Open Template
function getDoc(String Template){
byte[] sourceBytes = File.ReadAllBytes(Template.FullName);
using (MemoryStream _workingMemoryStream = new MemoryStream())
{
// Load into memory
_workingMemoryStream.Write(sourceBytes, 0, sourceBytes.Length);
using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(_workingMemoryStream, true))
{
XElement newBody = XElement.Parse(wordDocument.MainDocumentPart.Document.Body.OuterXml);
// I didn't get any of the node
IList<XElement> mailMergeFields =
(from el in newBody.Descendants()
where el.Attribute(XMLNS + "instrText") != null
select el).ToList();
// Replace all merge fields with Data
foreach (XElement field in mailMergeFields)
{
//do something
}
}
}