C# Xml XDocument - 需要将相关的重复节点名称转换为唯一的

C# Xml XDocument - Need to convert related duplcate Node Names to be Unique

首先我是 xml 的新手。已经阅读了很多关于 XmlDocument 和 XDocument 的内容。需要一些帮助来整理以下功能。

我有理由需要所有重复项(相同级别、相同祖先/parents 是唯一的,无论 xml 结构有多远。

在下面的示例文件中...在位置 11 和 12 处有两个名为 "entry" 的节点名称具有相同的嵌套 parents(即 result/products 或祖先().count() == 2) 稍后在此示例文件中...还有另一个名为 "entry" 的节点序列,但是它们的 parent 节点是 result/forms/required/。 (或祖先()。计数()== 3)

有人可以帮我用递归代码找到 parent 匹配的匹配节点并附加下划线后跟数字 "entry_1, entry _2. ... entry_99" 并将 xml 写回到一个文件。

要特别清楚,作为同名兄弟节点的每个节点都应从 _1 开始,除非 parent 匹配具有现有附加“_Number”的兄弟节点。

感谢您的帮助、建议和想法!!

示例文件:

<?xml version="1.0" encoding="UTF-8"?>
  <result>
  <quote><![CDATA[1776.5]]></quote>
  <account_name><![CDATA[Acme Mountaineers]]></account_name>
  <account_number><![CDATA[CDTEST-07052016]]></account_number>
  <effective_dt><![CDATA[1467676800]]></effective_dt>
  <effective_dt_string><![CDATA[2016-07-05T00:00:00+00:00]]></effective_dt_string>
  <zip><![CDATA[25301]]></zip>
  <street><![CDATA[170 SE 3rd Ave]]></street>
  <city><![CDATA[CHARLESTON]]></city>
  <state><![CDATA[WV]]></state>
  <products>
    <entry><![CDATA[gl]]></entry>
    <entry><![CDATA[auto]]></entry>
  </products>
  <coverage_type><![CDATA[Occurrence]]></coverage_type>
  <product_type><![CDATA[Excess]]></product_type>
  <admitted_status><![CDATA[Non Admitted]]></admitted_status>
  <classifications>
    <entry>
      <location><![CDATA[1]]></location>
      <street><![CDATA[170 SE 3rd Ave]]></street>
      <city><![CDATA[CHARLESTON]]></city>
      <county><![CDATA[KANAWHA]]></county>
      <state><![CDATA[WV]]></state>
      <territory><![CDATA[1]]></territory>
      <zip><![CDATA[25301]]></zip>
      <class_code><![CDATA[91582]]></class_code>
      <class_code_desc><![CDATA[(91582) Contractors - subcontracted work - in connection with building construction, reconstruction,]]></class_code_desc>
      <premium_basis><![CDATA[Total Cost]]></premium_basis>
      <premium_basis_amount><![CDATA[200000]]></premium_basis_amount>
      <authority><![CDATA[A]]></authority>
    </entry>
  </classifications>
  <limit><![CDATA[1,000,000 CSL]]></limit>
  <limit_aggregate><![CDATA[1,000,000 CSL]]></limit_aggregate>
  <aggregate_prod_ops><![CDATA[1,000,000 CSL]]></aggregate_prod_ops>
  <exclude_prod_ops><![CDATA[0]]></exclude_prod_ops>
  <exclude_terrorism><![CDATA[1]]></exclude_terrorism>
  <forms>
    <required>
      <entry>
        <name><![CDATA[EX DS 01 05 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Declarations]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[HU DS 02 10 15]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Signature Endorsement]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX DS 02 05 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Schedule of Forms and Endorsements]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX DS 03 05 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Schedule of Controlling Underlying Insurance]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[CX 00 01 04 13]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Commercial Excess Liability Form]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX 21 01 02 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[New Residential Construction Limitation]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX 21 02 02 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Pre-Existing or Progressive Damage or Defect]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX 21 03 02 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Punitive Damages]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[CX 21 14 04 13]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - EIFS]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[CX 21 13 04 13]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Fungi Or Bacteria]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[CX 21 16 04 13]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Silica]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[CX 21 01 09 08]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Nuclear]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[CX 21 19 04 13]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Employment Related]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[CX 21 20 04 13]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Professional]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX 21 10 02 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Prior Completed or Abandoned Work]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX 21 11 02 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Drywall ]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX 21 13 02 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Cross Suits]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX 21 14 02 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Asbestos]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX 21 15 02 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Lead]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX 21 16 02 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Violation of Statutes]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX 21 17 02 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Exclusion - Wrap Up]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[HU 01 01 03 15]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Service of Suit]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[HU 01 02 03 15]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Hamilton Notice of Claims ]]></title>
        <state_exception><![CDATA[All Policies]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[CX 21 30 10 15]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Cap on Losses From Certified Acts of Terrorism]]></title>
        <state_exception><![CDATA[Attach based on accept / reject Terrorism]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[EX N 005 06 16]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[Disclosure Pursuant to Terrorism Risk Insurance Act]]></title>
        <state_exception><![CDATA[Attach based on accept / reject Terrorism]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
      <entry>
        <name><![CDATA[CX 02 22 09 08]]></name>
        <category><![CDATA[Common]]></category>
        <lob><![CDATA[Common]]></lob>
        <title><![CDATA[West Virginia Changes - Cancellation]]></title>
        <state><![CDATA[WV]]></state>
        <state_exception><![CDATA[WV Only]]></state_exception>
        <required><![CDATA[M]]></required>
      </entry>
    </required>
    <optional>
      <entry>
        <name><![CDATA[CX 31 08 11 13]]></name>
        <category><![CDATA[Auto Liability]]></category>
        <lob><![CDATA[Auto Liability]]></lob>
        <title><![CDATA[WV Excess UM UIM]]></title>
        <state><![CDATA[WV]]></state>
        <state_exception><![CDATA[Attach in WV only based on UM acceptance and choice]]></state_exception>
        <required><![CDATA[O]]></required>
      </entry>
    </optional>
  </forms>
  <transaction_type><![CDATA[New Business]]></transaction_type>
  <ppt><![CDATA[1]]></ppt>
  <extra_heavy_trucks_medium><![CDATA[1]]></extra_heavy_trucks_medium>
  <al_combined_single_limit><![CDATA[1,000,000 CSL]]></al_combined_single_limit>
  <auto_year_established><![CDATA[2014]]></auto_year_established>
  <selected_excess_layer><![CDATA[1]]></selected_excess_layer>
  <gl_actual_premium><![CDATA[2,300]]></gl_actual_premium>
  <emp_liab_each_accident><![CDATA[0,000]]></emp_liab_each_accident>
  <empl_liab_accid_effec_dt><![CDATA[07/22/2016]]></empl_liab_accid_effec_dt>
  <empl_liab_accid_expr_dt><![CDATA[07/22/2017]]></empl_liab_accid_expr_dt>
  <emp_liab_agg_policy_limit><![CDATA[0,000]]></emp_liab_agg_policy_limit>
  <emp_liab_each_employee><![CDATA[0,000]]></emp_liab_each_employee>
  <emp_benefit_liab><![CDATA[0,000 Claims Made]]></emp_benefit_liab>
  <empl_bene_liab_effec_dt><![CDATA[07/18/2016]]></empl_bene_liab_effec_dt>
  <empl_bene_liab_expr_dt><![CDATA[07/18/2017]]></empl_bene_liab_expr_dt>
  <retro_date><![CDATA[1468454400]]></retro_date>
  <retro_date_string><![CDATA[2016-07-14T00:00:00+00:00]]></retro_date_string>
  <um_cov_question><![CDATA[1]]></um_cov_question>
  <um_cov_equal_question><![CDATA[1]]></um_cov_equal_question>
  <score_gl><![CDATA[67]]></score_gl>
  <uw_questions>
    <defense_cost><![CDATA[1]]></defense_cost>
    <rated_a><![CDATA[1]]></rated_a>
    <rated_b><![CDATA[1]]></rated_b>
    <claims5_yrs><![CDATA[None]]></claims5_yrs>
    <fleet_exceed25><![CDATA[0]]></fleet_exceed25>
    <sub_contract_work><![CDATA[1]]></sub_contract_work>
    <req_equal_limits><![CDATA[1]]></req_equal_limits>
    <perform_work><![CDATA[0]]></perform_work>
    <hazard_hauling><![CDATA[0]]></hazard_hauling>
  </uw_questions>
</result>

使用xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication7
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            GetNode((XElement)doc.FirstNode);
        }
        static void GetNode(XElement element)
        {
            // Loop through the XML nodes until the leaf is reached.
            // Add the nodes to the TreeView during the looping process.

            List<XElement> children = element.Elements().ToList();
            for (int index = children.Count - 1; index >= 0; index--)
            {
                XElement child = children[index];
                if (child.Name.LocalName == "entry")
                {
                    child.ReplaceWith(new XElement("entry_" + (index + 1).ToString(), child.Elements()));
                }
                GetNode(child);

            }
        }
    }

}

