77018

A few XmlElement attributes on a property C#

Question:

Can I have more than one XmlElement on property of a class? For example:

[XmlElement("name")] [XmlElement("clientName")] public string Name { .. }

I need this for deserialization. Let's say that the Name element in the XML file will be named "name" or "clientName". I want to achieve some kind of flexibility (to list the possible names for the xml element which correspond to the Name attribute.

The main idea is that I have to import XML files from another program and I have to make some kind of "templates for import".

Answer1:

The answer is actually yes, but only under certain conditions. If you want a different element name for different types, you can do that. As for no type specified, the <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute.aspx" rel="nofollow">documentation</a> says nothing.

[XmlElement(typeof(int), ElementName = "ObjectNumber"), XmlElement(typeof(string), ElementName = "ObjectString")] public ArrayList ExtraInfo;

Answer2:

Guessing that you need to import XML with two different element names for the same value you could do this:

string _Name; [XmlElement("name")] public string Name { get { return _Name; } set { _Name = value; } } [XmlElement("clientName")] public string ClientName { get { return _Name; } set { _Name = value; } }

Recommend

  • Model with BatchNormalization: stagnant test loss
  • How to handle a Boolean when expecting a String in GSON? [closed]
  • How to understand ndarray.reshape function?
  • Failed to resolve: com.google.gms.google-services:15.0.1:
  • calculate daily averages for 3d array
  • Finding references in Visual Studio
  • Geom_jitter colour based on values
  • EntLib Way to Bind “Null” Value to Parameter
  • NHibernate manually control fetching
  • How do I remove all but some records based on a threshold?
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • Django model inheritance, filtering models
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Installed module is empty
  • Play WS (2.2.1): post/put large request
  • Yii2: Config params vs. const/define
  • Ajax Loaded meta Tags
  • Read text file and split every line in MSBuild
  • C# - Serializing and deserializing static member
  • HTML download movie download link
  • Is possible to count alias result on mysql
  • Java applet as stand-alone Windows application?
  • output of program is not same as passed argument
  • Deserializing XML into class C#
  • Akka Routing: Reply's send to router ends up as dead letters
  • Is there a mandatory requirement to switch app.yaml?
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • KeystoneJS: Relationships in Admin UI not updating
  • Benchmarking RAM performance - UWP and C#
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Error creating VM instance in Google Compute Engine
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • unknown Exception android
  • Checking variable from a different class in C#
  • How to Embed XSL into XML
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?