Xml file, replacing numeric tag names with a valid

2019-09-20 15:10发布

问题:

i have a large xml file that couldn't be validated, because it contains some numeric tag names, here is how it looks:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
<0>
    <id>545f7bf982efa41a496379d2</id>
    <org_id>1</org_id>
    <status>open</status>
    <name>Carrefour</name>
    <address>1175 rue Guillaume Du Vair</address>
    <created>1415543801.5059</created>
</0>
<1>
    <id>545f7bf982efa41a496379d4</id>
    <org_id>1</org_id>
    <status>open</status>
    <name>Carrefour Angers Grand Maine</name>
    <address>rue Du Grand Launay</address>
    <created>1415543801.5146</created>
</1>
<2>
    <id>545f7bf982efa41a496379d6</id>
    <org_id>1</org_id>
    <status>open</status>
    <name>Carrefour Angers Saint Serge</name>
    <address>boulevard Gaston Ramon</address>
    <created>1415543801.523</created>
</2>
<3>
    <id>545f7bf982efa41a496379d8</id>
    <org_id>1</org_id>
    <status>open</status>
    <name>Carrefour Anglet Bab2</name>
    <address>avenue Jean Léon Laporte</address>
    <created>1415543801.5302</created>
</3>
<4>
    <id>545f7bf982efa41a496379da</id>
    <org_id>1</org_id>
    <status>open</status>
    <name>Carrefour Angoulins</name>
    <address>route De Rochefort</address>
    <created>1415543801.5385</created>
</4>
<5>
    <id>545f7bf982efa41a496379dc</id>
    <org_id>1</org_id>
    <status>open</status>
    <name>Carrefour Annecy</name>
    <address>134 avenue De Genève</address>
    <created>1415543801.5458</created>
</5>
.
.
.
</root>

What i am trying to do is to replace automatically each numeric tag with a valid one, for example: replace

Replace <0>...</0> with <A0>...</A0>

my file contains thousands of these, is there any tool i can use to do it automatically .

回答1:

I managed to that that with regular expression on notepad++, it wasn't that hard,

regex



回答2:

Any decent text editor that supports regex search-replace feature:

search: <(/?)(\d+)>
replace: <\1A\2>

This should replace all tags, whether opening or closing in your XML.



回答3:

I added xyz before each numeric tag

        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string input = File.ReadAllText(FILENAME);

            string pattern = @"(?'open_tag'</?)(?'numbers'\d+)>";

            MatchCollection matches = Regex.Matches(input, pattern);
            input = Regex.Replace(input, pattern, "${open_tag}" + "xyz" + "${numbers}" + ">");

        }​