Sort XML

  • Thread starter Thread starter Deep
  • Start date Start date
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
 
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
 
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?
 
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.
 
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);
 
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! |
************************************************
 
Back
Top