Sort XML

D

Deep

Dear friends,
I have an Xml File like
<?xml version="1.0" encoding="UTF-8"?>
<MainMenu>
<War ID="1" Title="Operation Iraqi Freedom"
VideoPath="WarMemorial_Assets/Videos/Iraqi.flv">
<Record ID="1" Name="Brancato, Capt. Carlo" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Employee" VideoPath="" />
<Record ID="2" Name="Hislip, Robert E." War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="3" Name="Massimino, Andrew" War="Operation Iraqi
Freedom" Branch="U.S. Navy" Served="Served" Affiliation="Student /
Employee" VideoPath="" />
<Record ID="4" Name="Montalvo, Ekne" War="Operation Iraqi Freedom"
Branch="U.S. Army" Served="Served" Affiliation="Student" VideoPath="" / <Record ID="5" Name="Moreira, GySgt. Valdemar" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="6" Name="Navarro. Oscar" War="Operation Iraqi Freedom"
Branch="U.S. Marine Corps" Served="Served" Affiliation="Student"
VideoPath="" />
<Record ID="7" Name="Sherman, Sgt. Stephen R." War="Operation Iraqi
Freedom" Branch="U.S. Army" Served="KIA" Affiliation="Son of Employee"
VideoPath="" />
<Record ID="8" Name="Vaccaro, GySgt. James" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
</War>
<War ID="2" Title="Operation Enduring Freedom"
VideoPath="WarMemorial_Assets/Videos/Enduring.flv">
<Record ID="1" Name="Greenberg, Keith" War="Operation Enduring
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="2" Name="Hislip, Robert E." War="Operation Enduring
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="3" Name="Newman, Jason" War="Operation Enduring Freedom"
Branch="U.S. Army" Served="Served" Affiliation="Student" VideoPath="" / <Record ID="4" Name="test name" War="Operation Enduring Freedom"
Branch="test branch" Served="student" Affiliation="indian army"
VideoPath="" />
</War>
</MainMenu>

I have to sort Child node of xml on Title basis and after sort its Id
should be managed in ascending order. I dont have knowledge of xml. I
think parent node can be sorted using dataview sort property but how
its child node can be sorted.

Somebody told me to create schema. Please tell me the process how to
do it

Please help me. Its urgent
Thanks in advance
 
M

Mr. Arnold

Deep said:
I have to sort Child node of xml on Title basis and after sort its Id
should be managed in ascending order. I dont have knowledge of xml. I
think parent node can be sorted using dataview sort property but how
its child node can be sorted.

Somebody told me to create schema. Please tell me the process how to
do it

Please help me. Its urgent

If you're using .Net Framework 2.0 or above you can use Linq-2-XML

You can do the same thing with C# or VB.Net. The examples are in VB,
which there are 'sort' examples in the link.

http://msdn.microsoft.com/en-us/vbasic/bb688087.aspx
 
G

Guest

Dear friends,
   I have an Xml File like
<?xml version="1.0" encoding="UTF-8"?>
<MainMenu>
        <War ID="1" Title="Operation Iraqi Freedom"
VideoPath="WarMemorial_Assets/Videos/Iraqi.flv">
                <Record ID="1" Name="Brancato, Capt. Carlo" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Employee" VideoPath="" />
                <Record ID="2" Name="Hislip, Robert E.." War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
                <Record ID="3" Name="Massimino, Andrew" War="Operation Iraqi
Freedom" Branch="U.S. Navy" Served="Served" Affiliation="Student /
Employee" VideoPath="" />
                <Record ID="4" Name="Montalvo, Ekne" War="Operation Iraqi Freedom"
Branch="U.S. Army" Served="Served" Affiliation="Student" VideoPath="" /

                <Record ID="5" Name="Moreira, GySgt. Valdemar" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
                <Record ID="6" Name="Navarro. Oscar" War="Operation Iraqi Freedom"
