By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

How do I read values from XmlEnumAttribute values of an Enum

P: 2
I created a class out of .xsd. Now I am generating a new xml file and trying to add "Notice" as an element with an attribute of Index="001". I'm trying to get to value XmlEnumAttribute("0001"). This is how my code looks like:
*************************Class******************** *************
public partial class Notice {

private NoticeIndex indexField;

public Notice() {
this.indexField = NoticeIndex.Item0001;

/// <remarks/>
public NoticeIndex Index {
get {
return this.indexField;
set {
this.indexField = value;

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.1432")]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=true)]
public enum NoticeIndex {

/// <remarks/>
*************************End Class*********************************
I saw another post here and tried this from my program but it complains about converting Index to string:
***************************Program**************** ******************
Notice notice = new Notice();
Type enumType = typeof(NoticeIndex);
foreach (System.Reflection.FieldInfo fi in enumType.GetFields())
object[] attrs = fi.GetCustomAttributes(typeof(XmlEnumAttribute), false);
if (attrs.Length > 0)
bSNShipNotice.Index = ((XmlEnumAttribute)attrs[0]).Name;

xml.Element.Notice = notice;
************************************************** *******************
Any help would be greatly appreciated.
Jan 9 '09 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 229
If I understood what you're asking, your problem is that your Index property does not get serialized. The problem is that XmlSerializer does not serialize properties when their value is equal to their default value.

To resolve this, you can comment out the "DefaultValue" attributes for your properties:

Expand|Select|Wrap|Line Numbers
  1. // [System.ComponentModel.DefaultValueAttribute(NoticeIndex.Item0001)]
If xsd is generating your class at build time (on every build), you can also remove the 'default' attribute from your Notice element xsd schema to avoid getting the DefaultValueAttribute in code, but if you cannot change the schema and need to have it updated on every build, consider adding an additional build script to comment out those lines for you (at Perl or batch file, or even a small C# console app, just to be sure that you don't forget it).
Jan 12 '09 #2

P: 2
Thanks for the help vekipeki, it actually works after I comment out the default attribute value and i'm not generating a class out of xsd on a runtime, its just one time thing so I guess i will be okay.
Jan 14 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.