3

I have an object that I am trying to serialise and the output looks something like this:

 <root>
  <Items>
    <Item>   
      <Value> blabla </Value>
    </Item>  
  </Items>

where Item is a class that the class root uses.

[Serializable]
[XmlType("root")]
public class Root { }

[Serializable]
[XmlInclude(typeof(Item))]
public class Items {}

[Serializable]
public class Item 
{ 
    [XmlElement("Value")]
    public string DefaultValue { get; set; }
}

In some cases I want to ignore the value of value and I have this code

 var overrides = new XmlAttributeOverrides();
 var attributes = new XmlAttributes { XmlIgnore = true };
 attributes.XmlElements.Add(new XmlElementAttribute("Item"));                  
 overrides.Add(typeof(Item), "Value", attributes);               
 var serializer = new XmlSerializer(typeof(root), overrides);

but the value is still written in the output.

What am I doing wrong?

2
  • 1
    Could you add the Item class? Mar 10, 2011 at 13:05
  • 1
    Side note: The [Serializable] attribute is not meaningful when dealing with Xml Serialization in .NET.
    – Cheeso
    Mar 10, 2011 at 15:29

3 Answers 3

3

Now that you updated your question, it is obvious what you are doing wrong. :)

[Serializable]
public class Item 
{ 
    [XmlElement("Value")]
    public string DefaultValue { get; set; }
}

You should pass the name of the property instead of the xml name, as specified in the documentation.

overrides.Add(typeof(Item), "DefaultValue", attributes);

... instead of ...

overrides.Add(typeof(Item), "Value", attributes);

Also as specified in Fun Mun Pieng's answer, you shouldn't add the XmlElementAttribute anymore, so remove the following line:

 attributes.XmlElements.Add(new XmlElementAttribute("Item"));  
1
  • Yup that was the problem. and I thought that the class was obvious! This also means that some old parts of the code are also wrong. yay :/
    – Marcom
    Mar 10, 2011 at 15:25
3

If value is always ignored, you're better off assigning the attribute directly to the member.

[Serializable]
[XmlInclude(typeof(Item))]
public class Items
{
    [XmlIgnore]
    public string Value
}

If value is conditionally ignored, I suspect you're better off removing the element from the root class before serializing.

As for your code, I suspect (I may be wrong because I haven't try it yet!) the following is sufficient:

var overrides = new XmlAttributeOverrides();
var attributes = new XmlAttributes { XmlIgnore = true };
overrides.Add(typeof(Items), "Value", attributes);               
serializer =  new XmlSerializer(typeof(root), overrides);

Update: I tested the above code. It works. :D Update again: it should be Items instead of Item, because Value is in Items. Or if you like it the other way, it could be Value in Item and Item all the way.

5
  • I just thought of the same thing, this might be the solution. This does raise the question what to do when the XmlElement is already applied, how can you remove it? Mar 10, 2011 at 13:10
  • Yeah I copied and pasted an extra line as im trying different things but that dosent work either :/
    – Marcom
    Mar 10, 2011 at 13:27
  • and for reasons there is no point going into, removing the element before serializing is not a trival matter.
    – Marcom
    Mar 10, 2011 at 13:36
  • @Marcom: Apparently it works for Fun Mun Pieng, and all documentation also specifies it should work. Mar 10, 2011 at 13:55
  • @Marcom: It may not have worked due to the difference between Item and Items. Mar 10, 2011 at 14:00
0

I believe XMLIgnore attribute should be used to decorate a public member of a class which has been decorated with XmlSerializable attribute, that way would work.

1
  • but want it to be ignored in some cases, not all the time.
    – Marcom
    Mar 10, 2011 at 12:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.