Home > other >  XML edit replace/remove
XML edit replace/remove

Time:10-27

I have an XML document I create with some data, it is always created in the same way, with the same amount of data, as seen in the following example:

<cac:Party>
  <cbc:EndpointID schemeID="Country">3023</cbc:EndpointID>
  <cac:PartyIdentification>
    <cbc:ID schemeID="Country">3023</cbc:ID>
  </cac:PartyIdentification>
  <cac:PartyName>
    <cbc:Name>Test</cbc:Name>
  </cac:PartyName>
  <cac:PostalAddress>
    <cbc:AddressFormatCode listAgencyID="###" listID="urn:oioubl:codelist:addressformatcode-1.1">StructuredLax</cbc:AddressFormatCode>
    <cbc:StreetName>Test</cbc:StreetName>
    <cbc:CityName>Test</cbc:CityName>
    <cbc:PostalZone>Test</cbc:PostalZone>
    <cac:Country>
      <cbc:IdentificationCode>DK</cbc:IdentificationCode>
    </cac:Country>
  </cac:PostalAddress>
  <cac:PartyTaxScheme>
    <cbc:CompanyID schemeID="Country">3023</cbc:CompanyID>
    <cac:TaxScheme>
      <cbc:ID schemeID="urn:oioubl:id:taxschemeid-1.1" schemeAgencyID="320">63</cbc:ID>
      <cbc:Name>Moms</cbc:Name>
    </cac:TaxScheme>
  </cac:PartyTaxScheme>
  <cac:PartyLegalEntity>
    <cbc:RegistrationName>Test/cbc:RegistrationName>
    <cbc:CompanyID schemeID="Country">3023</cbc:CompanyID>

Now what I want is to edit the value: "3023" to "2020" (strings) but I only want to do it for the last one.

So far I've been trying to use "StreamReader StreamWriter Regex" as follows:

                        string docText = null;
                        using (StreamReader sr = new StreamReader(filename))
                        {
                            docText = sr.ReadToEnd();
                        }

                        Regex oldValue = new Regex("3023");
                        docText = oldValue.Replace(docText, "2020");

                        using (StreamWriter sw = new StreamWriter(filename))
                        {
                            sw.Write(docText);
                        }

The obvious issue I run into here is that it changes it all the way through the document, so my thought was to maybe use substrings and find the specific index of the last "3023". So because the document is always build the same way with the same amount of values, so I know the "3023" string I want is always the fourth one in the document, so I can find the starting index by using:

docText.IndexOf("3023", 4);

this gives me the starting index, I also know the number sequence is four letters "3023" this will always be the case, so therefore I can also find the ending index.

This is where I stall though, I am unsure what to make use of to then replace the string using indexes, there probably is a smarter method aswell, so I will happily take any pointers.

Thanks.

CodePudding user response:

Use Xml Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication40
{
    class Program
    {
        const string INPUT_FILENAME = @"c:\temp\test.xml";
        const string OUTPUT_FILENAME = @"c:\temp\test1.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(INPUT_FILENAME);
            XElement CompanyID = doc.Descendants().Where(x => x.Name.LocalName == "CompanyID").Last();
            CompanyID.Value = "2020";
            doc.Save(OUTPUT_FILENAME);
        }
    }
 
}
  • Related