Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there,
I'm currently working on a project of WPF application which allows me to configure a XML file. This application has to load a XML file and permit the user to dynamically change the attributes values.

I try to use the XmlSerializer object, but its native behavior doesn't match what I need to do.

The idea is to obtain such an XML file, with a non known number of attributes (the names and the number of attribute comes from an other XML file) :

XML
<Parameters>
<elementOne attributeOne="ValueOne" attributeTwo="ValueTwo" ...attributeN="ValueN">
</elementOne>
</Parameters>


In order to be able to change the number of attributes, I created such a Class, and the idea is to implement IXmlSerializable, but I don't know how, to be able to create a dynamic list of attributes in my Xml file :

C#
public class element : IXmlSerializable
{
    List<attribute> AttributeList;

    public void WriteXml(XmlWriter writer)
    {
          ???
    }
}


Could you tell me, please :
1)If what I want is possible.
2)If I'm on the good way

Thank you.
Posted
Comments
Sinisa Hajnal 14-Dec-15 11:01am    
Is there any special reason why you have to have attributes instead of nodes?
DimitriStPi 14-Dec-15 11:05am    
Hi,
Yes, the reason is that my tool, the configurator, has to be added to a big existing solution all linked to this XML. So the XML I have to be able to configure can't be changed from his original scheme...

Simply use XMlElement class and xmlElement.setattribute() in loop to take care of this....
 
Share this answer
 
Comments
DimitriStPi 15-Dec-15 3:13am    
Hi,
I really wanted to use the XmlSerializer which could have been usefull to make both the serialization/deserialization. BUT after all searches I did, I think having a fixed scheme is a big condition to use XmlSerializer

So I'll take your solution, and to deserialize I think I can use the classes you mentionned. I obtain all the XElements and attributes, and I just have to compare it with the list I contructed with my other Xml which gaves me the available Elements/Attributes.

Thank you !

Dimitri.SP
So... There is a solution !

What we have to do is rather simple..

If our class Attribute looks like the following one :
C#
{
    public class Attribute
    {
        public Attribute(string name, int value)
        {
            this.Name = name;
            this.Value = value;
        }

        public string Name;
        public int Value;
    }
}


The solution could be :

C#
public class element : IXmlSerializable
{
    List<Attribute> AttributeList;
 
    public void WriteXml(XmlWriter writer)
    {
          foreach(var attribute in AttributeList)
          {
            writer.WriteAttributeString(attribute.Name,attribute.Value.ToString());
          }
    }
}



And the result is what i was looking for.
 
Share this answer
 
v2

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