Solved

Manipulating the output of empty XML elements for .NET serialization

Posted on 2014-04-15
5
1,444 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
[X]
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
  • 2
  • 2
5 Comments
 
LVL 36

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 36

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

726 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