Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Manipulating the output of empty XML elements for .NET serialization

Posted on 2014-04-15
5
1,339 Views
Last Modified: 2014-05-08
Greetings,

I need to find out how to manipulate the serialization of objects for empty elements.  Specifically my requirement is to pass a <xmlnode></xmlnode> for empty nodes versus <xmlnode/>.  

I am using the XML serializer to do this.  

I tried setting my object as object.node = string.empty, but this only construct xml documents with nodes as <node/>.  

The other technique i tried was setting a blank space like object.node = " ", but this also is being constructed as <node/>.  

Any ideas how I can force a "full" xml element that is empty?
0
Comment
Question by:DislocatedGI
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 40002987
You have to do two things... You have to set the contents of whatever serializes to your <xmlnode> to an empty string and you have to create your own XmlWriter subclass that redirects calls of WriteEndElement() to WriteFullEndElement().

The following sample code should show you how to do it (note that the Description field gets outputted as you are after)...
using System.IO;
using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

public class Test {   
    [XmlElement]
    public string Name;
    [XmlElement]
    public string Description;

    static void Main() {
        XmlSerializer serializer = new XmlSerializer(typeof(Test));
        Test test = new Test();
        test.Name = "Tester";
        test.Description = "";

        XmlWriter writer = new MyXmlWriter(Console.Out);
        serializer.Serialize(writer, test);
        writer.Close();

    }
    
    public class MyXmlWriter : XmlTextWriter {
        public MyXmlWriter(TextWriter textWriter) : base(textWriter) {
        }

        public override void WriteEndElement() {
            WriteFullEndElement();
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:DislocatedGI
ID: 40004391
Thank you for the quick response and solution.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40005573
Your welcome! :)

However, I would like to point out one small thing. I see that you have been around here for a while but have not asked many question at all, so I'm guessing that you may not be aware of the "grading tips".

http://support.experts-exchange.com/customer/portal/articles/481419

If upon reading the above, you would like to change the grade assigned to this question, you can click the "Request Attention" link that is located just below your original question, and an Admin can help further. This may seem like a insignificant issue, but the reason that I raise it is that if you were to ask more questions in the future, some experts will look at the gradings that you have assigned to previous questions before attempting to answer, and so you are more likely to get help from everyone here on EE if your gradings follow the general advice in those tips above.
0
 

Author Comment

by:DislocatedGI
ID: 40024691
Sorry for the late response.  I was tied uptight family stuff.

How do I go about changing the grade?  I looked for the request attention link
 Mccarl references.

Now that I read the grading practices I would like to change it to an A.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These instructions are based on installing Owncloud on your new raspberry pi connected with a usb HDD. What do you need Part A? A Raspberry Pi, model B. A boot SD card for the Raspberry Pi. A usb HDD An Ethernet cable to connect to the lo…
To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question