473,396 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Xml Serialization... Undesirable results

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?
Nov 15 '05 #1
2 1430
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.

--
Greg Ewing [MVP]
http://www.citidc.com
"Trevor" <tr****@spam.com> wrote in message
news:eE**************@tk2msftngp13.phx.gbl...
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?

Nov 15 '05 #2
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" <tr****@spam.com> wrote in message
news:eE**************@tk2msftngp13.phx.gbl...
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?

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DotNetJunkies User | last post by:
I need to send a header from my client. The heade need to have following format: <Header> <id>23</id> </Header> 1. I created a soap header: <System.Xml.Serialization.XmlRoot("id")> _ Public...
0
by: psy000 | last post by:
Hi, I have a C# web service client that talks to a JAVA application sever. I use AXIS to generate the WSDL file, use wsdl.exe to generate proxy stub c# code. When I try to use c# client connect...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
11
by: ajou_king | last post by:
I was running some tests on my Win32 1GHZ processor to see how long it would take to transmit objects numerous times via TCP/IP using C# ..NET Remoting vs the C++ trustworthy method of binary...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
8
by: vinay | last post by:
Hi Guys I want to understand Serialization. What is serialization. When do we need to use?? What are advantages and Disadvantages. Also please diret me to some good sites on serialization....
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
0
by: hic78 | last post by:
Hi, I use a proxy class to invoke web service developed in SOAP/Java: using System.Diagnostics; using System.Xml.Serialization; using System; using System.Web.Services.Protocols; using...
3
by: Zachary Turner | last post by:
Hello, I have a situation where I would like to perform custom serialization and deserialization of an existing .NET framework object (specifically, System.DateTime). Is there a common paradigm...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.