0

I have the following class fragment:

public class varString
{
    public string KeyWord;
    public string Value;
    public static implicit operator string(varString v)
    {
        return v == null ? null : (string)v.Value;
    }
}

KeyWord is allowed to be null.

When serializing to XML, I get the following output when KeyWord is null:

<varString>
    <Value>value goes here</Value>
</varString>

How can I get the XML serializer to output the following when and only when KeyWord is null?:

<varString>value goes here</varString>

If KeyWord is not null, I'd still like it to output the following:

<varString>
    <!-- Can either be <Value></Value or just straight text -->
    <KeyWord>KeyWord goes here</KeyWord>
<varString>

Please note that I have already modified the deserialize events to handle this case to convert the lone string into a varString with a null KeyWord.

2
  • 1
    Why do you need that? It makes the xml harder to parse for the receipent. Commented Nov 2, 2016 at 19:53
  • It's easier for me to edit the xml in this way. KeyWord is 9/10 times null, so I'd rather not have the extra tag output. Just personal preference is all, doesn't really affect anything. Commented Nov 2, 2016 at 20:10

2 Answers 2

1

Mark Value with the attribute [XmlText]:

public class varString
{
    [XmlText]
    public string Value;
    public string KeyWord;
    public static implicit operator string(varString v)
    {
        return v == null ? null : (string)v.Value;
    }
}

Sample fiddle.

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

4 Comments

This works in the fiddle, but I can't seem to get it to work with a more complex object that contains these in my code. However, I think this is the easiest and simplest solution so I'm marking it as the answer because it does work.
@Hondros - Check the Remarks section of the docs, e.g. Only one instance of the XmlTextAttribute class can be applied in a class. and You can apply the XmlTextAttribute to public fields and public read/write properties that return primitive and enumeration types.
Hm interesting. I modified your fiddle to see if it would support classes with defined varStrings: dotnetfiddle.net/yan3ts . And it does in fact work. I'm sure I can figure it out from here though. Thanks so much!
I figured it out. I'm running a converter on some of my xml files, so the input was expecting XmlText even though <value> tags are currently there, so they weren't being read in.
0

Try this approach:

var vs = new varString();
vs.KeyWord = "key"; // null;
vs.Value = "value";


var attr = new XmlAttributes();

if (vs.KeyWord == null)
    attr.XmlText = new XmlTextAttribute();
else
    attr.XmlElements.Add(new XmlElementAttribute { ElementName = "Value" });

var overrides = new XmlAttributeOverrides();
overrides.Add(typeof(varString), "Value", attr);


var xs = new XmlSerializer(typeof(varString), overrides);
xs.Serialize(Console.Out, vs);

Depending on the KeyWord value we add XmlTextAttribute or XmlElementAttribute.

3 Comments

Unfortunately, I don't think this will work for me because each varString would have a null keyword on a case by case basis, and I am using a container class to hold multiple varStrings.
@Hondros - Yes, for multiple values it's inapplicable.
You need to cache the serializer if you construct it using XmlAttributeOverrides. See Memory Leak using StreamReader and XmlSerializer

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.