mscorlib.dll附加信息中出现未处理的“system.argumentexception”类型异常:路径中的非法字符。 [英] An unhandled exception of type 'system.argumentexception' occurred in mscorlib.dll additional information: illegal characters in path.

查看:194
本文介绍了mscorlib.dll附加信息中出现未处理的“system.argumentexception”类型异常:路径中的非法字符。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想要做的是更改XML文件的innertext。所以我点击了文本框中加载的选择文件按钮。我点击我的按钮'更改文件'来更改一些节点内容。然后我点击我的保存按钮将更改保存在所需位置。但是当我调试代码时它会在线给我错误信息(如标题中所示)

Hi,

What I want to do is to change the innertext of an XML file. So I'm clicking on my button 'Select File' that is loaded in a textbox. I click on my button 'change file' to change some node contents. Then I click on my 'save button' to save the changes in a desired location. But when I debug the code it's giving me the error message (as in title) on line
"

doc.Load(selectedFile);



文件被正确读取,因为当我将鼠标悬停在'selectedFile'上时,我会看到内容,但'doc。'正在给出问题。

1)有人可以告诉我是什么我需要做什么才能解决这个问题?

2)有人可以告诉我,btnMakeNegative_Click中foreach循环中的代码是否会给出结果?



先谢谢。



我尝试过:



"
the file is read correctly because when I hover over 'selectedFile' I see the content, but 'doc.' is giving the issue.
1) Could someone tell me what I need to do to solve this problem please?
2) Can somebody tell me if the code in the foreach loop in " btnMakeNegative_Click " will give a result?

Thanks in advance.

What I have tried:

public partial class frmEditXML : Form
   {
       //get files from directory
       string[] path = Directory.GetFiles(@"C:\Users\decraiec\Documents\A_Automated", "*.XML");

       public frmEditXML()
       {
           InitializeComponent();
       }

       private void btnSelectFile_Click(object sender, EventArgs e)
       {
           openFileDialog1.Filter = "xml files|*.xml|All files|*.*";
           if (openFileDialog1.ShowDialog() == DialogResult.OK)
           {
               try
               {
                   richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
               }
               catch (Exception exc)
               {
                   MessageBox.Show("An error occured: " + System.Environment.NewLine + exc.ToString() + System.Environment.NewLine);
                   throw;
               }
           }
       }

       private void btnMakeNegative_Click(object sender, EventArgs e)
       {
           //Read selected file
           string selectedFile = richTextBox1.Text;
           XmlDocument doc = new XmlDocument();
           doc.Load(selectedFile); // --> error message
           //find the node to change the content
           //if node is not present, do nothing
           foreach (XmlNode xNode in doc)
           {
               if (xNode.ParentNode.Attributes["*.*"].Value != "")
               {
                   doc.SelectSingleNode("Identification").InnerText = "string";
                   doc.SelectSingleNode("SCI").InnerText = "string";
                   doc.SelectSingleNode("ReferenceType").InnerText = "string";
                   doc.SelectSingleNode("CCType").InnerText = "AA";
               }
               else { } //do nothing
           }

       }

       private void btnSavechanges_Click(object sender, EventArgs e)
       {
           if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);
           }
       }

       private void btnConvertToXML_Click(object sender, EventArgs e)
       {
           Form2 frm2 = new Form2();
           frm2.Show();

       }

       private void frmOpenRead_Load(object sender, EventArgs e)
       {
           Application.Exit();
       }


   }

推荐答案

string selectedFile = richTextBox1.Text;
XmlDocument doc = new XmlDocument();
doc.Load(selectedFile); // --> error message

您从RichText控件获取文件名,其中包含无效的文件名。



我不喜欢知道您要使用哪个文件名,但看起来您需要将选定的文件名存储在某个地方而不是加载到RichText框中的文件内容。

You are getting the file name from the RichText control and that contains an invalid file name.

I don't know which file name you want to use but it looks like you need to store the selected file name somewhere to use that instead of the file content loaded into the RichText box.


doc.Load(selectedFile); 





加载用于从文件加载XML。如果你想在一个变量中使用xml(这就是你所拥有的),那么请使用LoadXml





Load is used to load XML from a file. If you have xml in a variable you want to then use (which is what you have) then use LoadXml instead

doc.LoadXml(selectedFile); 


当您将鼠标悬停在 selectedFile 上并查看其内容时,并不意味着内容是 XmlDocument.load 方法的有效路径。

使用调试器来查看引发的exeception的详细信息,您应该能够在这样的路径中发现问题(另请参阅文档 [ ^ ])。
When you hover over selectedFile and see its content, it doesn't mean the content is a valid path for the XmlDocument.load method.
Using the debugger in order to see the detail of the exeception raised, you should be able to spot the problem in such a path (see also the documentation[^]).


这篇关于mscorlib.dll附加信息中出现未处理的“system.argumentexception”类型异常:路径中的非法字符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