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 885367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:54:00+00:00 2026-05-15T12:54:00+00:00

I am performing XML Operations on an XHTML document utilizing Xml and Linq. When

  • 0

I am performing XML Operations on an XHTML document utilizing Xml and Linq.

When converting it back to a String for outputting to a browser, it renders script tags in their original provided form when creating the XElement as
<script />.

That prevents it being processed correctly by most browsers on the market.

I would like to perform the XElement to String conversion in a way that it outputs the script tag like this <script></script>

Can someone help me on that? Thanks in advance. 🙂

First edit
Providing some more information, the data is coming from an xml field in a MSSQL 2008R2 database.
It is loaded from the xml field as
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js" />

Second edit
Working sample

using System;
using System.Linq;
using System.Xml.Linq;

namespace Test1
{
    class Test1
    {
        static void Main(string[] args)
        {
            XElement element = XElement.Parse("<div><script /><script>Test</script></div>");
            var EmptySet = (from e in element.DescendantsAndSelf()
                           where e.IsEmpty
                           select e);
            foreach(XElement e in EmptySet)
            {
                e.Value = String.Empty;
            }
            Console.WriteLine(element.ToString(SaveOptions.None));
        }
    }
}

And its results

<div>
  <script></script>
  <script>Test</script>
</div>
  • 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-05-15T12:54:01+00:00Added an answer on May 15, 2026 at 12:54 pm

    Have you tried using SaveOptions.DisableFormatting?

    I can’t actually reproduce the problem, mind you… what XML operations are you performing? Are you creating the script element yourself? This works fine:

    using System;
    using System.Xml.Linq;
    
    class Test
    {
        static void Main()
        {
            XElement element = XElement.Parse("<script></script>");
            // Both of these write <script></script>
            Console.WriteLine(element.ToString(SaveOptions.None));
            Console.WriteLine(element.ToString(SaveOptions.DisableFormatting));
        }
    }
    

    (XDocument.Save and XElement.Save have the same options.)

    EDIT: To change an element with no child nodes into one with a single empty text node, you can just set element.Value to “”. For example:

    XElement element = XElement.Parse("<script />");
    Console.WriteLine(element.ToString()); // Prints <script />
    element.Value = "";
    Console.WriteLine(element.ToString()); // Prints <script></script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having String.xml and performing Localization. while writing content in String tag I
When performing many disk operations, does multithreading help, hinder, or make no difference? For
I have some working Javascript code that generates an RDF/XML document using variables picked
I'm performing a trade study evaluating various methods for parsing XML for a large
I am performing a search in an XML file, using the following code: $result
Has anyone had experience performing xml transform on resx files before? I would like
While performing a conditional DELETE operation in one of my InnoDB tables, which apparently
While performing some upcoming maintenance, I'm going to have to redirect all site traffic
When performing many inserts into a database I would usually have code like this:
Occasionally when performing a rebase using the MercurialEclipse plugin my repository gets thrown into

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.