C# - 打开.RPD文件(伪装的XML文件),另存为.XML,然后在XmlDocument

时间:2015-09-01 10:14:17

标签: c# xml

正如标题所示 - 我正在尝试打开.RPD文件(伪装成XML文件),另存为.XML,然后使用XmlDocument打开XML进行处理。 。 。全部在c#。

下面的代码一直有效,直到遇到行字符串jN = jobName.InnerText;线。 我得到的错误是: -

System.NullReferenceException:未将对象引用设置为对象的实例。在XML Display.cs中的WindowsFormsApplication1.Form1.button1_Click(Object sender,EventArgs e):第73行

在我看来它并没有正确地获取新的XML文件 - 即使它存在吗?

有人能指出我正确的方向让它发挥作用吗?

// XML reading, writing and pulling out data.

using System;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // set up some variables to use
        public string document;
        public string material;
        public string thickness;
        public string sheetx;
        public string sheety;
        public string used;

        public Form1()
        {
            InitializeComponent();
        }

        public void label1_Click(object sender, EventArgs e) { }
        private void Form1_Load(object sender, EventArgs e) { }
        private void outputBox_TextChanged(object sender, EventArgs e) { }

        public void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            // Filter for xml files only
            fDialog.Filter = "RPD File|*.rpd";
            // Filter for starting directory - in this case a network drive.
            fDialog.Title = "Please pick your RPD file . . .";

            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                outputBox.AppendText("Converting your RPD into an XML, saving and then loading back in!" + System.Environment.NewLine);
                // set up xml document - using the filename used - load the XML
                XmlDocument thisOrder1 = new XmlDocument();
                string fN = fDialog.FileName;
                thisOrder1.Load(fN);
                int fileExtPos = fN.LastIndexOf(".");
                string nFN = fN.Substring(0, fileExtPos)+".xml";
                // make a duplicate of the file - rename it to a XML file
                System.IO.File.Copy(fN, nFN, true);
                XmlDocument thisOrder = new XmlDocument();
                // load up the new XML file.
                thisOrder.Load(nFN);

                outputBox.AppendText("Done!" + System.Environment.NewLine);

                // try and process this file
                try
                {
                    // pull in node list
                    XmlNodeList xmlnode = thisOrder.GetElementsByTagName("Sheet");
                    XmlNode jobName = thisOrder.DocumentElement.SelectSingleNode("JobName");

                    // pull jobName in from the XML
                    string jN = jobName.InnerText;
                    // replace question marks and extra spaces with nothing
                    jN = jN.Replace("?", "");
                    jN = jN.TrimEnd();
                    // outout the Job Name
                    outputBox.AppendText("Job Name > " + jN + System.Environment.NewLine);
                    // find the CR number from - this will have to be updated.
                    XmlNode crNumber = thisOrder.DocumentElement.SelectSingleNode("RadanSchedule/JobDetails/NestFolder");
                    string cR = crNumber.InnerText;
                    cR = cR.Substring(45, 8);

                    // output CR number and setup ready to show Material and amount used
                    outputBox.AppendText("CR Number > " + cR + System.Environment.NewLine + System.Environment.NewLine);
                    outputBox.AppendText("Material\t\t\t\tUsed" + System.Environment.NewLine);

                    // main routine to pull out data as needed.
                    try
                    {
                        for (int i = 1; i < xmlnode.Count; i++)
                        {
                            used = string.Format(xmlnode[i].ChildNodes.Item(11).InnerText);
                            if (Convert.ToInt32(used) >= 1)
                            {
                                material = string.Format(xmlnode[i].ChildNodes.Item(2).InnerText);
                                thickness = string.Format(xmlnode[i].ChildNodes.Item(3).InnerText);
                                sheetx = string.Format(xmlnode[i].ChildNodes.Item(5).InnerText);
                                sheety = string.Format(xmlnode[i].ChildNodes.Item(6).InnerText);

                                outputBox.AppendText(material + "-" + thickness + "-" + sheetx + "-" + sheety + "\t\t\t" + used + System.Environment.NewLine);
                            }
                        }
                    }
                    catch
                    {
                        // if the data is found to be empty - tell us
                        outputBox.AppendText("No data found!" + System.Environment.NewLine);
                    }
                }
                catch (Exception ex)
                {
                    // if there is a major problem - tell us
                    outputBox.AppendText("Problem processing new XML file!" + System.Environment.NewLine + "ERROR > " + ex + System.Environment.NewLine);
                }
            }
        }
    }
}

编辑上面的代码以显示所有代码。

谢谢。

0 个答案:

没有答案