Gawi
Gawi

Reputation: 47

xml serialization in c#

My class looks very similar to this:

[Serializable]
class ExampleClass {
    public bool Value { get; set; }
    public string Names { get; set; }
}

I would like to serialize it to this: for Value == true :

<ExampleClass>
   <Value>
   <Names>SomeName</Names>
</ExampleClass>

and for Value == false :

<ExampleClass>
   <Value/>
   <Names>SomeName</Names>
</ExampleClass>

As you can see for Value==true we get an opening tag and no attributes. In case when Value is false we get only closing tag.

How can I achieve this with c# serialization?

Upvotes: 1

Views: 525

Answers (6)

Dipti Mehta
Dipti Mehta

Reputation: 547

Well, as correctly said by everyone, what you are trying to do in case of value = true is invalid attribute.

Instead, as i assume that you want the value thing to be present in the XML only when it is true, add it as an attribute. or say as an optional attribute. Which is present only when the value is true.

Like : When true,

 <ExampleClass value=true>           
 <Names>SomeName</Names>  
 </ExampleClass>

and when its false

 <ExampleClass >       
 <Names>SomeName</Names> 
 </ExampleClass>

You will have to add the following attributes to the property in class :

 [System.Xml.Serialization.XmlAttributeAttribute()]
 [System.ComponentModel.DefaultValueAttribute(false)]

When the value of the element/attribute is equal to default value , it is not included in the generated xml !.

Hope this helps.

Upvotes: 0

Henius
Henius

Reputation: 1

There's a way to do this (even for this crappy protocol you use):

[XmlIgnore]
public bool Value { get; set; }

[XmlText]
public string ValueString
{
    get
    {
        return Value ? "<Value>" : "<Value/>";
    }
    set
    {

    }
}

Upvotes: -1

codeulike
codeulike

Reputation: 23064

As others have said, what you are asking to produce is not valid XML.

I once had to produce an 'xml' file that had a non-printing character in a certain place. Of course, thats not XML, but you still might get asked to do it.

Lets assume that you can't get hold of the people specifying the protocol and call on the wrath of the XML-gods to strike them with lightning.

If you need to produce something that is a-bit-like-xml-but-not-quite, running it through a standard XML serializer into a string variable and then doing a search/replace might be your best option. Something like:

XmlSerializer serializer = new XmlSerializer(someobj.GetType());
string asString = null;
using (StringWriter writer = new StringWriter())
{
    serializer.Serialize(writer, someobj);
    asString = writer.ToString();
}
string weirdXml = asString.Replace("<Value>True</Value>","<Value>").Replace("<Value>False</Value>","<Value/>");

(make sure you pray for forgiveness to the XML-gods each time you compile the code though)

Upvotes: 1

Hanan
Hanan

Reputation: 1445

I have a feeling that all you really asking for is someway to convert an object to XML and you don't really care about the implementing the ISerializable interface.

If that's the case just use TextWriter and write your text file according to your object.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273169

You seem to want invalid XML.

No tool or API will let you do that, you'll have to write your own XML using a TextWriter.

The best advice of course is not to do this. Rethink your solution.

Upvotes: 1

Tom W
Tom W

Reputation: 5403

Could I ask why you want to deliberately misuse the .NET libraries? As has been mentioned, what you're asking for is invalid XML, and most classes you'll find in the framework are specifically designed to avoid mistakes like mismatched tags. So the likely answer is, you can't.

Someone can probably find you a better solution if you describe the problem in more detail.

Upvotes: 2

Related Questions