Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My objectif is to put attributes via XElement,

This is my code :

*** My Class

**** implementation

I get this result:
XML
<Definition>
            <Definition ID="1" Name="COMPANY" xmlns=""/>                    
  </Definition>



So the result has a duplicate name XElement "Definition",

I want the result like this :

XML
<Definition id="1048591" Name="COMPANY"/>  


What I have tried:

C#
[DataContract]
public class Characeristic
{    
    [DataMember]     
    public XElement Definition = new XElement("Definition");     
}



C#
  Characteristic lstChars = new Characteristic();                         

lstChars.Definition.SetAttributeValue("ID", finalCharac.ID);
lstChars.Definition.SetAttributeValue("Name", finalCharac.Name);
Posted
Updated 12-Nov-19 6:34am
v2
Comments
Richard Deeming 12-Nov-19 10:32am    
How are you populating an instance of your class, and how are you serializing it?
Member 14601214 12-Nov-19 10:38am    
thanks for your reply, I updated my post

1 solution

Try like this:
string TestXml = @"<Definition>
            <Definition ID='1' Name='COMPANY' xmlns=''/>
        </Definition > ";

XElement x = XElement.Parse(TestXml);
var def = x.Element("Definition");
Debug.Print(def.ToString());
 
Share this answer
 

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