Beispiel #1
0
        /// <summary>
        /// Creates a new SPSS data document, initializing its dictionary
        /// by copying the dictionary from an existing SPSS data file.
        /// </summary>
        /// <param name="filename">
        /// The filename of the new document to create.
        /// </param>
        /// <param name="copyDictionaryFromFileName">
        /// The filename of the existing SPSS data file to copy the dictionary from.
        /// </param>
        /// <returns>
        /// The newly created <see cref="SpssDataDocument">SPSS data document</see>.
        /// </returns>
        public static SpssDataDocument Create(string filename, string copyDictionaryFromFileName)
        {
            if (File.Exists(filename))
            {
                throw new InvalidOperationException("File to create already exists.");
            }

            if (!File.Exists(copyDictionaryFromFileName))
            {
                throw new FileNotFoundException("File to copy does not exist.", copyDictionaryFromFileName);
            }

            using (SpssDataDocument read = SpssDataDocument.Open(copyDictionaryFromFileName, SpssFileAccess.Read))
            {
                SpssDataDocument toReturn = new SpssDataDocument(filename, SpssFileAccess.Create);

                foreach (SpssVariable var in read.Variables)
                {
                    toReturn.Variables.Add(var.Clone());
                }

                toReturn.CommitDictionary();
                return(toReturn);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Converts the metadata in an SPSS .SAV data file into a DataTable.
        /// </summary>
        /// <param name="spssSavFilename">
        /// The filename of the SPSS .SAV data file.
        /// </param>
        /// <returns>
        /// The <see cref="DataTable"/> containing all the metadata.
        /// </returns>
        public static DataTable ToDataTable(string spssSavFilename)
        {
            if (spssSavFilename == null)
            {
                throw new ArgumentNullException("spssSavFilename");
            }
            DataTable dataTable = new DataTable();

            using (SpssDataDocument doc = SpssDataDocument.Open(spssSavFilename, SpssFileAccess.Read))
            {
                ToDataTable(doc, dataTable);
            }

            // Return the completed DataTable.
            return(dataTable);
        }
Beispiel #3
0
        /// <summary>
        /// Converts the metadata in an SPSS .SAV data file into a DDI codebook.
        /// </summary>
        /// <param name="spssSavFilename">
        /// The filename of the SPSS .SAV data file.
        /// </param>
        /// <returns>
        /// The <see cref="XmlDocument"/> containing all the metadata.
        /// </returns>
        public static XmlDocument ToDdi(string spssSavFilename)
        {
            const string ddiNamespace = "http://www.icpsr.umich.edu/DDI";

            if (spssSavFilename == null)
            {
                throw new ArgumentNullException("spssSavFilename");
            }

            XmlDocument ddi = new XmlDocument();

            // Build initial ddi document up.
            // Open up SPSS file and fill in the ddi var tags.
            using (SpssDataDocument doc = SpssDataDocument.Open(spssSavFilename, SpssFileAccess.Read))
            {
                ddi.PreserveWhitespace = true;

                // Read from the embedded xml file: blankDdi.xml into the ddi document
                ddi.LoadXml(EmbeddedResources.LoadFileFromAssemblyWithNamespace("/blankDdi.xml", Project.DefaultNamespace));

                // This is where the hard coding ends and methods are called to extract data from the sav file
                XmlElement          xmlRoot = ddi.DocumentElement;
                XmlNamespaceManager xmlNS   = new XmlNamespaceManager(ddi.NameTable);
                xmlNS.AddNamespace("ddi", ddiNamespace);
                XmlNode nData = xmlRoot.SelectSingleNode(@"ddi:dataDscr", xmlNS);

                foreach (SpssVariable var in doc.Variables)
                {
                    string nameOfVar = var.Name;

                    // variable name and its ID and then if its a numeric : its interval
                    XmlElement variable = ddi.CreateElement("ddi:var", ddiNamespace);
                    variable.SetAttribute("ID", string.Empty, nameOfVar);
                    variable.SetAttribute("name", string.Empty, nameOfVar);

                    // This is the variable that holds the characteristic whether the variable has discrete or continuous interval
                    int Dec;
                    if (var is SpssNumericVariable)
                    {
                        Dec = ((SpssNumericVariable)var).PrintDecimal;
                        string interval = string.Empty;
                        if (Dec == 0)
                        {
                            interval = "discrete";
                        }
                        else
                        {
                            interval = "contin";
                        }

                        variable.SetAttribute("intrvl", string.Empty, interval);
                    }

                    // for the location width part
                    XmlElement location = ddi.CreateElement("ddi:location", ddiNamespace);
                    int        Wid      = var.ColumnWidth;
                    location.SetAttribute("width", Wid.ToString());
                    variable.AppendChild(location);

                    // label of the variable is set in "varlabel" and extracted using var.Label
                    XmlElement varLabel = ddi.CreateElement("ddi:labl", ddiNamespace);
                    varLabel.InnerText = var.Label;
                    variable.AppendChild(varLabel);

                    foreach (var response in var.GetValueLabels())
                    {
                        XmlElement answer = ddi.CreateElement("ddi:catgry", ddiNamespace);

                        // catValue(category Value) is the element storing the text i.e. option number
                        XmlElement catValue = ddi.CreateElement("ddi:catValu", ddiNamespace);
                        catValue.InnerText = response.Key;
                        answer.AppendChild(catValue);

                        // catLabel(category Label) is the element storing the text i.e. name of answer
                        XmlElement catLabel = ddi.CreateElement("ddi:labl", ddiNamespace);
                        catLabel.InnerText = response.Value;
                        answer.AppendChild(catLabel);

                        // appending the answer option to the parent "variable" node i.e. the question node
                        variable.AppendChild(answer);
                    }

                    // end of extracting the response values for each variable
                    XmlElement varFormat = ddi.CreateElement("ddi:varFormat", ddiNamespace);

                    if (var is SpssNumericVariable)
                    {
                        varFormat.SetAttribute("type", "numeric");
                    }
                    else if (var is SpssStringVariable)
                    {
                        varFormat.SetAttribute("type", "character");
                    }
                    else
                    {
                        throw new NotSupportedException("Variable " + nameOfVar + " is not a string or a numeric variable type.");
                    }

                    variable.AppendChild(varFormat);

                    nData.AppendChild(variable);
                }

                // end of extraction of each variable and now we have put all the variable data into ndata
                // Return the completed ddi file.
                return(ddi);
            }
        }