Solved

Manipulating the output of empty XML elements for .NET serialization

Posted on 2014-04-15
5
1,237 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When coding a Webservice to provide data, the normal way is to create a method that returns a Dataset object.   But a Dataset cannot be used easily from another platform, such as PHP,  FLAS (FLEX) , etc. Solution : Return a XML Document instea…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now