通过网络访问XML文件

时间:2017-03-08 20:41:02

标签: c# xml winforms

我已经在这个管理应用程序上工作了几个星期,而且它已经差不多完成了。当用户单击保存按钮时,应该发生什么,调用VerifyFields()方法。之后,它应该询问用户是否要保存并发送电子邮件。用户单击“是”后,应该向XML文件中的电子邮件地址发送电子邮件。我收到错误消息"找不到文件:DREmailAddresses.xml:\ fs01 \ Applications \ EMS-Manager。"但是,该文件存在且可访问。我已经复制了下面的相关代码以及image of the error.

// Begin Email Section
    public bool VerifyFields()
    {
        var status = false;
        switch (tbxDynPartNumber.Text)
        {
            case "":
                MessageBox.Show(@"Enter Dynalab Part#", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                status = true;
                break;
        }
        switch (tbxSupplier.Text)
        {
            case "":
                MessageBox.Show(@"Enter Supplier Name", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                status = true;
                break;
        }
        switch (tbxInitiatedBy.Text)
        {
            case "":
                MessageBox.Show(@"Enter Initiator Name", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                status = true;
                break;
        }
        switch (orderQty.Text)
        {
            case "0":
                MessageBox.Show(@"Order Quantity Must Be Greater Than 0!", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                status = true;
                break;
        }

        switch (dtpDate.Text)
        {
            case "":
                MessageBox.Show(@"Enter A Valid Date.", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                status = true;
                break;
        }
        switch (tbxDescription.Text)
        {
            case "":
                MessageBox.Show(@"Enter Description", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                status = true;
                break;
        }

        switch (tbxLocation.Text)
        {
            case "":
                MessageBox.Show(@"Enter Location", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                status = true;
                break;
        }
        switch (tbxDiscrepancyDescription.Text)
        {
            case "":
                MessageBox.Show(@"Enter Discrepancy Description", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                status = true;
                break;
        }
        return status;
    }

    public void PrepareEmail(string subject, string message)
    {
        if (MessageBox.Show(@"Are you sure you want to save and send Discrepancy Report: " +tbxDRNumber.Text + @"?\n Click YES to save\n Click NO to cancel" , @"Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {

            SendEmail(subject,message);
        }
    }
    public Array AddEmail()
    {
        string[] dRemail = { "", "", "" };
        if (File.Exists(@"\\fs01\Applications\EMS-Manager\DREmailAddresses.xml"))
        {
            XmlReader emailDocument = new XmlTextReader(@"\\fs01\Applications\EMS-Manager\DREmailAddresses.xml");
            while (emailDocument.Read())
            {
                var type = emailDocument.NodeType;
                switch (type)
                {
                    case XmlNodeType.Element:
                        if (emailDocument.Name == "DRCreatedAddEmail")
                        {
                            dRemail[0] = emailDocument.ReadInnerXml();
                        }
                        if (emailDocument.Name == "DRActionNeededAddEmail")
                        {
                            dRemail[1] = emailDocument.ReadInnerXml();
                        }
                        if (emailDocument.Name == "DRPendingAddEmail")
                        {
                            dRemail[2] = emailDocument.ReadInnerXml();
                        }
                        else
                        {
                            MessageBox.Show(
                                @"The file: 'DREmailAddresses.xml' was not found at: \\fs01\Applications\EMS-Manager");
                        }
                        break;
                }
            }
        }
        return dRemail;
    }

1 个答案:

答案 0 :(得分:1)

如果您收到该错误消息,那么您的代码将超过File.Exists的检查并超过文档的读取,但只需点击" if的else分支(if)(emailDocument.Name == " DRPendingAddemail&#34)"检查。

所以你的功能应该接近这个

public Array AddEmail()
{
    string[] dRemail = { "", "", "" };
    if (File.Exists(@"\\fs01\Applications\EMS-Manager\DREmailAddresses.xml"))
    {
        XmlReader emailDocument = new XmlTextReader(@"\\fs01\Applications\EMS-Manager\DREmailAddresses.xml");
        while (emailDocument.Read())
        {
            var type = emailDocument.NodeType;
            switch (type)
            {
                case XmlNodeType.Element:
                    if (emailDocument.Name == "DRCreatedAddEmail")
                    {
                        dRemail[0] = emailDocument.ReadInnerXml();
                    }
                    else if (emailDocument.Name == "DRActionNeededAddEmail")
                    {
                        dRemail[1] = emailDocument.ReadInnerXml();
                    }
                    else if (emailDocument.Name == "DRPendingAddEmail")
                    {
                        dRemail[2] = emailDocument.ReadInnerXml();
                    }
                    else
                    {
                        MessageBox.Show("Unknown node type " + emailDocument.Name);
                    }
                    break;
            }
        }
    }
    else
    {
         MessageBox.Show(@"The file: 'DREmailAddresses.xml' was not found at: \\fs01\Applications\EMS-Manager");
    }
    return dRemail;
}