Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8112009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:19:35+00:00 2026-06-06T02:19:35+00:00

I am trying to programmatically clean up invalid XML with duplicate root elements in

  • 0

I am trying to programmatically clean up invalid XML with duplicate root elements in C# .NET 4.0. What I want to do is consolidate all of the inner elements into one root element and remove the duplicates roots, so that

<a>
    <b></b>
</a>
<a>
    <c></c>
</a>

becomes

<a>
    <b></b>
    <c></c>
</a>

However, the duplicated root element could also appear in the inner XML. In that case, we would not want to replace it, so that

<a>
    <a></a>
    <b></b>
</a>
<a>
    <c></c>
    <a></a>
</a>

becomes

<a>
    <a></a>
    <b></b>
    <c></c>
    <a></a>
</a>

Also, the duplicated root element isn’t guaranteed to always be <a>; it could have any name.

Thus far I’ve been trying to think of some sort of elegant Regex to accomplish this task, such as /<((.|\n|\r)*?)>(.|\n|\r)*<\/\1>/, but the problem with this is that a greedy match on the inner XML matches too much, and non-greedy match on the inner XML matches too little.

I was hoping I wouldn’t have to resort to creating a stack to count open and close tags to identify when I was back to the root of the document. I’m looking for a simple and elegant way of solving this problem.

Open source, third-party libraries are potentially acceptable solutions if one of them handles this kind of situation, but I’d rather avoid them.

Does anyone have any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:19:36+00:00Added an answer on June 6, 2026 at 2:19 am

    It may be better to actually read XML as XML… You should be able to create reader with ConformanceLevel set to Fragment and read all fragments as normal XML. And than use normal XML processing to select/copy Xml nodes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to get into the 'C:\Windows\System32\winevt\Logs' folder programmatically using C# so I
I am trying to programmatically enter values into my table. I cannot use a
I'm trying to programmatically create an unordered list using the asp.net HTMLGenericControl. It properly
Trying to programmatically add options to a SELECT drop down in IE Windows Mobile.
I'm trying to programmatically recreate the indented button look that can be seen on
I'm trying to programmatically create a window with my own custom OpenGL NSView as
I'm trying to programmatically add an Execute SQL task from within a script task
I am trying to programmatically create a PowerPoint from graphs in Access. Ideally, when
I am trying to programmatically drop a column in a table inside of an
I'm trying to programmatically extract information from an Enterprise Architect model (saved in an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.