Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to change the elements name depending on condition.

e.g
C#
Class Section
{
[XmlAttribute(AttributeName = "name")]
public string Name{get;set;}
[XmlElement(ElementName = "question")]
public List<question>Question{get;set;}
} 

Class Question
{
public int Type{get;set;}
[XmlAttribute(AttributeName = "id")]
public int Id{get;set;}

 [XmlAttribute(AttributeName = "score")]
public int score{get;set;}
}

Question element name from Section depends on Type. if question type is 1 then element name should be yes_no_question and if question type is 2 then elemnet name should be Wh_question

XML
<section name="abc">
      <yes_no_question id="S1.Q1" score="2" />
      <wh_question id="S1.Q2" score="3" />
    </section>



is there any optimise solution for this
Posted
Updated 30-Mar-15 1:29am
v4
Comments
Nathan Minier 30-Mar-15 7:41am    
Based on what I'm seeing of your schema, you might be better served deriving YesNoQuestion : Question and WhQuestion : Question classes and using those to tack your values, rather than trying to hard-code an int translation into a modified XMLSerializer, which is the other option that I can think of.
Maciej Los 30-Mar-15 8:03am    
What you mean by saying: "optimise solution for this"?
ZurdoDev 30-Mar-15 14:22pm    
Write code to do it. Where are you stuck?
Member 10420430 31-Mar-15 1:37am    
I can write by using XMLWriter and all but I want it from directly by xmlserialization

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900