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. Optimising XML

Optimising XML

Scheduled Pinned Locked Moved C#
xmltutorialquestion
3 Posts 3 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.
  • S Offline
    S Offline
    si_69
    wrote on last edited by
    #1

    Hi All I am trying to find a way to optimise the below code for my web service In my webservice i recieve 10 different XML files, each xml file is always the same format, at the moment i have 10 different methods to handle each one, the below code is an example of one of the methods. The only difference between the methods are the local variables and the node attributes. Instead of having 10 methods almost identical i would like to have just one. Can anyone advise on the best way to achieve this ? thanks

    public void NavigateXmlSessionData(XPathNavigator xPathNav)
    {

            xPathNav.MoveToRoot(); 
    
            xPathNav.MoveToFirstChild(); 
    
    
            xPathNav.MoveToFirstChild(); 
            string  description, personId; 
           // initalise vars 
            description = ""; 
            personId = ""; 
    
            do 
            { 
                //display the child nodes 
                if (xPathNav.MoveToFirstChild()) 
                { 
                    while (xPathNav.MoveToNext()) 
                    { 
    
                        switch (xPathNav.Name) 
                        { 
                            #region NodeAttributes 
                            case "Description": 
                                { 
                                    StringBuilder strBuild = new StringBuilder(xPathNav.Value, 200); 
                                    description = strBuild.Replace('\\'', ' ').ToString(); 
                                    break; 
                                } 
                            case "PersonId": 
                                { 
                                    StringBuilder strBuild = new StringBuilder(xPathNav.Value, 200); 
                                    personId = strBuild.Replace('\\'', ' ').ToString(); 
                                    break; 
                                } 
                                #endregion 
    
    
                        } 
    
                    } 
                    //move back to the parent 
                    xPathNav.MoveToParent(); 
                } 
                ProcessRecord(); 
                // initalise vars 
                description = ""; 
                personId = ""; 
    
    
            } while (xPathNav.MoveToNext()); 
        }
    
    P R 2 Replies Last reply
    0
    • S si_69

      Hi All I am trying to find a way to optimise the below code for my web service In my webservice i recieve 10 different XML files, each xml file is always the same format, at the moment i have 10 different methods to handle each one, the below code is an example of one of the methods. The only difference between the methods are the local variables and the node attributes. Instead of having 10 methods almost identical i would like to have just one. Can anyone advise on the best way to achieve this ? thanks

      public void NavigateXmlSessionData(XPathNavigator xPathNav)
      {

              xPathNav.MoveToRoot(); 
      
              xPathNav.MoveToFirstChild(); 
      
      
              xPathNav.MoveToFirstChild(); 
              string  description, personId; 
             // initalise vars 
              description = ""; 
              personId = ""; 
      
              do 
              { 
                  //display the child nodes 
                  if (xPathNav.MoveToFirstChild()) 
                  { 
                      while (xPathNav.MoveToNext()) 
                      { 
      
                          switch (xPathNav.Name) 
                          { 
                              #region NodeAttributes 
                              case "Description": 
                                  { 
                                      StringBuilder strBuild = new StringBuilder(xPathNav.Value, 200); 
                                      description = strBuild.Replace('\\'', ' ').ToString(); 
                                      break; 
                                  } 
                              case "PersonId": 
                                  { 
                                      StringBuilder strBuild = new StringBuilder(xPathNav.Value, 200); 
                                      personId = strBuild.Replace('\\'', ' ').ToString(); 
                                      break; 
                                  } 
                                  #endregion 
      
      
                          } 
      
                      } 
                      //move back to the parent 
                      xPathNav.MoveToParent(); 
                  } 
                  ProcessRecord(); 
                  // initalise vars 
                  description = ""; 
                  personId = ""; 
      
      
              } while (xPathNav.MoveToNext()); 
          }
      
      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Maybe pass in the XPath?

      1 Reply Last reply
      0
      • S si_69

        Hi All I am trying to find a way to optimise the below code for my web service In my webservice i recieve 10 different XML files, each xml file is always the same format, at the moment i have 10 different methods to handle each one, the below code is an example of one of the methods. The only difference between the methods are the local variables and the node attributes. Instead of having 10 methods almost identical i would like to have just one. Can anyone advise on the best way to achieve this ? thanks

        public void NavigateXmlSessionData(XPathNavigator xPathNav)
        {

                xPathNav.MoveToRoot(); 
        
                xPathNav.MoveToFirstChild(); 
        
        
                xPathNav.MoveToFirstChild(); 
                string  description, personId; 
               // initalise vars 
                description = ""; 
                personId = ""; 
        
                do 
                { 
                    //display the child nodes 
                    if (xPathNav.MoveToFirstChild()) 
                    { 
                        while (xPathNav.MoveToNext()) 
                        { 
        
                            switch (xPathNav.Name) 
                            { 
                                #region NodeAttributes 
                                case "Description": 
                                    { 
                                        StringBuilder strBuild = new StringBuilder(xPathNav.Value, 200); 
                                        description = strBuild.Replace('\\'', ' ').ToString(); 
                                        break; 
                                    } 
                                case "PersonId": 
                                    { 
                                        StringBuilder strBuild = new StringBuilder(xPathNav.Value, 200); 
                                        personId = strBuild.Replace('\\'', ' ').ToString(); 
                                        break; 
                                    } 
                                    #endregion 
        
        
                            } 
        
                        } 
                        //move back to the parent 
                        xPathNav.MoveToParent(); 
                    } 
                    ProcessRecord(); 
                    // initalise vars 
                    description = ""; 
                    personId = ""; 
        
        
                } while (xPathNav.MoveToNext()); 
            }
        
        R Offline
        R Offline
        RobCroll
        wrote on last edited by
        #3

        Not an answer to your question but if you want to optimize reading XML, use the XMLReader class. It streams the data so you don't have to wait for the whole file to be read and you don't have to wait for indexing to be completed. Having 1 method instead of 10 isn't going to offer much in the way of optimization but it will make the code more maintainable. Just isolate the minor differences and pass them as arguments or enumerated values.

        public void NavigateXmlSessionData(XPathNavigator xPathNav, MyEnum difference)

        "You get that on the big jobs."

        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