Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a problem and i dont know what i should do about it.

I am using XMLSerialization to write the XML like

XmlSerializer s = new XmlSerializer(typeof(Config));
                TextWriter w = new StreamWriter(ConfigFileName);
                s.Serialize(w, this);
                w.Flush();
                w.Dispose();
                w.Close();

now i want the XML to be like

<Extension>Mp3</Extensions>


and it has to an array.

I am doing like this

[XmlArrayAttribute("Extensions")]
public string[] Extension = new string[0];


and the class like

public class Extension
    {
        public string Extention;
        //public string Extensions;

        //public Extension()
        //{
        //  //  Extensions += Extention;
        //}
        //public Extension(string[] ThisExt)
        //{
        //    Extention += ThisExt;
        //}
    }


Please see the commented part for more on the class.

Now as i write the Xml it gives me the output like

<String>Mp3</String>


I know i have to implement the class to achieve this but when i am trying to write the XML it says it cannot write the xml as only non parametrized constructor need. Is it like serialization can be done only for default constructor and not for the user defined constructor for a class.

This is one issue and the other issue is,

HOW TO DO IT. Please reply. Thanks in Adv.
Posted
Updated 15-Feb-10 2:45am
v5

Ummmm, shouldn't the name of the property in the class be the name of the XML element/attribute you want?
 
Share this answer
 
That is true it has to be like

<pre lang="cs">XmlArrayAttribute(&quot;Extensions&quot;)]
public Extension[] Extension = new string[0];</pre>

and the class has to be like

<pre lang="cs">public class Extension
{
public string Extention;
public string Extensions;

public Extension()
{
// Extensions += Extention;
}
public Extension(string[] ThisExt)
{
Extention += ThisExt;
}
}</pre>

but when i write the XML its comes out to be like

<Extension>
<Extension>MP3</Extension>
</Extension>
<Extension>
<Extension>Dss</Extension>
</Extension>
why it is like this

i need it to be like

<Extension>
<Extension>Mp3</Extension>
<Extension>DSS</Extension>
</Extension >
 
Share this answer
 
v3

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