Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Why is anything that I try to read out the XML file is falling over the root element?

<?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:02-02-AnXMLTestFile">

/* Using XmlReader, XmlDocument, BOM, MemoryStreamer/Writer, ...
Even if I leave out the method 'Encoding GetEncoding()' I still have the 'illegal characters in path' on codeline
XmlTextReader reader = new XmlTextReader(doc.ToString());

*/
Improvement:
cleaned up some code just to use the basics. changed the string to use
string xml = richTextBox1.Text;


Now the code doesn't loop over my nodeList.

Can somebody help me please? Thanks in advance.

What I have tried:

XML-file example
<?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:02-02-AnXMLTestFile">
  <HeaderBEDocument>
    <Identification>45071dc8-558d-439a-8f0a-88ae73a74910</Identification>
    <DocumentType listAgencyIdentifier="6">386</DocumentType>
    <Creation>2016-06-14T12:31:58.0+01:00</Creation>
    <SenderBEEnergyParty>
      <Identification schemeAgencyIdentifier="9">5414488009809</Identification>
    </SenderBEEnergyParty>
    <RecipientBEEnergyParty>
      <Identification schemeAgencyIdentifier="9">0000000000000</Identification>  
    </RecipientBEEnergyParty>
    <IssuerBEEnergyParty>
      <Identification schemeAgencyIdentifier="9">5414488009809</Identification>
    </IssuerBEEnergyParty>
    <AddresseeBEEnergyParty>
      <Identification schemeAgencyIdentifier="9">0000000000000</Identification>
    </AddresseeBEEnergyParty>
    <DocumentStructureRevision>02-02.001</DocumentStructureRevision>
  </HeaderBEDocument>

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            // Create an OpenFileDialog object.
            OpenFileDialog openFile1 = new OpenFileDialog();

            // Initialize the filter to look for text files.
            openFile1.Filter = "Xml Files|*.xml";

            // If the user selected a file, load its contents into the RichTextBox. 
            if (openFile1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);
            }
        }
 private void btnMakeNegative_Click(object sender, EventArgs e)
        {
            // Load XML
            var doc = new XmlDocument();
            string xml = richTextBox1.Text;
            doc.LoadXml(xml);

            // Define Default namespace and add prefix to use in Xpath (Mandatory!)
            var nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace("x", "un:unece:260:data:EEM:AnXMLTestFile");

            // Selecting All HeaderBEDocument nodes
            var nodeList = doc.DocumentElement.SelectNodes("//x:HeaderBEDocument", nsmgr);

            // Loop all HeaderBEDocument nodes
            foreach (XmlNode node in nodeList)
            {
                // Select Identification node under HeaderBEDocument node
                var identificationNode = node.SelectSingleNode("x:Identification", nsmgr);
                if (identificationNode != null)
                {
                    // Change value of indentification node to string
                    identificationNode.InnerText = "string";
                }
                // Select SCI node under HeaderBEDocument node
                var SCI = node.SelectSingleNode("x:SCI", nsmgr);
                if (SCI != null)
                {
                    // change value of SCI node to string
                    SCI.InnerText = "56987465";
                }
                // Select ReferenceType node under HeaderBEDocument node
                var ReferenceType = node.SelectSingleNode("x:ReferenceType", nsmgr);
                if (ReferenceType != null)
                {
                    // change value of ReferenceType node to string
                    ReferenceType.InnerText = "AA";
                }
                // Select CCType node under HeaderBEDocument node
                var CCType = node.SelectSingleNode("x:CCType", nsmgr);
                if (CCType != null)
                {
                    // change value of CCType node to string
                    CCType.InnerText = "string";
                }
            }

            // Print new XML in richtextbox
           richTextBox1.Text = nodeList.ToString();
        }

}
Posted
Updated 28-Feb-17 2:09am
v5
Comments
Richard Deeming 28-Feb-17 10:05am    
15 days and five questions later, and I'm starting to think there's an echo in here!

A string containing XML is NOT a valid file path. They are two completely different things. You can't pass one to a method that's expecting the other.

It's like the difference between diesel and unleaded - they're both liquids, but if you put the wrong one into your car, it's not going to end well!

If you genuinely don't understand what we're saying, please let us know. Don't just keep reposting what is essentially the same question over and over again.

Isn't this similar to your question An unhandled exception of type 'system.argumentexception' occurred in mscorlib.dll additional information: illegal characters in path.[^]?

