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

  • SEARCH
  • Home
  • 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 6046207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:12:43+00:00 2026-05-23T07:12:43+00:00

This is the same question as: Explicit Element Closing Tags with System.Xml.Linq Namespace but

  • 0

This is the same question as:
Explicit Element Closing Tags with System.Xml.Linq Namespace

but I use Net 4.0 and the answers do not work anymore.

The problem is I save tags with no values really, and my output XML looks like this:

<field/>

But what I need is always opening and closing tag, i.e.

<field></field>

QUESTION: how to do it?

Edits

1

Adding empty nodes:

if (field_xml == null) // always true, because I create the file for the first time
{
    field_xml = new XElement(XMLKeys.field,String.Empty);
    table_xml.Add(field_xml);
}
field_xml.SetAttributeValue(XMLKeys.name, field_info.Name);
// ... setting some other attributes of this node

and later, saving the xml:

var writer = new FullEndingXmlTextWriter(parameters.OutputFilename, Encoding.UTF8);
root_xml.Save(writer);

FullEndingXmlTextWriter is the specialized class which The Evil Greebo pointed out (it is supposed to force explicit closing tag).

  • 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-23T07:12:44+00:00Added an answer on May 23, 2026 at 7:12 am

    I can’t reproduce your error. This works as expected in both 4.0 and 3.5 netFX:

    namespace ExplicitXmlClosingTags
    {
        using System.Xml;
        using System.Xml.Linq;
    
        class Program
        {
            static void Main(string[] args)
            {
                const string ElementRoot = "RootElement";
                const string ElementChild = "ChildElement";
                const string AttributeChild = "ChildAttribute";
    
                XDocument xDoc = new XDocument();
                XElement root = new XElement(ElementRoot);
                XElement child = new XElement(ElementChild, string.Empty);
                root.Add(child);
    
                child.SetAttributeValue(AttributeChild, "AttrValue");
                xDoc.Add(root);
    
                XmlWriterSettings xws = new XmlWriterSettings();
                xws.Indent = true;
                using (XmlWriter xw = XmlWriter.Create("out.xml", xws))
                {
                    xDoc.Save(xw);    
                }
            }
        }
    }
    

    producing following content:

    <?xml version="1.0" encoding="utf-8"?>
    <RootElement>
      <ChildElement ChildAttribute="AttrValue"></ChildElement>
    </RootElement>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is driving me nuts. I believe I asked this exact same question, but
This is the same question as this but I'm looking for a classic ASP
This is the same question for older version of Scala, but they say that
The same as this question but for java Update Based on the comments and
[This question is related to but not the same as this one .] My
I saw this same question for VIM and it has been something that I
I've asked this same question with Python. Now I like to know if this
I thought I would rewrite this question (same iteration). The original was how to
This is basically the same question as Click items in select box, and then
I had the same question as was asked in this thread , i.e. I

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.