Click here to Skip to main content
14,920,188 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I have a problem in writing the xml using the XmlSerializer.
The problem is with this XmlArrayAttribute
C#
[XmlArrayAttribute("Extensions")]
public Extension[] Extensions = { new Extension(new string[10]) };

and the class for this is like
C#
public class Extension
{
    //[XmlElement("Extention")]
    public string[] Extention;

    public Extension()
    {
    }

    public Extension(string[] ThisExt)
    {
        Extention = new string[ThisExt.Length];
        for (int i = 0; i <= ThisExt.Length - 1; i++)
        {
            Extention[i] = ThisExt[i];
        }
    }
}

but it is giving the xml output as
XML
<Extensions>
  <Extension>
    <Extention>
      <string xsi:nil="true" />
      <string xsi:nil="true" />
      <string xsi:nil="true" />
      <string xsi:nil="true" />
      <string xsi:nil="true" />
      <string xsi:nil="true" />
      <string xsi:nil="true" />
      <string xsi:nil="true" />
      <string xsi:nil="true" />
      <string xsi:nil="true" />
    </Extention>
  </Extension>
</Extensions>

it writes the xml using
C#
XmlSerializer s = new XmlSerializer(typeof(Config));
TextWriter w = new StreamWriter(ConfigFileName);
s.Serialize(w, this);
w.Flush();
w.Dispose();
w.Close();

but I don't want it in that way, I want it in
XML
<Extention>DSS</Extention>

and also I am reading the xml using
C#
XmlSerializer s = new XmlSerializer(typeof(Config));
TextReader r = new StreamReader(ConfigFileName);
Config MyConfig;
MyConfig = (Config)s.Deserialize(r);
r.Dispose();
r.Close();

so when ever I am reading the xml and fetching the data in the text box it always gives string.stings[]. I don't know how to do it. Please help.
Posted
Updated 10-Feb-10 4:37am
v5

1 solution

So is your problem that you need
<Extention>DSS</Extention>

instead of
<string xsi:nil="true" />

For this you should use:
[XmlArrayItem("Extention")]
public string[] Extention;

You still need to name the list container as something though.

If this does not answer your question, please make a simpler example just using

C#
var w = new System.IO.StringWriter();
s.Serialize(w, new Foo());

and having a more meaningful names: extension - extension -extention is quite hard to follow :) .
   

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