Xml Serialization... Undesirable results

T

Trevor

I have created a class Newsgroup which allows itself to be serialized using
Xml serialization attributes.


[Serializable]
[XmlRoot("Newsgroup")]
public class Newsgroup
{
[XmlAttribute("Name")]
public string Name;
[XmlIgnore]
public int LastArticle;
[XmlIgnore]
public int FirstArticle;
[XmlIgnore]
public bool PostingAllowed;
[XmlIgnore]
public int EstimatedArticles;

public Newsgroup()
{
}
public Newsgroup(string response)
{
}
}

I serialize the contents of an ArrayList of Newsgroup objects to a file
"C:\\news.xml".


ArrayList list = client.GetNewsgroupList();
TextWriter tw = new StreamWriter("C:\\news.xml");
XmlSerializer xs = new XmlSerializer(typeof(Newsgroup));
foreach (Newsgroup newsgroup in list)
xs.Serialize(tw, newsgroup);
tw.Close();


This works fine, but the file is much larger than I would like. A sample
line from this generated XML file looks like so:

<Newsgroup xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Name="microsoft.public.access" /><?xml version="1.0" encoding="utf-8"?>

What do I have to do to get each line look like this:
<Newsgroup Name="microsoft.public.access">


I am looking for a way to skip all of these "xmlns:xsd" & "xmlns:xsi"
attributes. I would also like to avoid putting so many <?xml version> tags
in the file, there is one at the beginning of the document already. I do
not know XML very well, is there something I am missing?
 
G

Greg Ewing [MVP]

Trevor, first off, you see a lot of <?xml> tags because you are serializing
each individual element in your area separately and appending them all to
the same file. How about Serializing your array list directly? You might
need to use the XmlInclude class to tell the XmlSerializer how to serialize
your ArrayList.
 
J

Jacob

Do you have a class that exposes the ArrayList as a property? It's funny
you mention serializing newsgroup names because I'm working on a program
that does exactly the same thing. My approach was to make a public property
of the Arraylist of newsgroup objects like so:

public class AllNewsInformation
{
private ArrayList newsgroups;

public AllNewsInformation()
{
newsgroups = new ArrayList();
}

[XmlArray(ElementName = "Newsgroups")]
public ArrayList Newsgroups
{
get { return newsgroups; }
}

//....
}




and then for your serializer try:




private void SaveAllNewsInformation(AllNewsInformation news)
{

// If a file with the same name already exists, delete it.
FileInfo fileInfo = new FileInfo("AllNewsInformation.xml");
if(fileInfo.Exists == true)
fileInfo.Delete();

// Create a serializer and save...
FileStream stream = new FileStream(fileInfo.FullName, FileMode.Create);
XmlSerializer serializer = new XmlSerializer(news.GetType());
serializer.Serialize(stream, news);
stream.Close();

}




Then you should be able to remove all the Xml Attributes from you Newsgroup
class except the ones you want to ignore.


Hope this helps,
Jacob


P.S. - This still wont change the fact that if you have 55,000 or so
newsgroups on your news server, that it won't be a big file.



Trevor said:
I have created a class Newsgroup which allows itself to be serialized using
Xml serialization attributes.


[Serializable]
[XmlRoot("Newsgroup")]
public class Newsgroup
{
[XmlAttribute("Name")]
public string Name;
[XmlIgnore]
public int LastArticle;
[XmlIgnore]
public int FirstArticle;
[XmlIgnore]
public bool PostingAllowed;
[XmlIgnore]
public int EstimatedArticles;

public Newsgroup()
{
}
public Newsgroup(string response)
{
}
}

I serialize the contents of an ArrayList of Newsgroup objects to a file
"C:\\news.xml".


ArrayList list = client.GetNewsgroupList();
TextWriter tw = new StreamWriter("C:\\news.xml");
XmlSerializer xs = new XmlSerializer(typeof(Newsgroup));
foreach (Newsgroup newsgroup in list)
xs.Serialize(tw, newsgroup);
tw.Close();


This works fine, but the file is much larger than I would like. A sample
line from this generated XML file looks like so:

<Newsgroup xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Name="microsoft.public.access" /><?xml version="1.0" encoding="utf-8"?>

What do I have to do to get each line look like this:
<Newsgroup Name="microsoft.public.access">


I am looking for a way to skip all of these "xmlns:xsd" & "xmlns:xsi"
attributes. I would also like to avoid putting so many <?xml version> tags
in the file, there is one at the beginning of the document already. I do
not know XML very well, is there something I am missing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top