Link to home
Start Free TrialLog in
Avatar of srini t
srini tFlag for United States of America

asked on

Need help on modify / update the xml tag by using the code (C#)

Hi Experts,

I need help on the following requirement.

We have around 500 + xmls need to modify by using any .net code.
The highlighted content we need to remove for 500 + xml's

Here is the sample xml (error one):

<?xml version="1.0" encoding="utf-8"?>
<ImportSession ErrorCode="19" ErrorMessage="Illegal file name (\\Localhost\CaptureSV\XML_Import\Test1.TIF)">
  <Batches>
    <Batch BatchClassName="TEST_BC" Priority="5" Processed="1">
      <Documents>
        <Document FormTypeName="FT_TEST">
          <IndexFields>
            <IndexField Name="BarcodeNumber" Value="1234">
            </IndexField>
          </IndexFields>
          <Pages>
            <Page ImportFileName="\\Localhost\CaptureSV\XML_Import\Test1.TIF" ErrorCode="19" ErrorMessage="Illegal file name (\\Localhost\CaptureSV\XML_Import\Test1.TIF)">
            </Page>
          </Pages>
        </Document>
      </Documents>
    </Batch>
  </Batches>
</ImportSession>

Here is the xml ( After manually removed):

<?xml version="1.0" encoding="utf-8"?>
<ImportSession>
  <Batches>
    <Batch BatchClassName="Test_BC" Priority="5">
      <Documents>
        <Document FormTypeName="FT_TEST">
          <IndexFields>
            <IndexField Name="BarcodeNumber" Value="1234" />
          </IndexFields>
          <Pages>
            <Page ImportFileName="\\Localhost\CaptureSV\XML_Import\Test1.TIF"/>
          </Pages>
        </Document>
      </Documents>
    </Batch>
  </Batches>
</ImportSession>

Please let me know if any quetstions.

Regards,
 Srini
Avatar of HainKurt
HainKurt
Flag of Canada image

what do you want to remove?
all ErrorCode and ErrorMessage from everywhere?
or just ErrorCode=19
or just ErrorCode and ErrorMessage from Page  and ImportSession  
Avatar of srini t

ASKER

Hi,
Thank you for your response.

I just need to remove the highlighted content from the xml.
Error code and Error Message from  Page  and ImportSession   
and  Processed="1"  from the Batch BatchClassName.

Please let me know any questions.

Regards,
Srini

have a look at here

https://dotnetfiddle.net/YfB69l
      XmlNodeList xnList = xml.SelectNodes("//*[@ErrorCode]");
      foreach (XmlNode xn in xnList)
      {
        Console.WriteLine(xn.Attributes["ErrorCode"].Value);
        xn.Attributes.Remove(xn.Attributes["ErrorCode"]);
        Console.WriteLine(xn.Attributes["ErrorMessage"].Value);
        xn.Attributes.Remove(xn.Attributes["ErrorMessage"]);
      }
      
      Console.WriteLine(xml.OuterXml);

Open in new window

it deletes all ErrorCode and ErrorMessage from all nodes...
Avatar of srini t

ASKER

Error XML's are one NAS Location.
In the code we need to take from that location and modify the xml and save it in other NAS  location.

Please let me know any questions. 

Regards,
Srini

Avatar of srini t

ASKER

Hain Kurt,
We have 500 + error xml's in one of the NAS Location,
In the code - we need to load each xml and modify then save in to another location.

Please let me know if any questions.

Regards,
Srini
this code should check Processed, if it is 1, then delete ErrorCode and ErrorMessage from other 2 nodes...

      XmlNodeList xnList = xml.SelectNodes("//ImportSession");
      foreach (XmlNode xn in xnList)
      {
         XmlNode xnB, xnIFN;
         xnB = xn.SelectSingleNode("Batches/Batch");
         xnIFN = xnB.SelectSingleNode("Documents/Document/Pages/Page");

         if (xnB.Attributes["Processed"].Value=="1"){
              xn.Attributes.Remove(xn.Attributes["ErrorCode"]);
              xn.Attributes.Remove(xn.Attributes["ErrorMessage"]);

              xnIFN.Attributes.Remove(xn.Attributes["ErrorCode"]);
              xnIFN.Attributes.Remove(xn.Attributes["ErrorMessage"]);
         }
      }

Open in new window

https://dotnetfiddle.net/oZf6BA
Avatar of srini t

ASKER

Hi Hainkurt,

Can you please add the code for Load xml and move the updated xml to other NAS Location?

Regards,
Srini


if you click the link, you will see how xml is loaded...
do you have 500+ xml file, or you have one xml with 500+ ImportSession or Batches
and what does move to another NAS?

I hardcoded the xml, but you will use

xml.Load(XMLPathFile);

and you will use

xml.Save(XMLNewPathFile);

to save the result to somewhere else...

Avatar of srini t

ASKER

I have 500+ xml files. I need to update all these 500+ xml's. So I want to create one  C# file for automate this process..
above is for one file...
make it a sub, create a loop, for each file call this sub, passing input/output file

Avatar of srini t

ASKER

I am new to C# coding - Could you please share me the complete code? Thank you.
Could you please share me the complete code?

I cannot...
I dont know how and where the files are
I dont know what the naming conventions for files are
I dont know where to save the files...
Avatar of srini t

ASKER

Can we use the temp location.
C:\Temp
C:\Temp\Updated_Xml

File Name : Test.xml

Basically I need the template of the code - I can update the file location

ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srini t

ASKER

Thank you HainKart.. It's worked