3

I'm serializing an object to xml in c# and I would like to serialize

public String Marker { get; set; }

into

<Marker></Marker>

when string Marker has no value.

Now I get

<Marker />

for Marker == string.Empty and no Marker node for null. How can I get this ?

7
  • 3
    I think you should expand on why you want to do this. Semantically, the two forms have no difference, so if you are writing a system in which they are not interchangable, it will confuse anyone (including yourself) who looks at it in the future. Commented Apr 28, 2011 at 10:33
  • Why would you care whether the element is in short or expanded form? It doesn't change the meaning... Commented Apr 28, 2011 at 10:33
  • Is it just that you like the <Marker></Marker> representation better or is there a technical reason? It almost never makes a difference (Actually, only the HTML <script> tag comes to my mind - and that's due to the deficiency of browsers). Commented Apr 28, 2011 at 10:33
  • 1
    I know that this doesn't change the meaning, but the parser on servers (old phone central) side only excepts expanded forms. Commented Apr 28, 2011 at 10:35
  • Are you only writing XML data or do you also need to consume/read XML? If you only need to read you might be better off using the XmlWriter class. Commented Apr 28, 2011 at 11:04

3 Answers 3

2

You can easily suppress the <Marker> element if the Marker property is null. Just add a ShouldSerializeMarker() method:

public bool ShouldSerializeMarker()
{
    return Marker != null;
}

It will be called automatically by XmlSerializer to decide whether or not to include the element in the output.

As for using the expanded form of the <Marker> element when the string is empty, there's no easy way to do it (you could probably write your own XmlWriter, but it would be a pain). But anyway it doesn't make sense, because <Marker /> and <Marker></Marker> have exactly the same meaning.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to have the closing tags you have to change the implementation of your xml structure; as far as I understand this topic of serialization separate closing tags are only produced if you are serializing a 'complex' object (eg: a class) and not a 'simple' object (eg: a string).

An example would be:

[XmlRoot]
public class ClassToSerialize
{
  private StringWithOpenAndClosingNodeClass mStringWithOpenAndClosingNode;

  [XmlElement]
  public StringWithOpenAndClosingNodeClass Marker
  {
    get { return mStringWithOpenAndClosingNode ?? new StringWithOpenAndClosingNodeClass(); }
    set { mStringWithOpenAndClosingNode = value; }
  }
}

[XmlRoot]
public class StringWithOpenAndClosingNodeClass
{
  private string mValue;

  [XmlText]
  public string Value
  {
    get { return mValue ?? string.Empty; }
    set { mValue = value; }
  }
}

If you serialize this object to XML you'll get:

<ClassToSerialize><Marker></Marker></ClassToSerialize>

I hope this helps!

2 Comments

Great idea, but when I serialize ClassToSerialize I get <ClassToSerialize> <Marker /> </ClassToSerialize>. But thanks anyway.
NO, that's not true because Marker is represented as a separate class (BTW this separate class could be reused for other string properties that need to follow the same pattern)
-1

You can use the IsNullable property of the XMLElementAttribute to configure the XmlSerializer to generate XML for your null value.

Haven't figured out how to produce an opening and closing elements for an element with no value, though. What you have already is perfectly legal XML. That is,

<Marker></Marker> 

is the same as

<Marker/>

Do you really need both opening and closing tags?

4 Comments

Unfortunately yes... Parser on servers side only excepts expanded forms. Shorter form returns an error, but when I manually send expanded form it works like a charm.
So the solution after you write the file to replace all the shorter forms with the expanded forms by looping through the file.
That's a good idea. I'll call it my emergency plan if no faster solution appears.
@Jonah Did you need to resort to your emergency plan?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.