Hi,
EDIT - refrasing my question and changing the code
I would like to change some innertext from my xml files by hardcoding it. But I'm stumbling on an error message that says that there are unreadable characters in the code. The error is thrown on the XmlDocument line.
I've also tried to add an XmlTextReader to read the selected XML file as I thought after it has been showed in the richTextbox, the memory isn't filled anymore with the content. So the button btnMakeNegative normally doesn't know what to make negative. Therefore we need to read out what's IN the richtextbox. But even there I'm getting the errormessage that strange characters were found.
Major purpose:
Click on button 1 to load an Xml file from directory X and choose the file out of a list.
Show this in a richTextBox
Click on a second button to change inner text on particular nodes
click on a third button to save the changes in a particular directory and give it the original name + "- NEG".
Thanks in advance.
EDIT 2 - Found the reason of the error but don't know how to handle it.
ok, I have found over which weird characters the code is stumbling but I can't fix it. Apparently, the xml file that I've opened with my button_click is read as a kind of HTML instead of an xml file. After initiating the XmlReader the reader can't figure out the backslashes and the backslashes+n in the file. when reading it it looks like this:
Hide Copy Code
"<?xml version=\"1.0\"?>\n<PrepaymentGridfeeDataByServiceDeliveryPoint xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"un:unece:260:data:EEM:02-02-PrepaymentGridfeeDataByServiceDeliveryPoint\">\n <HeaderBEEnergyDocument>\n <Identification>45071dc8-558d-439a-8f0a-88ae73a74910</Identification>\n <DocumentType listAgencyIdentifier=\"6\">386</DocumentType>\n <Creation>2016-06-14T12:31:58.0+01:00</Creation>\n <SenderBEEnergyParty>\n <Identification schemeAgencyIdentifier=\"9\">5414488009809</Identification>\n </SenderBEEnergyParty>\n <RecipientBEEnergyParty>\n <Identification schemeAgencyIdentifier=\"9\">0000000000000</Identification> \n </RecipientBEEnergyParty>\n <IssuerBEEnergyParty>\n <Identification schemeAgencyIdentifier=\"9\">5414488009809</Identification>\n </
Notice the backslashes? They should be in the opposite way for an xml. And why the hell are there spaces in this file? Really weird!
So a solution would be that when I open openFileDialog1 I find a way to make him read the text in XML and not in PlainText. Is there a way to do this? Or should I need to give up the openFileDialog toolbox for this issue? what to use instead then?
What I have tried:
Xml file looks like this (it's partial but the nodes are the same in the whole document: root - parentnode -childnode (- subchildnode) with or without attributes:
<?xml version="1.0"?>
<AnXMLTestFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="un:unece:260:data:EEM:AnXMLTestFile">
<HeaderBEDocument>
<Identification>45071dc8-558d-439a-8f0a-88ae73a74910</Identification>
<DocumentType listAgencyIdentifier="6">386</DocumentType>
<Creation>2016-06-14T12:31:58.0+01:00</Creation>
<SenderBEParty>
<Identification schemeAgencyIdentifier="9">1234567890123</Identification>
</SenderBEParty>
<RecipientBEParty>
<Identification schemeAgencyIdentifier="9">1234567890123</Identification>
</RecipientBEParty>
<IssuerBEParty>
<Identification schemeAgencyIdentifier="9">1234567890123</Identification>
</IssuerBEParty>
<AddresseeBEParty>
<Identification schemeAgencyIdentifier="9">1234567890123</Identification>
</AddresseeBEParty>
<DocumentStructureRevision>02-02.001</DocumentStructureRevision>
</HeaderBEDocument>
</AnXMLTestFile>
Code is as such:
public partial class frmEditXML : Form
{
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)
{
<pre>
string _input = richTextBox1.Text;
List<string> invalidChars = new List<string>();
foreach (char ch in _input)
{
if (!XmlConvert.IsXmlChar(ch))
{
invalidChars.Add(ch.ToString());
}
}
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
XmlTextReader reader = new XmlTextReader(_input);
XmlDocument doc = new XmlDocument();
doc.Load(_input);
XmlNodeList nodeList = doc.DocumentElement.SelectNodes("/AnXMLTestFile/");
foreach (XmlNode xNode in doc)
{
if (xNode.ParentNode.Attributes["HeaderBEDocument"].Value != "")
{
doc.SelectSingleNode("Identification").InnerText = "string";
doc.SelectSingleNode("SCI").InnerText = "string";
doc.SelectSingleNode("ReferenceType").InnerText = "string";
doc.SelectSingleNode("CCType").InnerText = "AA";
}
else { }
}
}
private void btnSavechanges_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);
}
}