The XmlTextReader Constructor (String) (System.Xml)[^] expects a string containing a valid file name but you are passing doc.ToString() which is probably not a file name but some file content.
 
Share this answer
 
Comments
AAB40 28-Feb-17 7:29am    
hello Jochen, yes it is indeed the content of a richtextbox that needs to be read and that should be an xml file. I can't add a string name of the file as it is already in the richtextbox. :-( So actually I'm turning arround not finding any solution to what I want to do.?
Jochen Arndt 28-Feb-17 7:47am    
A XmlTextReader is - as the name indicates - a class to read from files or streams.

If you have a richtext box containing XML content, define what you want to do with that and choose an appropriate class like XmlDocument as used in your previous question.
AAB40 28-Feb-17 7:45am    
Improved the code.
Jochen Arndt 28-Feb-17 7:58am    
Please don't edit your questions this way.

The initial question "illegal characters in path" has been answered by me and Griff. That is, it is answered and should be considered closed.

Modifying questions by adding not related content and removing related content will break the whole context.

Just close this and start a new one. But I suggest to think first about what you want to do. That is: Define the requirements.
AAB40 28-Feb-17 8:27am    
Jochen, I think it is time that all persons on CodeProject talk to each other to find a good solution on what to do and not to do. In the previous thread I was asked by Karthik to update the issue if I changed something. Not to each time open a new thread that looks like a repost, and they all will look like a repost. Because it's been 5 years that I have used C sharp and I was never a programmer, so everything that I will ask will look at each other. But trying.... Thanks for your time!
That isn't supposed to be XML - it's a file name and the path to the file, not the XML file content.
It isn;t XML that's complaining, it's the file system telling you that come of the characters in your file path are not allowed - and that implies that instead of passing the file name to your method, you are passing an XML string.

Use the debugger, check the value of the parameter you pass and then try to work out why you have passed the wrong thing to the function.
 
Share this answer
 
1) to pass the NodeList loop see through it that the namespace that you add is complete and the same as is in the file. Don't cut of.
2) Make sure you don't use one of the existing IEnumerables for XML.
- " "
- "xmlns"
- "xml"
add a specified one like I did, named "x". It will be added to the IEnumerable list.

Code is not working completely but we've passed the nodeList this time thanks to Jochen.
// Load XML
           var doc = new XmlDocument();
           string xml = richTextBox1.Text;
           doc.LoadXml(xml);

           XmlTextReader reader = new XmlTextReader(new StringReader(xml));
           System.Data.DataSet ds = new System.Data.DataSet();
           ds.ReadXml(reader, System.Data.XmlReadMode.Auto);

           // Define Default namespace and add prefix to use in Xpath (Mandatory!)
           var nsmgr = new XmlNamespaceManager(doc.NameTable);
           nsmgr.AddNamespace("x", "un:unece:260:data:EEM:02-02-AnXMLTestFile");

           // Selecting All HeaderBEDocument nodes
           var nodeList = doc.DocumentElement.SelectNodes("//x:HeaderBEDocument", nsmgr);

           // Loop all HeaderBEDocument nodes
           foreach (XmlNode node in nodeList)
           {
               // Select Identification node under HeaderBEDocument node
               var identificationNode = node.SelectSingleNode("x:Identification", nsmgr);
               if (identificationNode != null)
               {
                   // Change value of indentification node to string
                   identificationNode.InnerText = "string";
               }
               // Select SCI node under HeaderBEDocument node
               var SCI = node.SelectSingleNode("x:SCI", nsmgr);
               if (SCI != null)
               {
                   // change value of SCI node to string
                   SCI.InnerText = "56987465";
               }
               // Select ReferenceType node under HeaderBEDocument node
               var ReferenceType = node.SelectSingleNode("x:ReferenceType", nsmgr);
               if (ReferenceType != null)
               {
                   // change value of ReferenceType node to string
                   ReferenceType.InnerText = "AA";
               }
               // Select CCType node under HeaderBEDocument node
               var CCType = node.SelectSingleNode("x:CCType", nsmgr);
               if (CCType != null)
               {
                   // change value of CCType node to string
                   CCType.InnerText = "string";
               }
           }

           // Print new XML in richtextbox
          richTextBox1.Text = nodeList.ToString();
       }
 
Share this answer
 
v7

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