xml serialization

V

virendra.chandra

hi,
i wrote a method for writing a xml file. but it's throwing a exception
my files are like this

public class testforserialization
{
public testforserialization()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// Method to convert a custom Object to XML string
/// </summary>
/// <param name="pObject">Object that is to be serialized to
XML</param>
/// <returns>XML string</returns>
public void SerializeObject(Object pObject)
{
try
{
String XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(testclass));
//string _path =
TextWriter w = new StreamWriter( @"c:\list.xml" );
//XmlTextWriter xmlTextWriter = new XmlTextWriter(w,
Encoding.UTF8);

xs.Serialize(w, pObject);
w.Close();
//XmlizedString = UTF8ByteArrayToString(w.);

}
catch (Exception e)
{
throw new Exception(e.Message.ToString());
}
}
}


classes are :

[XmlRootAttribute(ElementName="WildAnimal", IsNullable=false)]
public class testclass
{
public testclass()
{
//
// TODO: Add constructor logic here
//
}
private String animalName;
private String foodTypeCategory;
private Boolean isDomesticed;
private String placeOfExistence;
private Int32 length;
private Int32 height;
private Int32 weight;
private IList childclass;//it has collection of testclass2
intantiate


public String AnimalName
{
get
{
return animalName;
}
set
{
animalName = value;
}
}

public String FoodTypeCategory
{
get
{
return foodTypeCategory;
}
set
{
foodTypeCategory = value;
}
}
public Boolean IsDomesticed
{
get
{
return isDomesticed;
}
set
{
isDomesticed = value;
}
}

public String PlaceOfExistence
{
get
{
return placeOfExistence;
}
set
{
placeOfExistence = value;
}
}

public Int32 Length
{
get
{
return length;
}
set
{
length = value;
}
}

public Int32 Height
{
get
{
return height;
}
set
{
height = value;
}
}

public Int32 Weight
{
get
{
return weight;
}
set
{
weight = value;
}
}

public IList ChildClass
{
get
{
return childclass;
}
set
{
childclass = value;
}
}

}



public class testclass2
{
public testclass2()
{
//
// TODO: Add constructor logic here
//
}
private string _name;
private string _location;

public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string Location
{
get
{
return _location;
}
set
{
_location = value;
}
}
}



if i am taking class array instead of Ilist . like this

private testclass2[] ChildClass;

then i am getting proper xml out put.
but in above scenario it's throwing exception.

is there any solution to work on this scenario. because any how i will
have to work on this scenario. otherwise i will have to change many
places and also i will get many different problems.

so pls any body give proper solution. thanks in advance

regards,
virendra
 
J

Jon Skeet [C# MVP]

virendra.chandra said:
i wrote a method for writing a xml file. but it's throwing a exception

When you're confused about why you're getting an exception, it's
*always, always* worth telling us what exception you're getting.

Now, you've posted a lot of code, but unfortunately not how you're
calling it. (There's also almost certainly rather more than you need -
I doubt whether you need all those properties, for instance.)

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 

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