none
Put attributes via XElement_Without duplicate XElement RRS feed

  • Question

  • My objectif is to put attributes via XElement,

    This is my code :

    *** My Class

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

    *** implementation

    XAttribute attribute = new XAttribute("ID", Dimension.Id);
    XAttribute attributeName = new XAttribute("Name", Dimension.Name);

    I get this result:

     <Definition>
                <Definition ID="1" Name="COMPANY" xmlns=""/>                    
      </Definition>

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

    I want the result like this :

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

    How can I fix it ? Thanks,

    Tuesday, November 12, 2019 3:04 PM

All replies

  • your definition node is a (more or less) complex object (since it got attributes). So you can define it that way:

            [DataContract]
            public class Characteristic
            {
                [DataMember]
                public Definition Definition { get; set; }
            }
    
            public class Definition
            {
                [XmlAttribute]
                public Int32 ID;
    
                [XmlAttribute]
                public String Name;
            }

    to test it I created a simple instance of 'Characteristic' and serialized it:

                var myobj = new Characteristic {Definition = new Definition() {ID = 1, Name = "Company"}};
    
                var ser = new XmlSerializer(typeof(Characteristic));
                var ms = new MemoryStream();
                ser.Serialize(ms, myobj);
    
                ms.Position = 0;
                var sr = new StreamReader(ms);
                var myStr = sr.ReadToEnd();

    Wednesday, November 13, 2019 6:45 AM
  • Hi Thabetidris,

    Thank you for posting here.

    For your question, you want put attributes via XElement without duplicate XElement.

    XAttribute is not necessary, you can use XElement.SetAttributeValue to add an attribute.

    This is a code example, you can refer to it.

    static void Main(string[] args)
    {
        XElement Definition = new XElement("Definition");
        Definition.SetAttributeValue("ID", 2);
        Definition.SetAttributeValue("Name", "COMPANY");
        Definition.Save(@"d:\test.xml");
    }

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 13, 2019 7:11 AM
  • thanks Timon Yang for your reply,

    yes I try this code: 

    Characteristic lstChars = new Characteristic()
    lstChars.Definition.SetAttributeValue("ID", Dimension.ID);
    lstChars.Definition.SetAttributeValue("Name", Dimension.Name);

    but I get always the same problem:

     <Characteristic>
         <Definition>
             <Definition ID="1" Name="COMPANY" xmlns=""/> 
         </Definition>
     </Characteristic>


    • Edited by Thabetidris Wednesday, November 13, 2019 8:43 AM
    Wednesday, November 13, 2019 8:43 AM
  • Thanks a lot Christoph Biegner,

    I try this code:

     [DataContract]
            public class Characteristic
            {
                [DataMember]
                public Definition Definition { get; set; }
            }
    
    [Serializable]
            public class Definition
            {
                [XmlAttribute]
                public int id;
                [XmlAttribute]
                public stringName;
            }


    and it is almost what I want, I get this result

     <Characteristic>
          <Definition>
                 <id>6</id>
                  <name>ACTIVITY</name>
          </Definition>
     </Characteristic>

    And my objectif is to get this result:

     <Characteristic>
    
          <Definition id="6" Name= "ACTIVITY" />        
        
     </Characteristic>

    So How can I fix it? Thanks,

    Wednesday, November 13, 2019 8:48 AM
  • This output is different to mine when I execute my code above. myStr has the value

    <?xml version="1.0"?>
    <Characteristic xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Definition ID="1" Name="Company" />
    </Characteristic>

    how do you serialize your object instance?

    Wednesday, November 13, 2019 9:49 AM
  • Thanks, Ok I see,

    In fact, i didn't add anything, just I make the Definition class as [Serializable]

    So how can fix this for get my result : <Definition id="6" Name= "ACTIVITY" /> ? 

    I serialize to get a string? or any way to serialize it with the class Characterstic ? 

    this is my result by POSTMAN

    Wednesday, November 13, 2019 10:10 AM
  • Hi Thabetidris,

    Thanks for your feedback.

    Try to serialize your object with XmlSerializer.

        class Program
        {
            static void Main(string[] args)
            {
                List<Characteristic> ListFinalDynamic = new List<Characteristic>();
                for (int i = 0; i < 3; i++)
                {
                    var lstChars = new Characteristic()
                    {
                        Definition = new Definition()
                        {
                            id = i,
                            name ="Timon"
                        }
                    };
                    ListFinalDynamic.Add(lstChars);
                }
    
                XmlSerializer xmlSerializer = new XmlSerializer(ListFinalDynamic.GetType());
                TextWriter writer = new StreamWriter(@"d:\test\b.xml");
                xmlSerializer.Serialize(writer, ListFinalDynamic);
    
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
        }
    
        [DataContract]
        public class Characteristic
        {
            [DataMember]
            public Definition Definition { get;set; }
        }
        [Serializable]
        public class Definition
        {
            [XmlAttribute]
            public int id;
            [XmlAttribute]
            public string name;
        }
    

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 14, 2019 8:02 AM
  • XmlAttribute does not work with DataContractSerializer. Refer to the user's original post on this matter.

    OP, please link to the original post when you are creating a follow up post. It saves people time.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, November 15, 2019 3:20 PM
    Moderator