尝试使用下面的代码替换所有标签。唯一不完全相同的是不必要的 CDATA 被删除了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication7
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            GetNode((XElement)doc.FirstNode);
        }
        static void GetNode(XElement element)
        {
            // Loop through the XML nodes until the leaf is reached.
            // Add the nodes to the TreeView during the looping process.

            List<XElement> children = element.Elements().ToList();
            var groups = element.Elements().GroupBy(x => x.Name.LocalName).ToList();
            foreach (var group in groups)
            {
                string tagName = group.Key;
                List<XElement> elements = group.ToList();
                for (int index = group.Count() - 1; index >= 0; index--)
                {
                    GetNode(elements[index]);
                    XElement newElement = new XElement(tagName + "_" + (index + 1).ToString(), new object[] { elements[index].Attributes(), elements[index].Elements()});
                    if (!newElement.HasElements) newElement.Add(elements[index].Value);
                    elements[index].ReplaceWith(newElement);


                }
            }
        }
    }

}

现在,如果您只想对重复项重新编号,试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication7
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            GetNode((XElement)doc.FirstNode);
        }
        static void GetNode(XElement element)
        {
            // Loop through the XML nodes until the leaf is reached.
            // Add the nodes to the TreeView during the looping process.

            List<XElement> children = element.Elements().ToList();
            var groups = element.Elements().GroupBy(x => x.Name.LocalName).ToList();
            foreach (var group in groups)
            {
                string tagName = group.Key;
                List<XElement> elements = group.ToList();
                if (group.Count() == 1)
                {
                    GetNode(group.FirstOrDefault());
                }
                else
                {
                    for (int index = group.Count() - 1; index >= 0; index--)
                    {
                        GetNode(elements[index]);
                        XElement newElement = new XElement(tagName + "_" + (index + 1).ToString(), new object[] { elements[index].Attributes(), elements[index].Elements() });
                        if (!newElement.HasElements) newElement.Add(elements[index].Value);
                        elements[index].ReplaceWith(newElement);
                    }
                }
            }
        }
    }

}

