Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XPathNavigator iterate and write

XPathNavigator iterate and write

Scheduled Pinned Locked Moved C#
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark F
    wrote on last edited by
    #1

    I am using XPathNavigator to sort nodes by name. Is there a method for writing the sorted nodes to the same or different file?

            foreach( XPathNavigator item in navigator.Select( expr ) )
            {
                 // write to new document here...,
            }
    

    Thanks, Mark

    C 1 Reply Last reply
    0
    • M Mark F

      I am using XPathNavigator to sort nodes by name. Is there a method for writing the sorted nodes to the same or different file?

              foreach( XPathNavigator item in navigator.Select( expr ) )
              {
                   // write to new document here...,
              }
      

      Thanks, Mark

      C Offline
      C Offline
      cechode
      wrote on last edited by
      #2

      How about something like this?

          static void Main(string\[\] args)
          {
              XDocument x = XDocument.Parse(sXml);
              Reorder(x.FirstNode);
              x.Save(newFileName);
          }
      
          private static void Reorder(XNode node)
          {
              var kids = (from nd in node.XPathSelectElements("\*") orderby nd.Name.LocalName select nd).ToList();
              if (kids.Count>1)
              {
                  (node as XElement).ReplaceNodes(kids);
              }
              kids.ForEach(f => Reorder(f));
          }
      
      M 1 Reply Last reply
      0
      • C cechode

        How about something like this?

            static void Main(string\[\] args)
            {
                XDocument x = XDocument.Parse(sXml);
                Reorder(x.FirstNode);
                x.Save(newFileName);
            }
        
            private static void Reorder(XNode node)
            {
                var kids = (from nd in node.XPathSelectElements("\*") orderby nd.Name.LocalName select nd).ToList();
                if (kids.Count>1)
                {
                    (node as XElement).ReplaceNodes(kids);
                }
                kids.ForEach(f => Reorder(f));
            }
        
        M Offline
        M Offline
        Mark F
        wrote on last edited by
        #3

        The xml is displayed in a TreeView control so alphabetical sorting by 'name' attribute was necessary. I worked out something similar to your reply and problem solved. Haven't had a prior occasion to use Linq however. Thanks for the help!

        modified on Friday, December 3, 2010 3:28 PM

        C 1 Reply Last reply
        0
        • M Mark F

          The xml is displayed in a TreeView control so alphabetical sorting by 'name' attribute was necessary. I worked out something similar to your reply and problem solved. Haven't had a prior occasion to use Linq however. Thanks for the help!

          modified on Friday, December 3, 2010 3:28 PM

          C Offline
          C Offline
          cechode
          wrote on last edited by
          #4

          of course not. ( there are several ways as you'd expect, heres one, not the best not the worst ) how's this ?

              static void Main(string\[\] args)
              {
                  string newFileName = "c:\\\\myfilename.xml";
          
                  XmlDocument docSource = new XmlDocument();
                  docSource.LoadXml(XML);
                  Reorder(docSource.DocumentElement);
                  docSource.Save(newFileName);
              }
          
          
              private static void Reorder(XmlNode node)
              {
                  SortedList childlist = SortedChildNodes(node);
                  node.RemoveAll();
                  for (int i = 0; i < childlist.Count; i++)
                  {
                      Reorder(node.AppendChild(childlist.Values\[i\]));
                  }
          
              }
              static SortedList SortedChildNodes(XmlNode node)
              {
                  SortedList ret = new SortedList();
                  for (int i = 0; i < node.ChildNodes.Count; i++)
                  {
                      string name = node.ChildNodes\[i\].Name;
                      XmlNode nd = node.ChildNodes\[i\];
                      ret.Add(name, nd);
                  }
                  return ret;
              }
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups