/// <summary> DEMO 3: Read the resulting demo2.dat file, change the title, publisher, and add /// a subject field and then save it again</summary> private static void demo3() { Console.WriteLine("Performing demo3"); // Create the marc21 exchange reader MARC21_Exchange_Format_Parser parser1 = new MARC21_Exchange_Format_Parser(); // Read the record MARC_Record record = parser1.Parse("demo2.dat"); // If this is null, say so if (record == null) { Console.WriteLine("Unable to read the demo2.dat in the 3rd demo portion"); return; } // Change the title field ( 245 ) record[245][0].Add_NonRepeatable_Subfield('a', "New Title"); // Also change the creator field (110 in this case) record[110][0].Add_NonRepeatable_Subfield('a', "Corn Maze Production, Incorporated"); // Add a new field to record MARC_Field newSubject = record.Add_Field(650, ' ', '0'); newSubject.Add_Subfield('a', "Soils"); newSubject.Add_Subfield('x', "Phosphorous content"); newSubject.Add_Subfield('z', "Indonesia"); // Save this as XML and also as Marc21 record.Save_MARC_XML("demo3.xml"); record.Save_MARC21("demo3.dat"); }
/// <summary> Reads the data from a XML Node Reader </summary> /// <param name="nodeReader">XML Node Reader </param> /// <param name="Record"> Record into which to read the contents of the MarcXML file </param> /// <returns>TRUE if successful, otherwise FALSE </returns> public static bool Read_MARC_Info(XmlReader nodeReader, MARC_Record Record) { try { // Move to the this node move_to_node(nodeReader, "record"); // Get the leader information int tag = -1; while (nodeReader.Read()) { if ((nodeReader.NodeType == XmlNodeType.EndElement) && (nodeReader.Name == "record")) { return(true); } if (nodeReader.NodeType == XmlNodeType.Element) { switch (nodeReader.Name.Trim().Replace("marc:", "")) { case "leader": nodeReader.Read(); Record.Leader = nodeReader.Value; break; case "controlfield": // Get the tag if (nodeReader.MoveToAttribute("tag")) { // The tag should always be numeric per the schema, but just relaxing this // for invalid MARC so the rest of the data can be successfully read. if (Int32.TryParse(nodeReader.Value, out tag)) { // Move to the value and then add this nodeReader.Read(); Record.Add_Field(tag, nodeReader.Value); } } break; case "datafield": // Set the default indicators char ind1 = ' '; char ind2 = ' '; // Get the indicators if they exist while (nodeReader.MoveToNextAttribute()) { if (nodeReader.Name.Trim() == "ind1") { string temp1 = nodeReader.Value; if (temp1.Length > 0) { ind1 = temp1[0]; } } if (nodeReader.Name.Trim() == "ind2") { string temp2 = nodeReader.Value; if (temp2.Length > 0) { ind2 = temp2[0]; } } if (nodeReader.Name.Trim() == "tag") { tag = Convert.ToInt32(nodeReader.Value); } } // Add this datafield MARC_Field newField = Record.Add_Field(tag, ind1, ind2); // Now, add each subfield while (nodeReader.Read()) { if ((nodeReader.NodeType == XmlNodeType.EndElement) && (nodeReader.Name.Replace("marc:", "") == "datafield")) { break; } if ((nodeReader.NodeType == XmlNodeType.Element) && (nodeReader.Name.Replace("marc:", "") == "subfield")) { // Get the code nodeReader.MoveToFirstAttribute(); char subfield = nodeReader.Value.Length > 0 ? nodeReader.Value[0] : ' '; // Get the value nodeReader.Read(); string dataValue = nodeReader.Value; // Save this subfield newField.Add_Subfield(subfield, dataValue); // Do some special stuff if this is the 260 if (tag == 260) { newField.Control_Field_Value = newField.Control_Field_Value + "|" + subfield + " " + dataValue + " "; } } } break; } } } return(true); } catch { return(false); } }