Category Archives: Sorting

Topics related to specific methods for sorting data

XML to XML – Translating XML with C#

Working with XML documents is the new way of EDI in today’s business work.  XML to XML provides a way to not only translate data that you want to work with, but also provides a way to store extreme amounts of information in a structured manner.  In this article I will discuss how to use XML in two forms, XML to XML or moving one XML document to another XML document and translating it along the way and XML to Object.  XML to Objects will be used in the XML to XML process but it will also help with keeping your XML uniform as well.

The most important piece in XML to XML is uniform documents

Starting out with a fresh document is very important in being successful in translation.  Here is an example of a simple yet effective XML document:

xml to xml job definition

The first part of XML to XML is to load the document into a XmlDocument file.  We do this through instantiating a XmlDocument and calling the Load method targeting our file.  The XmlDocument will be used to populate our new document and then write out to finish our XML to XML process.

            XmlDocument xmlJobDoc = new System.Xml.XmlDocument();
            XmlDocument xmlLoadDocument = new System.Xml.XmlDocument();
            XmlNode xmlJobNode;
            XmlAttribute xmlAttribute;
            XmlNode xmlRootNode = xmlLoadDocument.CreateElement("Root");
            xmlJobDoc.Load("JobDefinition.xml");
            for (int i = 0; i < 2; i++)
            {
                xmlJobNode = xmlLoadDocument.CreateElement("TruckRun");
                xmlAttribute = xmlLoadDocument.CreateAttribute("JobNumber");
                xmlAttribute.Value = i.ToString();
                xmlJobNode.Attributes.Append(xmlAttribute);

                xmlJobNode.AppendChild(xmlLoadDocument.ImportNode(xmlJobDoc.DocumentElement,true));
                xmlRootNode.AppendChild(xmlJobNode);
            }
            xmlLoadDocument.AppendChild(xmlRootNode);
            xmlLoadDocument.Save("JobFile.xml");

Xml to Xml utilizing Xml ImportNode Command

This will utilize the Xml to Xml specific command called ImportNode. ImportNode is powerful as it allows you to copy one specific section of an Xml document to another Xml document or use the Xml to Xml functionality as if you were copy and pasting. To finish up the Xml to Xml translation, we utilize the Save method on the Xml document and push the information to the specified file.  If you would like to learn more about translating Xml documents from Xml to Xml or other Xml processing capabilities of Microsoft dotNet Framework, check out this MSDN article.

HOW TO: Sort an array using an IComparable

A common problem in .NET is being able to present your data to a user in a way that makes sense to them. One such way is to provide them a list of information that is sorted in a way that is proper to their function. For example, if you were showing a list of transactions, you may want to sort in a descending order based on their date to show the last transaction that happened.

This can be accomplished using an array and/or list and the IComparable. In the object which you are sorting, implement the interface and use any rules necessary to compare two objects. In our previous example, we discussed a transaction. Here is a transaction object which sorts based on the transaction date:

Public Class Transaction
   Implements IComparable
   Private m_TransactionAmount As Decimal
   Private m_TransactionDate As Date
   Public Sub New(ByVal TransactionAmount As Decimal, ByVal TransactionDate As Date)
      m_TransactionAmount = TransactionAmount
      m_TransactionDate = TransactionDate
   End Sub

   Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
      'implements a icomparer rule to allow for sorting based on transaction date, and then transaction type

      Dim dteValueToCompare As Date
      Dim blnCredit As Boolean
      If TypeOf obj Is Transaction Then
         dteValueToCompare = CType(obj, Transaction).TransactionDate
      Else
         dteValueToCompare = obj
      End If
      Return Date.Compare(TransactionDate, dteValueToCompare)
   End Function
End Class

Now, to sort the transactions, you can use an Array and use the Array.Sort method to sort them by date. You can also use a Array.Reverse to get a reverse sort in case you want them in descending order. Happy coding!