Click here to Skip to main content
15,122,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a XML:

XML
<Articles>
<Article id="article_1-1" file="article_1-1.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_1-3" file="article_1-3.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_4-1" file="article_4-1.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_4-2" file="article_4-2.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_4-3" file="article_4-3.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_5-1" file="article_5-1.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_1-2" file="article_1-2.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_2-1" file="article_2-1.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_2-2" file="article_2-2.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_1-4" file="article_1-4.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_13-1" file="article_13-1.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_6-1" file="article_6-1.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_6-2" file="article_6-2.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_6-3" file="article_6-3.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_10-1" file="article_10-1.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_10-2" file="article_10-2.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_11-1" file="article_11-1.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_11-2" file="article_11-2.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_14-1" file="article_14-1.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_14-2" file="article_14-2.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_15-1" file="article_15-1.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_15-2" file="article_15-2.xml">
<title ref="headline">Title value</title>
</Article>
<Article id="article_16-1" file="article_16-1.xml">
<title ref="headline">Title value</title>
</Article>
</Articles>


I want to sort is in ascending order and I am unable to do it.

I am getting an error
Quote:
System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string.
Parameter name: length'


What I have tried:

C#
xrepdoc.Root.Element(replicaNamespace + "Articles").Elements(replicaNamespace + "Article")
.OrderBy(el => Int32.Parse(el.Attribute("id").Value.Substring(el.Attribute("id").Value.IndexOf("_"), el.Attribute("id").Value.IndexOf("-"))))
.ThenBy(el => Int32.Parse(el.Attribute("id").Value.Substring(el.Attribute("id").Value.IndexOf("-") + 1)))
.ToList();
Posted
Updated 2-Jun-19 23:30pm
Comments
lmoelleb 3-Jun-19 4:43am
   
Thomas already provided an answer, but for the future: You can set breakpoints in the lambdas. This will allow you to examine the call to substring with a debugger.

1 solution

Substring's second argument is length, but you are passing the end index.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900