Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.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# 如何按名称对XML元素排序_C# - Fatal编程技术网

C# 如何按名称对XML元素排序

C# 如何按名称对XML元素排序,c#,C#,我需要按元素名称对xmlelement进行排序。下面我提到了我的前整理和后整理文件 文件数据,如: <Root> <RB-3-10 K="3" P="0.5" L="5" G="5.5" E="3" l="5" O="2.5" /> <RB-4-6 K="3" P="0.5" L="6" G="5.5" E="3" l="6" O="2.5" /> <RB-3-5 K="3" P="0.5" L="

我需要按元素名称对xmlelement进行排序。下面我提到了我的前整理和后整理文件

文件数据,如:

  <Root>
       <RB-3-10  K="3" P="0.5" L="5" G="5.5" E="3" l="5" O="2.5" />
       <RB-4-6   K="3" P="0.5" L="6" G="5.5" E="3" l="6" O="2.5" />
       <RB-3-5   K="3" P="0.5" L="10" G="5.5" E="3" l="10" O="2.5" />
       <RB-3-12  K="3" P="0.5" L="12" G="5.5" E="3" l="12" O="2.5" />
       <RB-4-14  K="3" P="0.5" L="14" G="5.5" E="3" l="14" O="2.5" />
       <RB-3-15  K="3" P="0.5" L="15" G="5.5" E="3" l="15" O="2.5" />
       <RB-5-16  K="3" P="0.5" L="16" G="5.5" E="3" l="16" O="2.5" />
  </Root>

但我需要像这样的输出

  <Root>
       <RB-3-5    K="3" P="0.5" L="10" G="5.5" E="3" l="10" O="2.5" />
       <RB-3-10   K="3" P="0.5" L="5" G="5.5" E="3" l="5" O="2.5" />
       <RB-3-12   K="3" P="0.5" L="12" G="5.5" E="3" l="12" O="2.5" />
       <RB-3-15   K="3" P="0.5" L="15" G="5.5" E="3" l="15" O="2.5" />
       <RB-4-6    K="3" P="0.5" L="6" G="5.5" E="3" l="6" O="2.5" />
       <RB-4-14   K="3" P="0.5" L="14" G="5.5" E="3" l="14" O="2.5" />
       <RB-5-16   K="3" P="0.5" L="16" G="5.5" E="3" l="16" O="2.5" /> 
  </Root>

任何人都知道如何按名称对元素进行排序。

试试这个(假设元素名称中有相同的模式):

您也可以只使用一个来
OrderBy
(不使用
ThenBy
),但您必须事先知道整数在XML标记名中的大小。下面是一个假设数字不超过2位的示例

var ordered = xDoc.Root.Elements()
    .OrderBy(i => Convert.ToInt32(
        i.Name.LocalName.Split('-')[1].PadLeft(2, '0') +
        i.Name.LocalName.Split('-')[2].PadLeft(2, '0')))
    .ToList();
最后但并非最不重要的一点,这里有一种方法可以通过使用
IComparer
实现来实现(积分应计入此项):

NaturalSortComparer
类:

public class NaturalSortComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        return StrCmpLogicalW(x, y);
    }

    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
    public static extern int StrCmpLogicalW(string x, string y);
}
公共类自然端口比较器:IComparer
{
公共整数比较(字符串x、字符串y)
{
返回strcplulogicalw(x,y);
}
[DllImport(“shlwapi.dll”,CharSet=CharSet.Unicode,ExactSpelling=true)]
公共静态外部int strcplulogicalw(字符串x,字符串y);
}
试试这个(假设元素名称中有相同的模式):

您也可以只使用一个来
OrderBy
(不使用
ThenBy
),但您必须事先知道整数在XML标记名中的大小。下面是一个假设数字不超过2位的示例

var ordered = xDoc.Root.Elements()
    .OrderBy(i => Convert.ToInt32(
        i.Name.LocalName.Split('-')[1].PadLeft(2, '0') +
        i.Name.LocalName.Split('-')[2].PadLeft(2, '0')))
    .ToList();
最后但并非最不重要的一点,这里有一种方法可以通过使用
IComparer
实现来实现(积分应计入此项):

NaturalSortComparer
类:

public class NaturalSortComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        return StrCmpLogicalW(x, y);
    }

    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
    public static extern int StrCmpLogicalW(string x, string y);
}
公共类自然端口比较器:IComparer
{
公共整数比较(字符串x、字符串y)
{
返回strcplulogicalw(x,y);
}
[DllImport(“shlwapi.dll”,CharSet=CharSet.Unicode,ExactSpelling=true)]
公共静态外部int strcplulogicalw(字符串x,字符串y);
}

看,看,看,谢谢,亚历克斯·菲利波维奇先生,工作正常。谢谢,亚历克斯·菲利波维奇先生,工作正常。。