Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Problem with the recursion logic
Message
 
 
À
05/03/2013 23:52:56
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01567554
Message ID:
01567556
Vues:
33
>Without looking at your code, the most common cause of problems in recursion is improperly scoped variables. You want all variables to be local. The only inputs should be parameters passed in. Generally those parameters should be by value unless you really, really know what you're doing and you're willing to do some brutal debugging if it doesn't work as you expect.

I know. I almost got it to work now, but for the life of me I can not get the empty lines correct :((

See the output difference in the attached screen shot and my current implementation. It's 11pm here :(

So, right now the only difference seems to be the extra new line at the very end. I need to get rid of it to call it a day!
/// <summary>
      /// Formats a single element
      /// </summary>
      /// <param name="xe"></param>
      /// <param name="level"></param>
      /// <returns></returns>
      public String FormatSingleXmlElement(XElement xe, Int32 level = 0)
      {
         String result = "";
         String elementValue = "";
         String tagName = xe.Name.ToString();
         if (!xe.IsEmpty)
         {
            elementValue = xe.Value;

            if (!elementValue.IsValueEmpty())
            {
               if ("CC_SWIPE" == tagName.ToUpper())
               {
                  {
                     Int32 cardLength = elementValue.Length;
                     String mask = new String('X', cardLength - 4);
                     String maskedCardNumber = String.Format("{0}{1}", mask, elementValue.Substring(cardLength - 4));
                     result = "".PadRight(level) + (tagName + ":").PadRight(15) + maskedCardNumber;
                  }
               }
               else
               {
                  if (200 < elementValue.Length)
                     elementValue = elementValue.Left(200) + "...";
                  result = "".PadRight(level) + (tagName + ":").PadRight(15) + elementValue;
               }
            }
         }
         return result;
      }

      /// <summary>
      /// Based on the element either processes the current element or uses recursion
      /// </summary>
      /// <param name="element"></param>
      /// <param name="level"></param>
      /// <returns></returns>
      public String XmlToString(XElement element, Int32 level = 0)
      {
         StringBuilder output = new StringBuilder();
         String tagName = element.Name.ToString();
         String result = "";
         if (element.HasElements)
         {
            output.AppendLine("".PadRight(level) + (tagName + ":").PadRight(15));
            foreach (XNode xn in element.Nodes())
            {
               result = XmlToString((XElement)xn, level + 1);  
               if (!String.IsNullOrWhiteSpace(result))
                  output.AppendLine(result);
            }
         }
         else
         {
            return FormatSingleXmlElement(element, level);
         }

         return output.ToString();
      }

      /// <summary>
      /// Formats XML into a string
      /// </summary>
      /// <param name="input"></param>
      /// <param name="level"></param>
      /// <returns></returns>
      public String XmlDisplay(String input, Int32 level = 0)
      {
         try
         {
            if (0 == level)
            {
               String rootNode = "DummyNode";
               input = String.Format("<{0}>{1}</{0}>", rootNode, input);
            }
            if (level > 5)
               return ""; // don't create infinite loop

            XElement elements = XElement.Parse(input);
            StringBuilder output = new StringBuilder();
       
            foreach (XElement xe in elements.Elements())
            {
               output.Append(XmlToString(xe, level));
            }
            return output.ToString();
         }
         catch (Exception ex)
         {
            String error = ex.Message.ToString();
            Logging.Log(error, 1);
            return String.Format("<ERR>0</ERR><STATUSCODE>100</STATUSCODE><MSG>{0}</MSG>", error);
         }
      }
UPDATE. Found the answer by googling TrimEnd('\r', '\n');
http://stackoverflow.com/questions/873043/removing-carriage-return-and-new-line-from-the-end-of-a-string-in-c-sharp
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform