C# 解析 XML 文档以查找 paraId 等于某个值的 标签。

huangapple go评论88阅读模式
英文:

C# parsing an xml document to find <w:p> tag with paraId equal to some value

问题

我正在使用以下代码来解析Word文档的XML,但我不知道如何使用LINQ查询来找到我需要的节点。基本上,我有一个表格,所以有<w:tr>标签,我想要定位到paraId等于"12345"的那个,然后获取该<w:tr>节点内部的<w:p>标签。到目前为止,我的代码如下:

XDocument xdc = XDocument.Parse(docText);
var arrNames = xdc.Root
    .Descendants("w:tr")
    .Where(e => (string)e.Attribute(XName.Get("paraId", "w14")) == "12345")
    .Elements("w:p")
    .ToArray();

请注意,我在代码中使用XName.Get来处理w14:paraId属性的命名空间。这将帮助您选择paraId等于"12345"的<w:tr>节点,并获取其中的<w:p>标签。

英文:

I am using the following code to parse the xml of word document but I am unable to figure out how to do the LINQ query to find the node I need. Basically I have a table so there is a <w:tr> tags and I want to zone in on the one with a paraId equal to "12345" and I need to get the <w:p> tags inside of this <w:tr> node. My code so far is as follows:

                    XDocument xdc = XDocument.Parse(docText);
                    var arrNames = xdc.Root
                    .Descendants(&quot;w:tr&quot;)
                    .Select(e =&gt; e.Attribute(&quot;w14:paraId&quot;)).ToArray();

I am not very familiar with LINQ or XML so this is a bit tricky for me. The structure of the xml is pretty much as follows:

       &lt;w:tr w:rsidRPr=&quot;00DC3742&quot; w:rsidR=&quot;009336A8&quot; w:rsidTr=&quot;1FCAB362&quot; w14:paraId=&quot;570B1706&quot; w14:textId=&quot;77777777&quot;&gt;
        &lt;w:tc&gt;
           &lt;w:tcPr&gt;
              &lt;w:tcW w:w=&quot;10728&quot; w:type=&quot;dxa&quot; /&gt;
              &lt;w:tcBorders&gt;
                 &lt;w:top w:val=&quot;single&quot; w:color=&quot;auto&quot; w:sz=&quot;4&quot; w:space=&quot;0&quot; /&gt;
                 &lt;w:left w:val=&quot;single&quot; w:color=&quot;auto&quot; w:sz=&quot;4&quot; w:space=&quot;0&quot; /&gt;
                 &lt;w:bottom w:val=&quot;single&quot; w:color=&quot;auto&quot; w:sz=&quot;4&quot; w:space=&quot;0&quot; /&gt;
                 &lt;w:right w:val=&quot;single&quot; w:color=&quot;auto&quot; w:sz=&quot;4&quot; w:space=&quot;0&quot; /&gt;
              &lt;/w:tcBorders&gt;
              &lt;w:tcMar /&gt;
           &lt;/w:tcPr&gt;
           &lt;w:p w:rsidRPr=&quot;00896505&quot; w:rsidR=&quot;009336A8&quot; w:rsidP=&quot;1FCAB362&quot; w:rsidRDefault=&quot;009336A8&quot; w14:paraId=&quot;3574A5D8&quot; w14:textId=&quot;5A2EB300&quot;&gt;
              &lt;w:pPr&gt;
                 &lt;w:pStyle w:val=&quot;ListParagraph&quot; /&gt;
                 &lt;w:numPr&gt;
                    &lt;w:ilvl w:val=&quot;0&quot; /&gt;
                    &lt;w:numId w:val=&quot;10&quot; /&gt;
                 &lt;/w:numPr&gt;
                 &lt;w:spacing w:after=&quot;0&quot; /&gt;
                 &lt;w:rPr&gt;
                    &lt;w:rFonts w:ascii=&quot;Times New Roman&quot; w:hAnsi=&quot;Times New Roman&quot; w:cs=&quot;Times New Roman&quot; /&gt;
                    &lt;w:b w:val=&quot;1&quot; /&gt;
                    &lt;w:bCs w:val=&quot;1&quot; /&gt;
                 &lt;/w:rPr&gt;
              &lt;/w:pPr&gt;
              &lt;w:r w:rsidRPr=&quot;1FCAB362&quot; w:rsidR=&quot;03C18AEE&quot;&gt;
                 &lt;w:rPr&gt;
                    &lt;w:rFonts w:ascii=&quot;Times New Roman&quot; w:hAnsi=&quot;Times New Roman&quot; w:cs=&quot;Times New Roman&quot; /&gt;
                    &lt;w:b w:val=&quot;1&quot; /&gt;
                    &lt;w:bCs w:val=&quot;1&quot; /&gt;
                 &lt;/w:rPr&gt;
                 &lt;w:t&gt;This actually worked! Take 2&lt;/w:t&gt;
              &lt;/w:r&gt;
           &lt;/w:p&gt;
        &lt;/w:tc&gt;
     &lt;/w:tr&gt;

So I am basically just trying to target the <w:p> tags within the table row and store them as an array or just serialize them to a string.

答案1

得分: 1

以下是翻译好的代码部分:

var xDoc = XDocument.Parse(docText);
var root = xDoc.Root;

var w = root.GetNamespaceOfPrefix("w");
var w14 = root.GetNamespaceOfPrefix("w14");

var xRow = root.Descendants(w + "tr").FirstOrDefault(
    tr => tr.Attribute(w14 + "paraId")?.Value == "12345");

if (xRow != null)
{
    var xPars = xRow.Descendants(w + "p").ToArray();
    // TODO, use "w:p" elements...
}
英文:

Try this:

var xDoc = XDocument.Parse(docText);
var root = xDoc.Root;

var w = root.GetNamespaceOfPrefix(&quot;w&quot;);
var w14 = root.GetNamespaceOfPrefix(&quot;w14&quot;);

var xRow = root.Descendants(w + &quot;tr&quot;).FirstOrDefault(
    tr =&gt; tr.Attribute(w14 + &quot;paraId&quot;)?.Value == &quot;12345&quot;);

if (xRow != null)
{
    var xPars = xRow.Descendants(w + &quot;p&quot;).ToArray();
    // TODO, use &quot;w:p&quot; elements...
}

</details>



huangapple
  • 本文由 发表于 2023年4月13日 23:05:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定