xml

Asked By madhu radha
07-Nov-09 01:36 AM
Earn up to 0 extra points for answering this tough question.

how to sort data xmldocument  with out reading data binto dataset in csharp.net 

  Re:

web mavin replied to madhu radha
07-Nov-09 01:50 AM
You could sort it using XSL and XslCompiledTransform. So, you could have an approach something like this:
  1. Write an xsl that matches your xml, sorts them and had the sorted list as output
  2. Get the XslCompiledTransform holding that xsl from cache, and if it doesn't exist, create it and insert into cache
  3. Transform your xml through your xsl into a new XmlDocument

Also, check this link for more info.

  re

Web Star replied to madhu radha
07-Nov-09 11:33 AM

Code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"Input.xml");

XmlDocument xmlDocCopy = new XmlDocument();
xmlDocCopy.LoadXml(xmlDoc.OuterXml);
xmlDocCopy.SelectSingleNode("//Links").RemoveAll();

XmlNode node = xmlDoc.SelectSingleNode("//Links");
XPathNavigator navigator = node.CreateNavigator();
XPathExpression selectExpression = navigator.Compile("Link/Title");
selectExpression.AddSort(".", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text);
XPathNodeIterator nodeIterator = navigator.Select(selectExpression);
while (nodeIterator.MoveNext())
{
    XmlNode linkNode = xmlDoc.SelectSingleNode("//Link[Title=\"" + nodeIterator.Current.Value + "\"]");
    XmlNode importedLinkNode = xmlDocCopy.ImportNode(linkNode, true);
    xmlDocCopy.SelectSingleNode("//Links").AppendChild(importedLinkNode);
}

xmlDocCopy.Save(@"Output.xml");

  Yes, Web Mavin is right.

[)ia6l0 iii replied to madhu radha
08-Nov-09 01:43 PM
The approach seems to be well thought of in terms of execution. 

Also worth having a look is the usefulness of XSL to sort and filter the data. Charlie Heinemann has written an article on this about a decade back and you can view it here.

  doubt
madhu radha replied to Web Star
14-Nov-09 02:25 AM

hi,

  u prefered this code for sorting of data in xml document.actually i dont know wat is navigator add y we useed navigator.compile in which situations we can use them.

Code:


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"Input.xml");


XmlDocument xmlDocCopy = new XmlDocument();
xmlDocCopy.LoadXml(xmlDoc.OuterXml);
xmlDocCopy.SelectSingleNode("//Links").RemoveAll();


XmlNode node = xmlDoc.SelectSingleNode("//Links");
XPathNavigator navigator = node.CreateNavigator();
XPathExpression selectExpression = navigator.Compile("Link/Title");
selectExpression.AddSort(".", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text);
XPathNodeIterator nodeIterator = navigator.Select(selectExpression);
while (nodeIterator.MoveNext())
{
    XmlNode linkNode = xmlDoc.SelectSingleNode("//Link[Title=\"" + nodeIterator.Current.Value + "\"]");
    XmlNode importedLinkNode = xmlDocCopy.ImportNode(linkNode, true);
    xmlDocCopy.SelectSingleNode("//Links").AppendChild(importedLinkNode);
}


xmlDocCopy.Save(@"Output.xml");

  doubt
madhu radha replied to Web Star
14-Nov-09 02:25 AM

hi,

  u prefered this code for sorting of data in xml document.actually i dont know wat is navigator add y we useed navigator.compile in which situations we can use them.

Code:


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"Input.xml");


XmlDocument xmlDocCopy = new XmlDocument();
xmlDocCopy.LoadXml(xmlDoc.OuterXml);
xmlDocCopy.SelectSingleNode("//Links").RemoveAll();


XmlNode node = xmlDoc.SelectSingleNode("//Links");
XPathNavigator navigator = node.CreateNavigator();
XPathExpression selectExpression = navigator.Compile("Link/Title");
selectExpression.AddSort(".", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text);
XPathNodeIterator nodeIterator = navigator.Select(selectExpression);
while (nodeIterator.MoveNext())
{
    XmlNode linkNode = xmlDoc.SelectSingleNode("//Link[Title=\"" + nodeIterator.Current.Value + "\"]");
    XmlNode importedLinkNode = xmlDocCopy.ImportNode(linkNode, true);
    xmlDocCopy.SelectSingleNode("//Links").AppendChild(importedLinkNode);
}


xmlDocCopy.Save(@"Output.xml");

Create New Account