在VB中,这将创建XML的副本,并重命名所有入口节点,后缀为_number,当父节点更改时从1重新开始。第一部分只是您提供的数据 (XML)。

    'to load from a file
    '  xe = XElement.Load("Your Path Here")

    ' for testing
    Dim xe As XElement
    xe = <result>
             <quote><![CDATA[1776.5]]></quote>
             <account_name><![CDATA[Acme Mountaineers]]></account_name>
             <account_number><![CDATA[CDTEST-07052016]]></account_number>
             <effective_dt><![CDATA[1467676800]]></effective_dt>
             <effective_dt_string><![CDATA[2016-07-05T00:00:00+00:00]]></effective_dt_string>
             <zip><![CDATA[25301]]></zip>
             <street><![CDATA[170 SE 3rd Ave]]></street>
             <city><![CDATA[CHARLESTON]]></city>
             <state><![CDATA[WV]]></state>
             <products>
                 <entry><![CDATA[gl]]></entry>
                 <entry><![CDATA[auto]]></entry>
             </products>
             <coverage_type><![CDATA[Occurrence]]></coverage_type>
             <product_type><![CDATA[Excess]]></product_type>
             <admitted_status><![CDATA[Non Admitted]]></admitted_status>
             <classifications>
                 <entry>
                     <location><![CDATA[1]]></location>
                     <street><![CDATA[170 SE 3rd Ave]]></street>
                     <city><![CDATA[CHARLESTON]]></city>
                     <county><![CDATA[KANAWHA]]></county>
                     <state><![CDATA[WV]]></state>
                     <territory><![CDATA[1]]></territory>
                     <zip><![CDATA[25301]]></zip>
                     <class_code><![CDATA[91582]]></class_code>
                     <class_code_desc><![CDATA[(91582) Contractors - subcontracted work - in connection with building construction, reconstruction,]]></class_code_desc>
                     <premium_basis><![CDATA[Total Cost]]></premium_basis>
                     <premium_basis_amount><![CDATA[200000]]></premium_basis_amount>
                     <authority><![CDATA[A]]></authority>
                 </entry>
             </classifications>
             <limit><![CDATA[1,000,000 CSL]]></limit>
             <limit_aggregate><![CDATA[1,000,000 CSL]]></limit_aggregate>
             <aggregate_prod_ops><![CDATA[1,000,000 CSL]]></aggregate_prod_ops>
             <exclude_prod_ops><![CDATA[0]]></exclude_prod_ops>
             <exclude_terrorism><![CDATA[1]]></exclude_terrorism>
             <forms>
                 <required>
                     <entry>
                         <name><![CDATA[EX DS 01 05 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Declarations]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[HU DS 02 10 15]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Signature Endorsement]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX DS 02 05 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Schedule of Forms and Endorsements]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX DS 03 05 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Schedule of Controlling Underlying Insurance]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[CX 00 01 04 13]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Commercial Excess Liability Form]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX 21 01 02 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[New Residential Construction Limitation]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX 21 02 02 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Pre-Existing or Progressive Damage or Defect]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX 21 03 02 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Punitive Damages]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[CX 21 14 04 13]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - EIFS]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[CX 21 13 04 13]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Fungi Or Bacteria]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[CX 21 16 04 13]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Silica]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[CX 21 01 09 08]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Nuclear]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[CX 21 19 04 13]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Employment Related]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[CX 21 20 04 13]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Professional]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX 21 10 02 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Prior Completed or Abandoned Work]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX 21 11 02 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Drywall ]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX 21 13 02 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Cross Suits]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX 21 14 02 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Asbestos]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX 21 15 02 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Lead]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX 21 16 02 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Violation of Statutes]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX 21 17 02 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Exclusion - Wrap Up]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[HU 01 01 03 15]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Service of Suit]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[HU 01 02 03 15]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Hamilton Notice of Claims ]]></title>
                         <state_exception><![CDATA[All Policies]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[CX 21 30 10 15]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Cap on Losses From Certified Acts of Terrorism]]></title>
                         <state_exception><![CDATA[Attach based on accept / reject Terrorism]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[EX N 005 06 16]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[Disclosure Pursuant to Terrorism Risk Insurance Act]]></title>
                         <state_exception><![CDATA[Attach based on accept / reject Terrorism]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                     <entry>
                         <name><![CDATA[CX 02 22 09 08]]></name>
                         <category><![CDATA[Common]]></category>
                         <lob><![CDATA[Common]]></lob>
                         <title><![CDATA[West Virginia Changes - Cancellation]]></title>
                         <state><![CDATA[WV]]></state>
                         <state_exception><![CDATA[WV Only]]></state_exception>
                         <required><![CDATA[M]]></required>
                     </entry>
                 </required>
                 <optional>
                     <entry>
                         <name><![CDATA[CX 31 08 11 13]]></name>
                         <category><![CDATA[Auto Liability]]></category>
                         <lob><![CDATA[Auto Liability]]></lob>
                         <title><![CDATA[WV Excess UM UIM]]></title>
                         <state><![CDATA[WV]]></state>
                         <state_exception><![CDATA[Attach in WV only based on UM acceptance and choice]]></state_exception>
                         <required><![CDATA[O]]></required>
                     </entry>
                 </optional>
             </forms>
             <transaction_type><![CDATA[New Business]]></transaction_type>
             <ppt><![CDATA[1]]></ppt>
             <extra_heavy_trucks_medium><![CDATA[1]]></extra_heavy_trucks_medium>
             <al_combined_single_limit><![CDATA[1,000,000 CSL]]></al_combined_single_limit>
             <auto_year_established><![CDATA[2014]]></auto_year_established>
             <selected_excess_layer><![CDATA[1]]></selected_excess_layer>
             <gl_actual_premium><![CDATA[2,300]]></gl_actual_premium>
             <emp_liab_each_accident><![CDATA[0,000]]></emp_liab_each_accident>
             <empl_liab_accid_effec_dt><![CDATA[07/22/2016]]></empl_liab_accid_effec_dt>
             <empl_liab_accid_expr_dt><![CDATA[07/22/2017]]></empl_liab_accid_expr_dt>
             <emp_liab_agg_policy_limit><![CDATA[0,000]]></emp_liab_agg_policy_limit>
             <emp_liab_each_employee><![CDATA[0,000]]></emp_liab_each_employee>
             <emp_benefit_liab><![CDATA[0,000 Claims Made]]></emp_benefit_liab>
             <empl_bene_liab_effec_dt><![CDATA[07/18/2016]]></empl_bene_liab_effec_dt>
             <empl_bene_liab_expr_dt><![CDATA[07/18/2017]]></empl_bene_liab_expr_dt>
             <retro_date><![CDATA[1468454400]]></retro_date>
             <retro_date_string><![CDATA[2016-07-14T00:00:00+00:00]]></retro_date_string>
             <um_cov_question><![CDATA[1]]></um_cov_question>
             <um_cov_equal_question><![CDATA[1]]></um_cov_equal_question>
             <score_gl><![CDATA[67]]></score_gl>
             <uw_questions>
                 <defense_cost><![CDATA[1]]></defense_cost>
                 <rated_a><![CDATA[1]]></rated_a>
                 <rated_b><![CDATA[1]]></rated_b>
                 <claims5_yrs><![CDATA[None]]></claims5_yrs>
                 <fleet_exceed25><![CDATA[0]]></fleet_exceed25>
                 <sub_contract_work><![CDATA[1]]></sub_contract_work>
                 <req_equal_limits><![CDATA[1]]></req_equal_limits>
                 <perform_work><![CDATA[0]]></perform_work>
                 <hazard_hauling><![CDATA[0]]></hazard_hauling>
             </uw_questions>
         </result>

'========================================== ================

    Dim newXML As XElement = New XElement(xe) 'create a copy

    Dim ie As IEnumerable(Of XElement) = newXML...<entry>
    Dim lastParent As String = ""
    Dim ct As Integer

    For Each el As XElement In ie
        If el.Parent.Name.LocalName <> lastParent Then
            ct = 1
            lastParent = el.Parent.Name.LocalName
        Else
            ct += 1
        End If
        el.Name = "entry_" & ct.ToString
    Next

    newXML.Save("path here")