Optimising XML
-
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()); }
-
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()); }
Maybe pass in the XPath?
-
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()); }
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."