Branch="U.S. Marine Corps" Served="Served" Affiliation="Student"
VideoPath="" />
                <Record ID="7" Name="Sherman, Sgt. Stephen R." War="Operation Iraqi
Freedom" Branch="U.S. Army" Served="KIA" Affiliation="Son of Employee"
VideoPath="" />
                <Record ID="8" Name="Vaccaro, GySgt. James" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
        </War>
        <War ID="2" Title="Operation Enduring Freedom"
VideoPath="WarMemorial_Assets/Videos/Enduring.flv">
                <Record ID="1" Name="Greenberg, Keith" War="Operation Enduring
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
                <Record ID="2" Name="Hislip, Robert E.." War="Operation Enduring
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
                <Record ID="3" Name="Newman, Jason" War="Operation Enduring Freedom"
Branch="U.S. Army" Served="Served" Affiliation="Student" VideoPath="" /

                <Record ID="4" Name="test name" War="Operation Enduring Freedom"
Branch="test branch" Served="student" Affiliation="indian army"
VideoPath="" />
        </War>
</MainMenu>

I have to sort Child node of xml on Title basis and after sort its Id
should be managed in ascending order. I dont have knowledge of xml. I
think parent node can be sorted using dataview sort property but how
its child node can be sorted.

Somebody told me to create schema. Please tell me the process how to
do it

Please help me. Its urgent
Thanks in advance

Yes, you can use dataview for that. As far as I see the ids (by
<records> tag) are sorted from 1 to 10, do you mean you need to re-
sort it?
 
D

Deep

Yes, you can use dataview for that. As far as I see the ids (by
<records> tag) are sorted from 1 to 10, do you mean you need to re-
sort it?

Yes when some body enters another record then it will take another
largest id but after sorting child record with "name"
it can come in middle, then again its ids should be arranged otherwise
largest id can be in middle, it should be save in ascending order.
 
M

Martin Honnen

Deep said:
Dear friends,
I have an Xml File like
<?xml version="1.0" encoding="UTF-8"?>
<MainMenu>
<War ID="1" Title="Operation Iraqi Freedom"
VideoPath="WarMemorial_Assets/Videos/Iraqi.flv">
<Record ID="1" Name="Brancato, Capt. Carlo" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Employee" VideoPath="" />
<Record ID="2" Name="Hislip, Robert E." War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="3" Name="Massimino, Andrew" War="Operation Iraqi
Freedom" Branch="U.S. Navy" Served="Served" Affiliation="Student /
Employee" VideoPath="" />
<Record ID="4" Name="Montalvo, Ekne" War="Operation Iraqi Freedom"
Branch="U.S. Army" Served="Served" Affiliation="Student" VideoPath="" /
<Record ID="5" Name="Moreira, GySgt. Valdemar" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="6" Name="Navarro. Oscar" War="Operation Iraqi Freedom"
Branch="U.S. Marine Corps" Served="Served" Affiliation="Student"
VideoPath="" />
<Record ID="7" Name="Sherman, Sgt. Stephen R." War="Operation Iraqi
Freedom" Branch="U.S. Army" Served="KIA" Affiliation="Son of Employee"
VideoPath="" />
<Record ID="8" Name="Vaccaro, GySgt. James" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
</War>
<War ID="2" Title="Operation Enduring Freedom"
VideoPath="WarMemorial_Assets/Videos/Enduring.flv">
<Record ID="1" Name="Greenberg, Keith" War="Operation Enduring
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="2" Name="Hislip, Robert E." War="Operation Enduring
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="3" Name="Newman, Jason" War="Operation Enduring Freedom"
Branch="U.S. Army" Served="Served" Affiliation="Student" VideoPath="" /
<Record ID="4" Name="test name" War="Operation Enduring Freedom"
Branch="test branch" Served="student" Affiliation="indian army"
VideoPath="" />
</War>
</MainMenu>

I have to sort Child node of xml on Title basis and after sort its Id
should be managed in ascending order.

