473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

How do I read values from XmlEnumAttribute values of an Enum

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/>
[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute(
NoticeIndex.Item0001)]
public NoticeIndex Index {
get {
return this.indexField;
}
set {
this.indexField = value;
}
}

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

/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("0001")]
Item0001,
}
*************************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
2 6839
vekipeki
229 Expert 100+
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)]
  2.  
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
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

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

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
1
by: Sergey Poberezovskiy | last post by:
Hi, I have a simple enumeration in my schema: <xs:element name="el_1"> <xs:simpleType> <xs:restiction base="xs:string"> <xs:enumeration value="value and space 1"/> <xs:enumeration...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
3
by: giant food | last post by:
This seems like a long shot, but I wondered if there's a way to loop over the values in an Enum. For example, say I have an Enum as follows: Public Enum statusValues Ready Running Finished...
6
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
5
by: Edward Clements | last post by:
I have an enum defined as public enum velocityUom { /// <remarks/> ms, /// <remarks/> cma, ...
0
by: Edward Clements | last post by:
This post is a followup to the thread "retrieving the XmlEnumAttribute values for an Enum" in this same forum earlier this month, since my last query in that thread went unanswered. I have an...
0
by: Bum | last post by:
Hello all, I have a C dll I need to tap into and some C code to interpret to vb.net 2005, but am so far unsuccessful. I just get the generic "runtime has encountered a fatal error at XXXXXX...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.