如何检查属性值升序并查找重复项?

这是一个示例xml


<?xml version="1.0"?>

<catalog>

    <book id="bk101">

        <author>Gambardella, Matthew</author>

        <title>XML Developer's Guide</title>

        <genre>Computer</genre>

        <price>44.95</price>

        <publish_date>2000-10-01</publish_date>

        <description>An in-depth look at creating applications

        with XML.</description>

    </book>

    <book id="bk102">

        <author>Ralls, Kim</author>

        <title>Midnight Rain</title>

        <genre>Fantasy</genre>

        <price>5.95</price>

        <publish_date>2000-12-16</publish_date>

        <description>A former architect battles corporate zombies,

            an evil sorceress, and her own childhood to become queen

        of the world.</description>

    </book>

    <book id="bk102">

        <author>Corets, Eva</author>

        <title>Maeve Ascendant</title>

        <genre>Fantasy</genre>

        <price>5.95</price>

        <publish_date>2000-11-17</publish_date>

        <description>After the collapse of a nanotechnology

            society in England, the young survivors lay the

        foundation for a new society.</description>

    </book>

    <book id="bk103">

        <author>Corets, Eva</author>

        <title>Oberon's Legacy</title>

        <genre>Fantasy</genre>

        <price>5.95</price>

        <publish_date>2001-03-10</publish_date>

        <description>In post-apocalypse England, the mysterious

            agent known only as Oberon helps to create a new life

            for the inhabitants of London. Sequel to Maeve

        Ascendant.</description>

    </book>

</catalog>

如何检查节点中属性ID的值是否<book>按升序排列,还以最简单的方式查找其中是否存在重复值。我做了


static void Main(string[] args)

{


    XDocument myfile = XDocument.Parse(File.ReadAllText(@"D:\sample_xml.xml"));

    var check = myfile.Descendants("book").Select(a => a.Attribute("id").Value.Substring(2)).ToArray();


但这并不能说明重复的值...我该怎么做?


另外,是否有可能在属性id中找到缺失值(如果有),例如,如果存在bk109而下一个是bk112,则程序将显示bk110和bk111缺失。


梵蒂冈之花
浏览 143回答 2
2回答

小怪兽爱吃肉

您已经快到了-比较结果为0(即值与上一个相同)时,您将执行“严格递增,不重复”和“递增,允许重复”之间的唯一区别。如果比较的结果不是,您只需要更改IsSortedAscending返回的方法即可:false>= 0> 0public static bool IsSortedAscending(string[] arr){&nbsp; &nbsp; for (int i = arr.Length - 2; i >= 0; i--)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Fail if this ID is equal to or bigger than the next one.&nbsp; &nbsp; &nbsp; &nbsp; if (arr[i].CompareTo(arr[i + 1]) >= 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true;}(您也可以使用Skip和Zip作为另一种成对比较元素的方式,但这是稍有不同的事情。)请注意,如果您的数字长度不同,当前您的代码可能会失败。例如,考虑ID“ bk99”和“ bk100”。它将比较“ 99”和“ 100”作为字符串,并确定“ 99”在“ 100”之后。如果您的ID始终是真的“ bk”,后跟一个整数,我会尽早解析它们:var ids = myfile.Descendants("book")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(a => a.Attribute("id").Value.Substring(2))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(id => int.Parse(id))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToArray();然后,您将更改方法以接受int[]而不是string[]。到那时,检查“缺失” ID也更容易-字符串形式,没有“缺失” ID的真实概念,因为您可能会有“ bk101”,“ bk101a”,“ bk101c”-是“ bk101b” ”在那里不见了吗?如果是这样,那么“ bk101aa”呢?使用整数,它要简单得多。获得整数ID数组后,就可以使用数组的长度来检查是否缺少任何值:if (ids.Length > 0 ids.Length - 1 != ids.Last() - ids.First()){&nbsp; &nbsp; Console.WriteLine("At least one ID is missing");}诚然,那不会告诉您缺少哪个ID。
打开App,查看更多内容
随时随地看视频慕课网APP