/// <summary> Returns a string which represents a record in machine readable record format. </summary>
        /// <param name="Record"> MARC record to convert to MARC21 </param>
        /// <returns> MARC record as MARC21 Exchange format record string</returns>
        public static string To_Machine_Readable_Record(MARC_Record Record)
        {
            // Create the stringbuilder for this
            StringBuilder directory = new StringBuilder(1000);
            StringBuilder completefields = new StringBuilder(2000);
            StringBuilder completeLine = new StringBuilder(200);

            // Step through each entry by key from the hashtable
            List<string> overallRecord = new List<string>();
            int runningLength = 0;

            // Step through each field ( control and data ) in the record
            foreach (MARC_Field thisEntry in Record.Sorted_MARC_Tag_List)
            {
                // Perpare to build this line
                if (completeLine.Length > 0)
                    completeLine.Remove(0, completeLine.Length);

                // Is this a control field (with no subfields) or a data field?
                if (thisEntry.Subfield_Count == 0)
                {
                    if (!String.IsNullOrEmpty(thisEntry.Control_Field_Value))
                    {
                        completeLine.Append(int_to_string(thisEntry.Tag, 3) + Record_Seperator);
                        completeLine.Append(thisEntry.Control_Field_Value);
                        overallRecord.Add(completeLine.ToString());
                    }
                }
                else
                {
                    // Start this tag and add the indicator, if there is one
                    if (thisEntry.Indicators.Length == 0)
                        completeLine.Append(int_to_string(thisEntry.Tag, 3) + Record_Seperator);
                    else
                        completeLine.Append(int_to_string(thisEntry.Tag, 3) + Record_Seperator + thisEntry.Indicators);

                    // Build the complete line
                    foreach (MARC_Subfield thisSubfield in thisEntry.Subfields)
                    {
                        if (thisSubfield.Subfield_Code == ' ')
                        {
                            if (thisEntry.Indicators.Length == 0)
                                completeLine.Append(thisSubfield.Data);
                            else
                                completeLine.Append(Unit_Seperator.ToString() + thisSubfield.Data);
                        }
                        else
                        {
                            completeLine.Append(Unit_Seperator.ToString() + thisSubfield.Subfield_Code + thisSubfield.Data);
                        }
                    }

                    // Add this to the list
                    overallRecord.Add(completeLine.ToString());
                }
            }

            // Now, add these to the directory and completefields StringBuilders
            foreach (string thisLin in overallRecord)
            {
                // Add this line to the directory and fields
                directory.Append(thisLin.Substring(0, 3) + (int_to_string(adjusted_length(thisLin) - 3, 4)) + (int_to_string(runningLength, 5)));
                completefields.Append(thisLin.Substring(3));

                // Increment the running length
                runningLength += adjusted_length(thisLin) - 3;
            }

            // Get the length of just the directory, before we start appending more to it
            int directory_length = directory.Length;

            // Compile the return value
            directory.Append(completefields.ToString() + Record_Seperator + Group_Seperator);

            // Get the leader
            string leader = Record.Leader;

            // Insert the total length of this record
            runningLength += leader.Length + directory_length + 2;

            // Return the combination of these two fields, plus the end of record char
            return int_to_string(runningLength, 5) + leader.Substring(5, 7) + int_to_string(leader.Length + directory_length + 1, 5) +
                   leader.Substring(17) + directory;
        }
 /// <summary> Append a single record to the file </summary>
 /// <param name="Record">New record to append </param>
 public void AppendRecord(MARC_Record Record)
 {
     writer.Write(To_Machine_Readable_Record(Record));
 }
 /// <summary> Returns this MARC record as a MarcXML-formatted string </summary>
 /// <param name="Record"> MARC record to convert to a MarcXML-formatted string</param>
 /// <returns> This record as MarcXML-formatted string with the XML and collection declarative tags around the record </returns>
 public static string To_MarcXML(MARC_Record Record)
 {
     return To_MarcXML(Record, true);
 }
        /// <summary> Returns this MARC record as a MarcXML-formatted string </summary>
        /// <param name="Record"> MARC record to convert to a MarcXML-formatted string</param>
        /// <param name="Include_Start_End_Tags"> Flag indicates whether to include the XML and collection declarative tags around the record </param>
        /// <returns> This record as MarcXML-formatted string </returns>
        public static string To_MarcXML(MARC_Record Record, bool Include_Start_End_Tags)
        {
            StringBuilder returnValue = new StringBuilder(5000);

            // Add the MARC XML header and start this collection
            if (Include_Start_End_Tags)
            {
                const string indent = "    ";
                returnValue.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
                returnValue.AppendLine("<collection xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
                returnValue.AppendLine(indent + "xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim");
                returnValue.AppendLine(indent + indent + "http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"");
                returnValue.AppendLine(indent + "xmlns=\"http://www.loc.gov/MARC21/slim\">");
            }

            // Begin this record and add the leader
            XElement record_root = new XElement("record",
                                                new XElement("leader", Record.Leader)
                );

            // Step through each field in the collection
            foreach (MARC_Field thisField in Record.Sorted_MARC_Tag_List)
            {
                if (thisField.Subfield_Count == 0)
                {
                    if (thisField.Control_Field_Value.Length > 0)
                    {
                        // Create this new control field and add it to the root element
                        XElement controlField = new XElement("controlfield",
                                                             thisField.Control_Field_Value.Replace(Convert.ToChar((byte)0x1F), ' '),
                                                             new XAttribute("tag", thisField.Tag.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0'))
                            );
                        record_root.Add(controlField);
                    }
                }
                else
                {
                    // Create the new datafield element and add it to the root element
                    XElement dataField = new XElement("datafield",
                                                      new XAttribute("tag", thisField.Tag.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0')),
                                                      new XAttribute("ind1", thisField.Indicator1),
                                                      new XAttribute("ind2", thisField.Indicator2)
                        );
                    record_root.Add(dataField);

                    // Add each subfield
                    foreach (MARC_Subfield thisSubfield in thisField.Subfields)
                    {
                        // Create this subfield element and add it to the datafield
                        XElement subfield = new XElement("subfield",
                                                         thisSubfield.Data.Replace(Convert.ToChar((byte)0x1F), ' '),
                                                         new XAttribute("code", thisSubfield.Subfield_Code)
                            );
                        dataField.Add(subfield);
                    }
                }
            }

            // Add the XML text to the string builder
            returnValue.Append(record_root.ToString());

            // Close this collection, if requested
            if (Include_Start_End_Tags)
            {
                returnValue.AppendLine();
                returnValue.AppendLine("</collection>");
            }

            return returnValue.ToString();
        }
 /// <summary> Append a single record to the file </summary>
 /// <param name="Record">New record to append </param>
 public void AppendRecord(MARC_Record Record)
 {
     writer.WriteLine(To_MarcXML(Record, false));
 }