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 However I am not sure how to do it and I currently just extract data by LINQ with out using sort.
Anyone could help on this?
The below is the code I used now..I think there should be a smarter way to do it with LINQ?
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
I'm not opposed to the way you're doing it. I wrote up the below in LINQPad, but its not much more concise, but I think its easier to read (its in C#):
string xml = "<showschedule><fall name=\"FAL\" text=\"Y\" index=\"1\"/><spring name=\"SPR\" text=\"Y\" index=\"2\"/><winter name=\"WIN\" text=\"Y\" index=\"3\"/><summer name=\"SUM\" text=\"Y\" index=\"4\"/></showschedule>";
XElement ele = XElement.Parse(xml);
var query = (from x in ele.Descendants()
select new
{
Name = x.Attribute("name").Value,
Text = x.Attribute("text").Value,
Index = x.Attribute("index").Value
}).OrderBy(x => 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;
"Sometimes, it is more important to have the right problem than the best solution."
Marked as answer by Bee90124 on Apr 11, 2012 08:05 AM
//I assume you want to sort on the Index, hence i have changed the index sequence in you
//present XML.
string mySchedules = "<showschedule>" +
"<fall name=\"FAL\" text=\"Y\" index=\"3\"/>" +
"<spring name=\"SPR\" text=\"Y\" index=\"2\"/>" +
"<winter name=\"WIN\" text=\"Y\" index=\"1\"/>" +
"<summer name=\"SUM\" text=\"Y\" index=\"4\"/>" +
"</showschedule>";
XElement xShows = XElement.Parse(mySchedules);
//This is how you sort
IEnumerable<XElement> sortShows = from s in xShows.Descendants()
orderby (int)s.Attribute("index")
select s;
//This will give you output in the object sortShows as below.
/*
"<showschedule>" +
"<winter name=\"WIN\" text=\"Y\" index=\"1\"/>" +
"<spring name=\"SPR\" text=\"Y\" index=\"2\"/>" +
"<fall name=\"FAL\" text=\"Y\" index=\"3\"/>" +
"<summer name=\"SUM\" text=\"Y\" index=\"4\"/>" +
"</showschedule>";
*/
//Now instead of writing nested if else, all you have to do is as below.
string commaSeperatedSortedMsg = "";
foreach (XElement xE in sortShows)
{
commaSeperatedSortedMsg += " | " + xE.Attribute("index").Value + xE.Attribute("name").Value;
}
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
Marked as answer by Bee90124 on Apr 11, 2012 08:05 AM
How can I delete this? THere is no method call to remove the node..?
Dim xitem = (From x In xele.Descendants(name).Descendants("desc").Descendants() _
Select New With { _
.desc = x.Attribute("text").Value
}).Where(Function(x) x.desc = ctl.Text).FirstOrDefault
//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="WIN"
string mySchedules = "<showschedule>" +
"<fall name=\"FAL\" text=\"Y\" index=\"3\"/>" +
"<spring name=\"SPR\" text=\"Y\" index=\"2\"/>" +
"<winter name=\"WIN\" text=\"Y\" index=\"1\"/>" +
"<summer name=\"SUM\" text=\"Y\" index=\"4\"/>" +
"</showschedule>";
XElement xShows = XElement.Parse(mySchedules);
IEnumerable<XElement> singleSchedule = from s in xShows.Descendants()
where ((string)s.Attribute("name").Value.ToLower()).Equals("win")
select s;
singleSchedule.Remove();
string myOutput = xShows.ToString();
///*
// It will give you output as ::see the text of Winter.::
// *
// * <showschedule>
// <fall name="FAL" text="Y" index="3"/>
// <spring name="SPR" text="Y" index="2"/>
// <summer name="SUM" text="Y" index="4"/>
// </showschedule>
// *
// */
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
Marked as answer by Bee90124 on Apr 13, 2012 02:03 PM
is it if I dont use IEnumerable, I cant use that way to remove?
I tryed something like this in VB, but seems not working..
Dim xele As XElement = XElement.Load(MapPath("~\xml\course_desc.xml"))
Dim xitems As IEnumerable(Of XElement) = From x In xele.Descendants("core").Descendants("desc").Descendants() _
Where x.Attribute("name").Value.Equals("Students with CISSP, CISM or GCIH professional certification may apply for substitution for ISOM 5280.")
Bee90124
Member
33 Points
73 Posts
LINQ to XML specific question on extracting data
Apr 10, 2012 08:03 PM|LINK
Hello,
I have an XML defined as follow
<showschedule> <fall name="FAL" text="Y" index="1"/> <spring name="SPR" text="Y" index="2"/> <winter name="WIN" text="Y" index="3"/> <summer name="SUM" text="Y" index="4"/> </showschedule>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
However I am not sure how to do it and I currently just extract data by LINQ with out using sort.
Anyone could help on this?
The below is the code I used now..I think there should be a smarter way to do it with LINQ?
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 SubGreat THanks!
tehremo
Star
10540 Points
1704 Posts
Re: LINQ to XML specific question on extracting data
Apr 11, 2012 12:04 AM|LINK
I'm not opposed to the way you're doing it. I wrote up the below in LINQPad, but its not much more concise, but I think its easier to read (its in C#):
string xml = "<showschedule><fall name=\"FAL\" text=\"Y\" index=\"1\"/><spring name=\"SPR\" text=\"Y\" index=\"2\"/><winter name=\"WIN\" text=\"Y\" index=\"3\"/><summer name=\"SUM\" text=\"Y\" index=\"4\"/></showschedule>"; XElement ele = XElement.Parse(xml); var query = (from x in ele.Descendants() select new { Name = x.Attribute("name").Value, Text = x.Attribute("text").Value, Index = x.Attribute("index").Value }).OrderBy(x => 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;kavita_khand...
Star
9767 Points
1931 Posts
Re: LINQ to XML specific question on extracting data
Apr 11, 2012 05:59 AM|LINK
@ Bee
//I assume you want to sort on the Index, hence i have changed the index sequence in you //present XML. string mySchedules = "<showschedule>" + "<fall name=\"FAL\" text=\"Y\" index=\"3\"/>" + "<spring name=\"SPR\" text=\"Y\" index=\"2\"/>" + "<winter name=\"WIN\" text=\"Y\" index=\"1\"/>" + "<summer name=\"SUM\" text=\"Y\" index=\"4\"/>" + "</showschedule>"; XElement xShows = XElement.Parse(mySchedules); //This is how you sort IEnumerable<XElement> sortShows = from s in xShows.Descendants() orderby (int)s.Attribute("index") select s; //This will give you output in the object sortShows as below. /* "<showschedule>" + "<winter name=\"WIN\" text=\"Y\" index=\"1\"/>" + "<spring name=\"SPR\" text=\"Y\" index=\"2\"/>" + "<fall name=\"FAL\" text=\"Y\" index=\"3\"/>" + "<summer name=\"SUM\" text=\"Y\" index=\"4\"/>" + "</showschedule>"; */ //Now instead of writing nested if else, all you have to do is as below. string commaSeperatedSortedMsg = ""; foreach (XElement xE in sortShows) { commaSeperatedSortedMsg += " | " + xE.Attribute("index").Value + xE.Attribute("name").Value; }I would love to change the world, but they wont give me the source code.
Bee90124
Member
33 Points
73 Posts
Re: LINQ to XML specific question on extracting data
Apr 11, 2012 08:05 AM|LINK
Cool! Both works!
But i want to have a follow up question,
If I want to modify one of the item, say, Winter only,
How could I access this specified item?
The below is what i use after converting to VB
Dim xele As XElement = XElement.Load(MapPath("~\xml\course_desc.xml")) Dim xitems = (From x In xele.Descendants("showschedule").Descendants() _ Select New With { _ .Sqlname = x.Attribute("sqlname").Value, _ .Sqlshow = x.Attribute("show").Value, _ .Index = x.Attribute("index").Value, _ .Name = x.Attribute("name").Value _ }).OrderBy(Function(x) x.Index).ToList()kavita_khand...
Star
9767 Points
1931 Posts
Re: LINQ to XML specific question on extracting data
Apr 11, 2012 09:51 AM|LINK
//To update the text of Winter you might want to do this. string mySchedules = "<showschedule>" + "<fall name=\"FAL\" text=\"Y\" index=\"3\"/>" + "<spring name=\"SPR\" text=\"Y\" index=\"2\"/>" + "<winter name=\"WIN\" text=\"Y\" index=\"1\"/>" + "<summer name=\"SUM\" text=\"Y\" index=\"4\"/>" + "</showschedule>"; XElement xShows = XElement.Parse(mySchedules); IEnumerable<XElement> singleSchedule = from s in xShows.Descendants() where ((string)s.Attribute("name").Value.ToLower()).Equals("win") select s; foreach (XElement xWin in singleSchedule) { xWin.SetAttributeValue("text", "N"); } string myOutput = xShows.ToString(); /* It will give you output as ::see the text of Winter.:: * * <showschedule> <fall name="FAL" text="Y" index="3"/> <spring name="SPR" text="Y" index="2"/> <winter name="WIN" text="N" index="1"/> <summer name="SUM" text="Y" index="4"/> </showschedule> * */I would love to change the world, but they wont give me the source code.
Bee90124
Member
33 Points
73 Posts
Re: LINQ to XML specific question on extracting data
Apr 12, 2012 05:09 PM|LINK
Hello Kavita,
After I have select this node
How can I delete this? THere is no method call to remove the node..?
Dim xitem = (From x In xele.Descendants(name).Descendants("desc").Descendants() _ Select New With { _ .desc = x.Attribute("text").Value }).Where(Function(x) x.desc = ctl.Text).FirstOrDefaultkavita_khand...
Star
9767 Points
1931 Posts
Re: LINQ to XML specific question on extracting data
Apr 13, 2012 05:27 AM|LINK
//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="WIN" string mySchedules = "<showschedule>" + "<fall name=\"FAL\" text=\"Y\" index=\"3\"/>" + "<spring name=\"SPR\" text=\"Y\" index=\"2\"/>" + "<winter name=\"WIN\" text=\"Y\" index=\"1\"/>" + "<summer name=\"SUM\" text=\"Y\" index=\"4\"/>" + "</showschedule>"; XElement xShows = XElement.Parse(mySchedules); IEnumerable<XElement> singleSchedule = from s in xShows.Descendants() where ((string)s.Attribute("name").Value.ToLower()).Equals("win") select s; singleSchedule.Remove(); string myOutput = xShows.ToString(); ///* // It will give you output as ::see the text of Winter.:: // * // * <showschedule> // <fall name="FAL" text="Y" index="3"/> // <spring name="SPR" text="Y" index="2"/> // <summer name="SUM" text="Y" index="4"/> // </showschedule> // * // */I would love to change the world, but they wont give me the source code.
Bee90124
Member
33 Points
73 Posts
Re: LINQ to XML specific question on extracting data
Apr 13, 2012 11:39 AM|LINK
Hi Kavita,
No problem to use the previous XML file.
is it if I dont use IEnumerable, I cant use that way to remove?
I tryed something like this in VB, but seems not working..
Dim xele As XElement = XElement.Load(MapPath("~\xml\course_desc.xml")) Dim xitems As IEnumerable(Of XElement) = From x In xele.Descendants("core").Descendants("desc").Descendants() _ Where x.Attribute("name").Value.Equals("Students with CISSP, CISM or GCIH professional certification may apply for substitution for ISOM 5280.")