[英]How to check the values of attributes are in ascending order and also find duplicates?
这是一个示例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>
如何检查节点<book>
中的属性id的值是否按升序排列,还以最简单的方式查找其中是否存在重复的值。 我做了
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();
if (IsSortedAscending(check))
{
Console.WriteLine("Sorted in Ascending order");
}
else
{
Console.WriteLine("Check Sequence");
}
Console.ReadLine();
}
public static bool IsSortedAscending(string[] arr)
{
for (int i = arr.Length - 2; i >= 0; i--)
{
if (arr[i].CompareTo(arr[i + 1]) > 0)
{
return false;
}
}
return true;
}
但这并不能说明重复的值...我该怎么做?
另外,是否有可能在属性id中找到缺失值(如果有),例如,如果存在bk109而下一个是bk112,则程序将显示bk110和bk111缺失。
您已经快到了-比较结果为0(即值与上一个相同)时,您将执行“严格递增,不重复”和“递增,允许重复”之间的唯一区别。
如果比较的结果是>= 0
而不是> 0
IsSortedAscending
需要更改IsSortedAscending
方法以返回false
即可:
public static bool IsSortedAscending(string[] arr)
{
for (int i = arr.Length - 2; i >= 0; i--)
{
// Fail if this ID is equal to or bigger than the next one.
if (arr[i].CompareTo(arr[i + 1]) >= 0)
{
return false;
}
}
return true;
}
(您也可以使用“ Skip
和“ Zip
作为成对比较元素的另一种方式,但这是稍有不同的事情。)
请注意,如果您的数字长度不同,当前您的代码可能会失败。 例如,考虑ID“ bk99”和“ bk100”。 它将比较“ 99”和“ 100” 作为字符串,并确定“ 99”在“ 100”之后。
如果您的ID总是真的是“ bk”,后跟一个整数,我会尽早解析它们:
var ids = myfile.Descendants("book")
.Select(a => a.Attribute("id").Value.Substring(2))
.Select(id => int.Parse(id))
.ToArray();
然后,您将更改方法以接受int[]
而不是string[]
。
到那时,检查“缺失” ID也更容易-字符串形式,没有“缺失” ID的真实概念,因为您可能会有“ bk101”,“ bk101a”,“ bk101c”-是“ bk101b” ”在那里想念? 如果是这样,“ bk101aa”如何? 使用整数,要简单得多。
获得整数ID数组后,就可以使用数组的长度来检查是否缺少任何值:
if (ids.Length > 0 ids.Length - 1 != ids.Last() - ids.First())
{
Console.WriteLine("At least one ID is missing");
}
诚然,这不会告诉您缺少哪个 ID。
我只是将元素排序并放入字典中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement catalog = doc.Root;
Dictionary<string, List<XElement>> dict = catalog.Elements("book")
.OrderBy(x => (string)x.Attribute("id"))
.ThenBy(x => (DateTime)x.Element("publish_date"))
.GroupBy(x => (string)x.Attribute("id"), y => y)
.ToDictionary(x => x.Key, y => y.ToList());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.