gpt4 book ai didi

c# - 如何在 C# 中找出 XML 中特定元素之前的元素?

转载 作者:太空宇宙 更新时间:2023-11-03 18:52:03 25 4
gpt4 key购买 nike

我有以下格式的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE repub SYSTEM "C:\repub\Repub_V1.dtd">
<?xml-stylesheet href="C:\repub\repub.xsl" type="text/xsl"?>
<repubold>
<head>
<title>xxx</title>
</head>
<body>
<sec>
<title>First Title</title>
<break name="1-1"/>
<pps>This is an invalid text.</pps>
<h1>
<page num="1"/>First Heading
</h1>
<bl>This is another text</bl>
<fig>
<img src="images/img_1-1.jpg" alt=""/>
<fc>This is a caption</fc>
</fig>
<p>
<bold>This</bold> again
<br/> is
<br/>
<bold> a
<br/>paragraph
</bold>
</p>
</sec>
<sec>
<title>Second Title</title>
<break name="2-1"/>
<h1>
<page num="1"/>Second Heading
</h1>
<bl>This is another text</bl>
<fig>
<img src="images/img_2-1.jpg" alt=""/>
<fc>This is a caption</fc>
<cr>This is a credit</cr>
</fig>
<p>This is a paragraph</p>
</sec>
<sec>
<title>First Title</title>
<break name="3-1"/>
<h1>
<page num="1"/>Third Heading
</h1>
<bl>This is another text</bl>
<fig>
<img src="images/img_3-1.jpg" alt=""/>
<fc>This is a caption</fc>
</fig>
<p>This is a paragraph</p>
</sec>
<sec>
<title>Third Title</title>
<break name="4-1"/>
<h1>
<page num="1"/>Fourth Heading
</h1>
<bl>This is another text</bl>
<p>This is a paragraph</p>
<fig>
<img src="images/img_4-1.jpg" alt=""/>
<fc>This is a caption</fc>
<cr>This is a credit</cr>
</fig>
<break name="5-1"/>
<h1>
<page num="1"/>Fifth Heading
</h1>
<bl>This is another text</bl>
<fig>
<img src="images/img_5-1.jpg" alt=""/>
<fc>This is a caption</fc>
<cr>This is a credit</cr>
</fig>
<p>This is a paragraph</p>
</sec>
</body>
</repubold>

在此,所有<break>标签后面跟着 <h1> .所以,我想检查 <h1> 之前的元素, 如果有的话。如果不是 <psf>然后它会显示错误。因为我想要那个 <psf><break> 之间唯一可接受的标签和 <h1> .可以是<psf>或什么都没有,但如果有任何其他 <xyz>标记,然后它会显示错误。

请帮忙。

我试过了,但是代码不工作:

var pagetag = xdoc.Descendants("break").Descendants("h1")
.Where(br => br.ElementsBeforeSelf("h1") != new XElement("psf") ||
br.ElementsBeforeSelf("h1") != new XElement("break"))
.Select(br => br.Attribute("name").Value.Trim())
.Aggregate((a, b) => a + ", " + b);

MessageBox.Show("The following articles have invalid tags before <h1>: " + pagetag);

最佳答案

第一个问题是ElementsBeforeSelf()返回元素的序列,但您正在检查该序列是否等于单个 XElement - 并使用 != 通过引用比较它们.

您还要求 break 的后代元素 - 没有任何元素。我想你只想要所有 h1元素。

为了阐明您的要求,我认为您正试图找到所有 h1元素,其中 h1 之前的最后一个兄弟元素既不是 break也不psf .对于其中的每一个元素,您都希望找到最新的 break h1 之前的元素(如果有的话)并报告 name属性。

假设是这种情况,这里有一些我认为可以满足您要求的代码,并附有注释解释:

using System;
using System.Linq;
using System.Xml.Linq;

public class Test
{
public static void Main()
{
var xdoc = XDocument.Load("test.xml");
XName brName = "break";
XName psfName = "psf";

var invalidNames =
from h1 in xdoc.Descendants("h1")
// Find the last sibling element before the h1
let previous = h1.ElementsBeforeSelf().LastOrDefault()
// It's invalid if there isn't a previous element, or it has
// a name other than break or psf
where previous?.Name != brName && previous?.Name != psfName
// Get the name to report, handling the case where there's
// no previous break or no "name" attribute
select ((string) h1.ElementsBeforeSelf(brName).LastOrDefault()?.Attribute("name")) ?? "(no named break)";

Console.WriteLine(string.Join(", ", invalidNames));
}
}

它有一点缺陷,如果一个 <h1>无效,但没有立即 <break>前身,它会回溯到较早的那个找到一个名字...所以如果你删除 <break name="5-1"/>例如元素,它将报告名称“4-1”无效,因为这是最后一个 break h1 之前的元素那在 5-1 之后。我不知道这对你有多重要。

关于c# - 如何在 C# 中找出 XML 中特定元素之前的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55548029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com