Do you want to extract some data in sorted order? Or do you want to
rewrite the XML document?

Here is an example to extract some data in sorted order, using LINQ:

XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");

var wars = from war in doc.Root.Elements("War")
orderby (string)war.Attribute("Title")
select new
{
title = (string)war.Attribute("Title"),
records = from record in war.Elements("Record")
orderby (int)record.Attribute("ID")
select
(string)record.Attribute("Name")
};

foreach (var war in wars)
{
Console.WriteLine("{0}:", war.title);
foreach (var record in war.records)
{
Console.WriteLine("\t{0}", record);
}
}

If you want to rewrite the XML in sorted order then it is best to create
a second document with the order you want e.g.

XDocument input = XDocument.Load(@"..\..\XMLFile1.xml");

XDocument output = new XDocument(
new XElement(input.Root.Name,
from war in input.Root.Elements("War")
orderby (string)war.Attribute("Title")
select new XElement(war.Name,
from record in war.Elements("Record")
orderby (int)record.Attribute("ID")
select record)));

// for testing, could of course save to file.
output.Save(Console.Out);
 
G

Gregory A. Beamer

Deep said:
Dear friends,
I have an Xml File like
<?xml version="1.0" encoding="UTF-8"?>
<MainMenu>
<War ID="1" Title="Operation Iraqi Freedom"
VideoPath="WarMemorial_Assets/Videos/Iraqi.flv">
<Record ID="1" Name="Brancato, Capt. Carlo" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Employee" VideoPath="" />
<Record ID="2" Name="Hislip, Robert E." War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="3" Name="Massimino, Andrew" War="Operation Iraqi
Freedom" Branch="U.S. Navy" Served="Served" Affiliation="Student /
Employee" VideoPath="" />
<Record ID="4" Name="Montalvo, Ekne" War="Operation Iraqi Freedom"
Branch="U.S. Army" Served="Served" Affiliation="Student" VideoPath="" /
<Record ID="5" Name="Moreira, GySgt. Valdemar" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="6" Name="Navarro. Oscar" War="Operation Iraqi Freedom"
Branch="U.S. Marine Corps" Served="Served" Affiliation="Student"
VideoPath="" />
<Record ID="7" Name="Sherman, Sgt. Stephen R." War="Operation Iraqi
Freedom" Branch="U.S. Army" Served="KIA" Affiliation="Son of Employee"
VideoPath="" />
<Record ID="8" Name="Vaccaro, GySgt. James" War="Operation Iraqi
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
</War>
<War ID="2" Title="Operation Enduring Freedom"
VideoPath="WarMemorial_Assets/Videos/Enduring.flv">
<Record ID="1" Name="Greenberg, Keith" War="Operation Enduring
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="2" Name="Hislip, Robert E." War="Operation Enduring
Freedom" Branch="U.S. Marine Corps" Served="Served"
Affiliation="Student" VideoPath="" />
<Record ID="3" Name="Newman, Jason" War="Operation Enduring Freedom"
Branch="U.S. Army" Served="Served" Affiliation="Student" VideoPath="" /
<Record ID="4" Name="test name" War="Operation Enduring Freedom"
Branch="test branch" Served="student" Affiliation="indian army"
VideoPath="" />
</War>
</MainMenu>

I have to sort Child node of xml on Title basis and after sort its Id
should be managed in ascending order. I dont have knowledge of xml. I
think parent node can be sorted using dataview sort property but how
its child node can be sorted.

Somebody told me to create schema. Please tell me the process how to
do it

LINQ to XML is a great abstraction for handling the manipulation of objects
in XML. LINQ, in general (although I am not fond of LINQ to SQL in most
apps), is a great abstraction for enumerating objects.

You end up with objects you can bind and the sort can be handled in the LINQ
query. I would go this route, as it is extremely simple once you understand
the syntax. I am not a big fan of LINQ to SQL, but LINQ to XML is a
wonderful way to handle this type of work.

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top