Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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# 无法使用LINQ对Xelement进行排序_C#_Linq_Xelement - Fatal编程技术网

C# 无法使用LINQ对Xelement进行排序

C# 无法使用LINQ对Xelement进行排序,c#,linq,xelement,C#,Linq,Xelement,我正在尝试按优先级节点对下面的XML进行排序,因此1.0是最高的,然后紧随其后。然而,我尝试的一切都不会有回报。这是我的代码,第一个块是我的一次尝试,它只是空的,第二个块是我的内联尝试,它返回空引用 第一: XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; IEnumerable<XElement> list = from el i

我正在尝试按优先级节点对下面的XML进行排序,因此1.0是最高的,然后紧随其后。然而,我尝试的一切都不会有回报。这是我的代码,第一个块是我的一次尝试,它只是空的,第二个块是我的内联尝试,它返回空引用

第一:

 XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
            IEnumerable<XElement> list =
                from el in xelementList1.Elements(ns + "url")
                let priority = (int)el.Element(ns + "priority")
                orderby priority descending
                select el;
xns=”http://www.sitemaps.org/schemas/sitemap/0.9";
IEnumerable列表=
来自xelementList1.元素中的el(ns+“url”)
让优先级=(int)el.Element(ns+“优先级”)
按优先级递减排序
选择el;
第二:

XDocument xdocument = this.BuildXmlDocument((IEnumerable<XElement>)xelementList1.OrderByDescending(x => x.Element("priority")?.Value));
XDocument XDocument=this.BuildXmlDocument((IEnumerable)xelementList1.OrderByDescending(x=>x.Element(“priority”)?.Value));
文件:

<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" 
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https:///</loc>
<lastmod>2021-03-15</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>https:///404/</loc>
<lastmod>2021-04-22</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>https:///500/</loc>
<lastmod>2021-04-22</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>https:///avalon-brochures/</loc>
<lastmod>2021-04-27</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https:///contact-us/</loc>
<lastmod>2021-04-27</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https:///cors-test/</loc>
<lastmod>2021-03-15</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>

https:///
2021-03-15
每日的
0.5
https:///404/
2021-04-22
每日的
0.5
https:///500/
2021-04-22
每日的
0.5
https:///avalon-brochures/
2021-04-27
每日的
1
https:///contact-us/
2021-04-27
每日的
0.8
https:///cors-test/
2021-03-15
每日的
0.5

您应该将数据持久化(存储)过程与数据处理过程分开。这使您的代码:

  • 更容易理解,因为程序更小
  • 更易于重用,因为如果数据不是来自XML,而是来自JSON或数据库,则可以使用相同的数据处理 -更容易更改:如果您希望以不同的格式保存数据,例如JSON或CSV,则不必更改LINQ部分
  • 更容易进行单元测试:您可以测试存储/检索数据,而不必担心LINQ部分:如果将来更改LINQ(例如降序或按不同列排序),您就不必更改关于持久性的单元测试
(我不确定您的XML中有什么,在回答的其余部分,我将称之为
urldomptions

因此,您需要两个独立的代码部分,甚至可能是独立的类:

  • 从XML文件读取数据并将其转换为
    urldomptions
    序列的部分。类似:在XML文件中存储一系列
    UrlModifications
    。可能的扩展:从
    流读取/写入
  • 一种部件,它接受一系列的
    URL修改
    ,并按优先级降序排列
是否值得为此创建单独的类,取决于您是否认为某些人可能希望重用或更改此代码,或者是否希望为其编写单元测试,等等

对文件的读/写修改 首先是类URL修改。我想是这样的:

class UrlModification
{
    public string Location {get; set;}
    public DateTime LastModifiedDate {get; set; }
    public FrequencyDescription ChangeFrequency {get; set;}
    public decimal Priority {get; set;}
}
FrequencyDescription是一个枚举。您可以将其更改为字符串,如果有人键入不受支持的值,则可能会出现问题。如果你不想被诸如“代码>每日/小时/每周/……/代码>这样的值所限制,考虑使用一个时间段。代码>每日
将替换为24小时。无论如何,您都需要一个转换方法,因为要使用此值执行任何操作,您需要将单词
Daily
转换为
TimeSpan 24小时

要将其读写到文件中,我将创建一个repository类(仓库意义上的repository):您将项目存储在其中,以后您可以从中检索相同的项目,即使您已经重新启动了程序

为了使这个类更易于重用,我不仅将它保存到一个文件中,还将它保存到一个流和一个TextReader/TextWriter中。这也将使单元测试更容易

class UrlModificationRepository
{
    public IList<UrlModifiction> Load(string fileName)
    {
        using (var textReader = File.OpenText(fileName))
        {
            return Load(textReader);
        }
    }

    public IList<UrlModification> Load(System.IO.TextReader textReader)
    {
        return (UrlModification[]) serializer.Deserialize(textReader);
    }
}
你可能比我更了解XML,所以我想你会明白要点的

订购URL 我正在尝试按优先级节点对下面的XML进行排序,因此1.0是最高的,然后紧随其后

创建存储库后,这将非常简单:

string fileName = ...
UrlModificationRepository repository = new UrlModidificationRepository();

var urlsOrderedByDescendingPriority = repository.Load(fileName)
    .OrderByDescending(urlModification => urlModification.Priority);
结论
通过将数据的存储方式与数据处理分离,您使代码更易于理解和单元测试。代码具有高度的可重用性和可更改性。

如果您不按顺序运行代码,您的代码会返回任何内容吗?
public class UrlModification
{
    [XmlElement("Loc")]
    public string Location { get; set; }

    [XmlElement("LastMod", DataType = "date")]
    public DateTime LastModifiedDate { get; set; }
    public FrequencyDescription ChangeFrequency { get; set; }
    public decimal Priority { get; set; }
}
string fileName = ...
UrlModificationRepository repository = new UrlModidificationRepository();

var urlsOrderedByDescendingPriority = repository.Load(fileName)
    .OrderByDescending(urlModification => urlModification.Priority);