Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在jlinq中从xml节点获取值_C#_Linq_Visual Studio 2013 - Fatal编程技术网

C# 如何在jlinq中从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是一个元素,

我正在使用DocumentFormat.OpenXml.Wordprocessing在C#中合并文档

xml节点如下所示:

<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
            }
     }
}