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
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