Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to read the XML file with XmlDocument and using XPath to get to my childnodes. I've added a NamespaceManager and pointed to my parentNode. The purpose is to find childnodes with specific names and change the innertext whether or not the childnode has an attribute. Also it needs to be that specific word in the childnode, not a part of a word. How could I do this please?

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>
  </HeaderBEDocumtent>
  <PayloadBEInvoiceEvent>
    <Identification>45071dc8-558d-439a-8f0a-88ae73a74910</Identification>
    <Function listAgencyIdentifier="6">9</Function>
    <CalculationDateOccurrence>2016-06-14T12:31:58.0+01:00</CalculationDateOccurrence>
    <ConsumptionMonthOccurrence>--04</ConsumptionMonthOccurrence>
    <ConsumptionYearOccurrence>2016</ConsumptionYearOccurrence>
    <SDPUsedServiceDeliveryPointLocation>
      <HeadpointIdentificationDomainLocation>
        <Identification schemeAgencyIdentifier="9">941449900000000028</Identification>
      </HeadpointIdentificationDomainLocation>
      <SCI schemeAgencyIdentifier="86">SC_COMMINJ</SCI>
    </SDPUsedServiceDeliveryPointLocation>
  </PayloadBEInvoiceEvent>


The 'Identification' in bold should change of innertext.

On debugging I get the 'illegal characters were found' error. If I leave out the first part of my root node no error message are shown anymore and the innertext changes as it should. How can I fix this please?

What I have tried:

characters left out from the root:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"


Innertext filling up on debugging. But I need those URL's! They are in all 150 Xml-files that I want to change.
Posted
Updated 1-Mar-17 23:29pm
v2
Comments
Richard MacCutchan 1-Mar-17 12:08pm    
What is the exact error message, and which line does it refer to?

You can use XmlTextReader to get to all elements in a sequential way.
More information: XmlTextReader.Read Method (System.Xml)[^]
The XmlTextReader - A Beginner's Guide[^]
 
Share this answer
 
Solved: Foreach-loop containes the different nodes I need to change innertext from. In each " var XXXNode = node.SelectSingleNode("x:XXX", nsmgr); " You need to add "//" before the "x". Like this

// Select Identification node from all nodes
                var identificationNode = node.SelectSingleNode("//x:Identification", nsmgr); //<==
                if (identificationNode != null)
                {
                    // Change value of indentification node to string
                    identificationNode.InnerText = "string";
                }
// Select SCI node from all parentnodes
                var SCI = node.SelectSingleNode("//x:SCI", nsmgr);//<==
                if (SCI != null)
                {
                    // change value of SCI node to string
                    SCI.InnerText = "56987465";
                }
                // Select ReferenceType node from all nodes
                var ReferenceType = node.SelectSingleNode("//x:ReferenceType", nsmgr);//<==
                if (ReferenceType != null)
                {
                    // change value of ReferenceType node to string
                    ReferenceType.InnerText = "AA";
                }
                // Select CCType node from all nodes
                var CCType = node.SelectSingleNode("//x:CCType", nsmgr);//<==
                if (CCType != null)
                {
                    // change value of CCType node to string
                    CCType.InnerText = "string";
                }

Also use the "root.OuterXml" to show the changes in the object of VS. Here:
richTextBox1.Text = root.OuterXml;
 
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