Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
"
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();
       }


   }
Posted
Updated 16-Feb-17 4:23am
v3
Comments
njammy 16-Feb-17 4:58am    
Please kindly post the xml content being loaded.

C#
string selectedFile = richTextBox1.Text;
XmlDocument doc = new XmlDocument();
doc.Load(selectedFile); // --> error message
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.
 
Share this answer
 
v2
Comments
AAB40 16-Feb-17 5:07am    
hello Jochen, I need the content of the xml file, not the title of the xml file nor the header. It's about reading the content of the file that I have selected and change some content in it. How do you figure out that it's the file name from the RichtText control that I'm getting please? Because I don't see it anywhere in the error message.???
Jochen Arndt 16-Feb-17 5:15am    
See https://msdn.microsoft.com/en-us/library/875kz807(v=vs.110).aspx:

XmlDocument.Load Method (String)
Loads the XML document from the specified URL.

The string passed to the Load() method must be a valid URL / file name.
You are passing the content of the file which has been loaded into the RichText box:
richTextBox1.LoadFile(openFileDialog1.FileName, richTextBoxStreamType.PlainText);

If you want to read the file content as XmlDocument store the file name and use that:

// string selectedFile is a member
selectedFile = openFileDialog1.FileName;
AAB40 16-Feb-17 6:00am    
ah OK, thanks. Now, still tumbling over doc.LoadXml(selectedFile); but this time it's saying that the first line of my XML isn't right if I understand it correctly: "Data at the root level is invalid. Line 1, position 1." My xml file starts with "<?xml version="1.0"?>". I remember reading this somewhere but can't find it anymore. do you have an idea please?
Jochen Arndt 16-Feb-17 6:18am    
With XML version 1.0 that header line is recommended but optional.
With XML version 1.1 the version header is required.

For testing I suggest to use my solution reading from file. This ensures that the content is not modified when reading the content into the RichText box.

I'm not quite sure how the RichText behaves when reading plain text files. But a normal edit control would convert the text to Unicode (wide string) which is probably not understood by the XML reader when not inserting a BOM. If you don't know what I'm talking about, just try direct loading from file. If that works, I was right and we/you must find a solution for that problem. The solution depends on what you finally want to do.
AAB40 16-Feb-17 6:22am    
Jochen, I already did that. That's where I got the error message about the first line:

//Read selected file
string selectedFile = openFileDialog1.FileName;
XmlDocument doc = new XmlDocument();
doc.LoadXml(selectedFile);
//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("ServiceComponentIdentification").InnerText = "string";
doc.SelectSingleNode("ReferenceType").InnerText = "string";
doc.SelectSingleNode("CostCategoryType").InnerText = "AA";
}
else { } //do nothing
}
doc.Load(selectedFile); 


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); 
 
Share this answer
 
Comments
AAB40 16-Feb-17 5:40am    
Hi F-ES, thanks for your solution that works to pass that line of code but now c sharp is telling me that I should use 'new' for the XmlNode. FYI: I have changed my code a bit meanwhile.


doc.LoadXml(selectedFile);
//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("ServiceComponentIdentification").InnerText = "string";
doc.SelectSingleNode("ReferenceType").InnerText = "string";
doc.SelectSingleNode("CostCategoryType").InnerText = "AA";
}
else { } //do nothing
}
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[^]).
 
Share this answer
 
Comments
AAB40 16-Feb-17 5:55am    
thank you CPallini
closing this issue and opened another one. The problem lies with the XML Read.

How to read XML text from a richttextbox to change innertext?[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900