shahroz
shahroz

Reputation: 369

how to create xml file with serialize?

I want create xml file like this:

<channel>
<title>tttt</title>
<link>tttt</link>
<description>tttt</description>
<language>EN</language>
<item>
   <title>t</title>
   <description>d</description>
   <link>ll</link>
   <pubDate>d</pubDate>
</item>
<item>
   <title>t</title>
   <description>d</description>
   <link>ll</link>
   <pubDate>d</pubDate>
</item>
// item count uncertain
</channel>

item count uncertain.my class is:

[System.Serializable]
public class channel
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string Link { get; set; }
    public string language { get; set; }
    public string webMaster { get; set; }
    public string lastBuildDate { get; set; }

    public List<item> listItem
    {
        get;
        set;
    }
}
[Serializable]
public class item
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string pubDate { get; set; }
    public string Link { get; set; }
}

when I serialize with code:

XmlSerializer serializer = new XmlSerializer(typeof(channel));
channel listNR = new channel();
// I fill listNR 
serializer.Serialize(myxml, listNR);

My XML file name is 'myxml' ,myxml:

<channel>
<title>tttt</title>
<link>tttt</link>
<description>tttt</description>
<language>Fa</language>
<listItem>
<item>
   <title>t</title>
   <description>d</description>
   <link>http://www.farsnews.com/13941016001290</link>
   <pubDate>d</pubDate>
</item>
<item>
   <title>t</title>
   <description>d</description>
   <link>http://www.farsnews.com/13941016001290</link>
   <pubDate>d</pubDate>
</item>
<item>
   <title>t</title>
   <description>d</description>
   <link>http://www.farsnews.com/13941016001290</link>
   <pubDate>d</pubDate>
</item>
</listItem>
</channel>

the tag <listItem> appear myxml file.how do fix it? I want <listItem> tag unappear in myxml file. How do it?

Upvotes: 0

Views: 41

Answers (1)

STORM
STORM

Reputation: 4331

Try

[System.Serializable]
public class channel
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string Link { get; set; }
    public string language { get; set; }
    public string webMaster { get; set; }
    public string lastBuildDate { get; set; }

    [XmlElement("item")]
    public List<item> listItem
    {
        get;
        set;
    }
}
[Serializable]
public class item
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string pubDate { get; set; }
    public string Link { get; set; }
}

Upvotes: 1

Related Questions