LINQ to XML specific question on extracting datahttp://forums.asp.net/t/1791350.aspx/1?LINQ+to+XML+specific+question+on+extracting+dataFri, 13 Apr 2012 11:39:19 -040017913504925670http://forums.asp.net/p/1791350/4925670.aspx/1?LINQ+to+XML+specific+question+on+extracting+dataLINQ to XML specific question on extracting data <p>Hello,</p> <p>I have an XML defined as follow</p> <pre class="prettyprint">&lt;showschedule&gt; &lt;fall name=&quot;FAL&quot; text=&quot;Y&quot; index=&quot;1&quot;/&gt; &lt;spring name=&quot;SPR&quot; text=&quot;Y&quot; index=&quot;2&quot;/&gt; &lt;winter name=&quot;WIN&quot; text=&quot;Y&quot; index=&quot;3&quot;/&gt; &lt;summer name=&quot;SUM&quot; text=&quot;Y&quot; index=&quot;4&quot;/&gt; &lt;/showschedule&gt;</pre> <p>I want to sort it using LINQ to XML and then put the sorted value of name into 4 hidden field according to thier index<br />However I am not sure how to do it and I currently just extract data by LINQ with out using sort.</p> <p>Anyone could help on this?</p> <p>The below is the code I used now..I think there should be a smarter way to do it&nbsp;with LINQ?</p> <pre class="prettyprint">Protected Sub LoadSchedule() Dim xele As XElement = XElement.Load(MapPath("~\xml\course_desc.xml")) Dim tarlist As New ArrayList tarlist.Add("fall") tarlist.Add("spring") tarlist.Add("winter") tarlist.Add("summer") For Each taritem In tarlist Dim xitems = From x In xele.Descendants("showschedule").Descendants(taritem.ToString) For Each x In xitems If x.@index = "1" Then Sch_1.Value = x.@name ElseIf x.@index = "2" Then Sch_2.Value = x.@name ElseIf x.@index = "3" Then Sch_3.Value = x.@name ElseIf x.@index = "4" Then Sch_4.Value = x.@name End If Next Next End Sub</pre> <p>&nbsp;</p> <p>Great THanks!</p> <p>&nbsp;</p> 2012-04-10T20:03:06-04:004925862http://forums.asp.net/p/1791350/4925862.aspx/1?Re+LINQ+to+XML+specific+question+on+extracting+dataRe: LINQ to XML specific question on extracting data <p>I'm not opposed to the way you're doing it.&nbsp; I wrote up the below in LINQPad, but its not much more concise, but I think its easier to read (its in C#):</p> <pre class="prettyprint">string xml = &quot;&lt;showschedule&gt;&lt;fall name=\&quot;FAL\&quot; text=\&quot;Y\&quot; index=\&quot;1\&quot;/&gt;&lt;spring name=\&quot;SPR\&quot; text=\&quot;Y\&quot; index=\&quot;2\&quot;/&gt;&lt;winter name=\&quot;WIN\&quot; text=\&quot;Y\&quot; index=\&quot;3\&quot;/&gt;&lt;summer name=\&quot;SUM\&quot; text=\&quot;Y\&quot; index=\&quot;4\&quot;/&gt;&lt;/showschedule&gt;&quot;; XElement ele = XElement.Parse(xml); var query = (from x in ele.Descendants() select new { Name = x.Attribute(&quot;name&quot;).Value, Text = x.Attribute(&quot;text&quot;).Value, Index = x.Attribute(&quot;index&quot;).Value }).OrderBy(x =&gt; x.Index).ToList(); Sch_1.Value = query[0].Name; Sch_2.Value = query[1].Name; Sch_3.Value = query[2].Name; Sch_4.Value = query[3].Name;</pre> <p><br> &nbsp;</p> 2012-04-11T00:04:51-04:004926142http://forums.asp.net/p/1791350/4926142.aspx/1?Re+LINQ+to+XML+specific+question+on+extracting+dataRe: LINQ to XML specific question on extracting data <p>@ Bee</p> <pre class="prettyprint">//I assume you want to sort on the Index, hence i have changed the index sequence in you //present XML. string mySchedules = &quot;&lt;showschedule&gt;&quot; &#43; &quot;&lt;fall name=\&quot;FAL\&quot; text=\&quot;Y\&quot; index=\&quot;3\&quot;/&gt;&quot; &#43; &quot;&lt;spring name=\&quot;SPR\&quot; text=\&quot;Y\&quot; index=\&quot;2\&quot;/&gt;&quot; &#43; &quot;&lt;winter name=\&quot;WIN\&quot; text=\&quot;Y\&quot; index=\&quot;1\&quot;/&gt;&quot; &#43; &quot;&lt;summer name=\&quot;SUM\&quot; text=\&quot;Y\&quot; index=\&quot;4\&quot;/&gt;&quot; &#43; &quot;&lt;/showschedule&gt;&quot;; XElement xShows = XElement.Parse(mySchedules); //This is how you sort IEnumerable&lt;XElement&gt; sortShows = from s in xShows.Descendants() orderby (int)s.Attribute(&quot;index&quot;) select s; //This will give you output in the object sortShows as below. /* &quot;&lt;showschedule&gt;&quot; &#43; &quot;&lt;winter name=\&quot;WIN\&quot; text=\&quot;Y\&quot; index=\&quot;1\&quot;/&gt;&quot; &#43; &quot;&lt;spring name=\&quot;SPR\&quot; text=\&quot;Y\&quot; index=\&quot;2\&quot;/&gt;&quot; &#43; &quot;&lt;fall name=\&quot;FAL\&quot; text=\&quot;Y\&quot; index=\&quot;3\&quot;/&gt;&quot; &#43; &quot;&lt;summer name=\&quot;SUM\&quot; text=\&quot;Y\&quot; index=\&quot;4\&quot;/&gt;&quot; &#43; &quot;&lt;/showschedule&gt;&quot;; */ //Now instead of writing nested if else, all you have to do is as below. string commaSeperatedSortedMsg = &quot;&quot;; foreach (XElement xE in sortShows) { commaSeperatedSortedMsg &#43;= &quot; | &quot; &#43; xE.Attribute(&quot;index&quot;).Value &#43; xE.Attribute(&quot;name&quot;).Value; }</pre> <p></p> 2012-04-11T05:59:01-04:004926412http://forums.asp.net/p/1791350/4926412.aspx/1?Re+LINQ+to+XML+specific+question+on+extracting+dataRe: LINQ to XML specific question on extracting data <p>Cool! Both works!</p> <p>But i want to have a follow up question,</p> <p>If I want to modify one of the item, say, Winter only,</p> <p>How could I access this specified item?</p> <p>&nbsp;</p> <p>The below is what i use after converting to VB</p> <pre class="prettyprint">Dim xele As XElement = XElement.Load(MapPath(&quot;~\xml\course_desc.xml&quot;)) Dim xitems = (From x In xele.Descendants(&quot;showschedule&quot;).Descendants() _ Select New With { _ .Sqlname = x.Attribute(&quot;sqlname&quot;).Value, _ .Sqlshow = x.Attribute(&quot;show&quot;).Value, _ .Index = x.Attribute(&quot;index&quot;).Value, _ .Name = x.Attribute(&quot;name&quot;).Value _ }).OrderBy(Function(x) x.Index).ToList()</pre> <p></p> 2012-04-11T08:05:28-04:004926609http://forums.asp.net/p/1791350/4926609.aspx/1?Re+LINQ+to+XML+specific+question+on+extracting+dataRe: LINQ to XML specific question on extracting data <pre class="prettyprint">//To update the text of Winter you might want to do this. string mySchedules = &quot;&lt;showschedule&gt;&quot; &#43; &quot;&lt;fall name=\&quot;FAL\&quot; text=\&quot;Y\&quot; index=\&quot;3\&quot;/&gt;&quot; &#43; &quot;&lt;spring name=\&quot;SPR\&quot; text=\&quot;Y\&quot; index=\&quot;2\&quot;/&gt;&quot; &#43; &quot;&lt;winter name=\&quot;WIN\&quot; text=\&quot;Y\&quot; index=\&quot;1\&quot;/&gt;&quot; &#43; &quot;&lt;summer name=\&quot;SUM\&quot; text=\&quot;Y\&quot; index=\&quot;4\&quot;/&gt;&quot; &#43; &quot;&lt;/showschedule&gt;&quot;; XElement xShows = XElement.Parse(mySchedules); IEnumerable&lt;XElement&gt; singleSchedule = from s in xShows.Descendants() where ((string)s.Attribute(&quot;name&quot;).Value.ToLower()).Equals(&quot;win&quot;) select s; foreach (XElement xWin in singleSchedule) { xWin.SetAttributeValue(&quot;text&quot;, &quot;N&quot;); } string myOutput = xShows.ToString(); /* It will give you output as ::see the text of Winter.:: * * &lt;showschedule&gt; &lt;fall name=&quot;FAL&quot; text=&quot;Y&quot; index=&quot;3&quot;/&gt; &lt;spring name=&quot;SPR&quot; text=&quot;Y&quot; index=&quot;2&quot;/&gt; &lt;winter name=&quot;WIN&quot; text=&quot;N&quot; index=&quot;1&quot;/&gt; &lt;summer name=&quot;SUM&quot; text=&quot;Y&quot; index=&quot;4&quot;/&gt; &lt;/showschedule&gt; * */</pre> <p></p> 2012-04-11T09:51:14-04:004929679http://forums.asp.net/p/1791350/4929679.aspx/1?Re+LINQ+to+XML+specific+question+on+extracting+dataRe: LINQ to XML specific question on extracting data <p>Hello Kavita,</p> <p>After I have select this node<br> <br> How can I delete this? THere is no method call to remove the node..?</p> <pre class="prettyprint">Dim xitem = (From x In xele.Descendants(name).Descendants(&quot;desc&quot;).Descendants() _ Select New With { _ .desc = x.Attribute(&quot;text&quot;).Value }).Where(Function(x) x.desc = ctl.Text).FirstOrDefault</pre> <p></p> 2012-04-12T17:09:50-04:004930358http://forums.asp.net/p/1791350/4930358.aspx/1?Re+LINQ+to+XML+specific+question+on+extracting+dataRe: LINQ to XML specific question on extracting data <pre class="prettyprint">//I dont seem to find the DESC element mentioned in you recent post, hence am caryying forward it // with below XML only. Lets say you want to delete the element having name=&quot;WIN&quot; string mySchedules = &quot;&lt;showschedule&gt;&quot; &#43; &quot;&lt;fall name=\&quot;FAL\&quot; text=\&quot;Y\&quot; index=\&quot;3\&quot;/&gt;&quot; &#43; &quot;&lt;spring name=\&quot;SPR\&quot; text=\&quot;Y\&quot; index=\&quot;2\&quot;/&gt;&quot; &#43; &quot;&lt;winter name=\&quot;WIN\&quot; text=\&quot;Y\&quot; index=\&quot;1\&quot;/&gt;&quot; &#43; &quot;&lt;summer name=\&quot;SUM\&quot; text=\&quot;Y\&quot; index=\&quot;4\&quot;/&gt;&quot; &#43; &quot;&lt;/showschedule&gt;&quot;; XElement xShows = XElement.Parse(mySchedules); IEnumerable&lt;XElement&gt; singleSchedule = from s in xShows.Descendants() where ((string)s.Attribute(&quot;name&quot;).Value.ToLower()).Equals(&quot;win&quot;) select s; singleSchedule.Remove(); string myOutput = xShows.ToString(); ///* // It will give you output as ::see the text of Winter.:: // * // * &lt;showschedule&gt; // &lt;fall name=&quot;FAL&quot; text=&quot;Y&quot; index=&quot;3&quot;/&gt; // &lt;spring name=&quot;SPR&quot; text=&quot;Y&quot; index=&quot;2&quot;/&gt; // &lt;summer name=&quot;SUM&quot; text=&quot;Y&quot; index=&quot;4&quot;/&gt; // &lt;/showschedule&gt; // * // */</pre> <p></p> <p></p> 2012-04-13T05:27:22-04:004931018http://forums.asp.net/p/1791350/4931018.aspx/1?Re+LINQ+to+XML+specific+question+on+extracting+dataRe: LINQ to XML specific question on extracting data <p>Hi Kavita,</p> <p>No problem to use the previous XML file.<br> <br> is it if I dont use <span class="typ">IEnumerable</span>, I cant use that way to remove?<br> <br> I tryed something like this in VB, but seems not working..<br> <br> </p> <pre class="prettyprint">Dim xele As XElement = XElement.Load(MapPath(&quot;~\xml\course_desc.xml&quot;)) Dim xitems As IEnumerable(Of XElement) = From x In xele.Descendants(&quot;core&quot;).Descendants(&quot;desc&quot;).Descendants() _ Where x.Attribute(&quot;name&quot;).Value.Equals(&quot;Students with CISSP, CISM or GCIH professional certification may apply for substitution for ISOM 5280.&quot;)</pre> <p></p> 2012-04-13T11:39:19-04:00