Problem while converting class object to string using XmlSerializer
-
Hi All, I am trying to convert a class object into a string of xml format using XmlSerializer. My class structure is like this:
namespace test
{
public class Class1
{
public Class1(){}private string strStatus = "Active"; public string Status { get { return strStatus; } set { strStatus = value;} } public Class2 Class2 { get; set; } } public class Class2 { public Class2(){} private string strName = "ABC"; public string Name { get { return strName; } set { strName = value; } } public Class3 Class3 { get; set; } } public class Class3() { ArrayList Element; private int intCount = 0; public Class3() { Element = new ArrayList(); } public int Count { get { return intCount;} } public void Add(Class4 Entry) { Element.Add(Entry); intCount++; } public Class4 this\[int index\] { get { return (Class4)Element\[index\]; } } } public class Class4 { public Class4(){} private int intSub1; private int intSub2; public int Sub1 { get {return intSub1;} set {intSub1 = value;} } public int Sub2 { get {return intSub2;} set {intSub2 = value;} } }
}
Now, i am using this code for converting object of Class1 to a string of xml format:
Class1 objClass1 = new Class1();
Class4 objClass4 = new Class4();
objClass4.Sub1 = 68;
objClass4.Sub2 = 73;
objClass1.Class2.Class3.Add(objClass4);Class4 objClass4Dup = new Class4();
objClass4Dup.Sub1 = 76;
objClass4Dup.Sub2 = 65;
objClass1.Class2.Class3.Add(objClass4Dup);System.Xml.Serialization.XmlSerializer x = new XmlSerializer(objClass1 .GetType());
StringWriter sw = new StringWriter();
x.Serialize(sw, objClass1);
string strxml = sw.ToString();
MessageBox.Show(strxml);In the messagebox, i can see values till Class2 properly in xml format. Then i can see the "Count" of Class3 and its value, but the values of Class4 are not coming into string. Can anybody tell what i am missing or what i need to do to display values of Class4 into string? Thanks, Nagendra.
-
Hi All, I am trying to convert a class object into a string of xml format using XmlSerializer. My class structure is like this:
namespace test
{
public class Class1
{
public Class1(){}private string strStatus = "Active"; public string Status { get { return strStatus; } set { strStatus = value;} } public Class2 Class2 { get; set; } } public class Class2 { public Class2(){} private string strName = "ABC"; public string Name { get { return strName; } set { strName = value; } } public Class3 Class3 { get; set; } } public class Class3() { ArrayList Element; private int intCount = 0; public Class3() { Element = new ArrayList(); } public int Count { get { return intCount;} } public void Add(Class4 Entry) { Element.Add(Entry); intCount++; } public Class4 this\[int index\] { get { return (Class4)Element\[index\]; } } } public class Class4 { public Class4(){} private int intSub1; private int intSub2; public int Sub1 { get {return intSub1;} set {intSub1 = value;} } public int Sub2 { get {return intSub2;} set {intSub2 = value;} } }
}
Now, i am using this code for converting object of Class1 to a string of xml format:
Class1 objClass1 = new Class1();
Class4 objClass4 = new Class4();
objClass4.Sub1 = 68;
objClass4.Sub2 = 73;
objClass1.Class2.Class3.Add(objClass4);Class4 objClass4Dup = new Class4();
objClass4Dup.Sub1 = 76;
objClass4Dup.Sub2 = 65;
objClass1.Class2.Class3.Add(objClass4Dup);System.Xml.Serialization.XmlSerializer x = new XmlSerializer(objClass1 .GetType());
StringWriter sw = new StringWriter();
x.Serialize(sw, objClass1);
string strxml = sw.ToString();
MessageBox.Show(strxml);In the messagebox, i can see values till Class2 properly in xml format. Then i can see the "Count" of Class3 and its value, but the values of Class4 are not coming into string. Can anybody tell what i am missing or what i need to do to display values of Class4 into string? Thanks, Nagendra.
-
Hi,thanks for replying. I am using Indexer for getting the indexed based value of Element.
-
Hi, I added a public property for Element in Class3 as:
public ArrayList GetElement
{
get
{
return Element;
}
}But i am getting an error as "An error occured while creating Xml Document".
-
Hi,thanks for replying. I am using Indexer for getting the indexed based value of Element.
[Serializable]
public class Class1
{
private Class2 _class2;
private string _strStatus = "Active";public string Status { get { return \_strStatus; } set { \_strStatus = value; } } public Class2 Class2 { get { return \_class2; } set { \_class2 = value; } }
}
[Serializable]
public class Class2
{
private Class3 _class3;
private string _strName = "ABC";public string Name { get { return \_strName; } set { \_strName = value; } } public Class3 Class3 { get { return \_class3; } set { \_class3 = value; } }
}
[Serializable]
public class Class3
{
private List<Class4> _element = new List<Class4>();
private int _intCount;public int Count { get { return \_intCount; } } public List<Class4> Element { get { return \_element; } set { \_element = value; } } public Class4 this\[int index\] { get { return (Class4) \_element\[index\]; } } public void Add(Class4 Entry) { \_element.Add(Entry); \_intCount++; }
}
[Serializable]
public class Class4
{
private int _intSub1;
private int _intSub2;public int Sub1 { get { return \_intSub1; } set { \_intSub1 = value; } } public int Sub2 { get { return \_intSub2; } set { \_intSub2 = value; } }
}
-
[Serializable]
public class Class1
{
private Class2 _class2;
private string _strStatus = "Active";public string Status { get { return \_strStatus; } set { \_strStatus = value; } } public Class2 Class2 { get { return \_class2; } set { \_class2 = value; } }
}
[Serializable]
public class Class2
{
private Class3 _class3;
private string _strName = "ABC";public string Name { get { return \_strName; } set { \_strName = value; } } public Class3 Class3 { get { return \_class3; } set { \_class3 = value; } }
}
[Serializable]
public class Class3
{
private List<Class4> _element = new List<Class4>();
private int _intCount;public int Count { get { return \_intCount; } } public List<Class4> Element { get { return \_element; } set { \_element = value; } } public Class4 this\[int index\] { get { return (Class4) \_element\[index\]; } } public void Add(Class4 Entry) { \_element.Add(Entry); \_intCount++; }
}
[Serializable]
public class Class4
{
private int _intSub1;
private int _intSub2;public int Sub1 { get { return \_intSub1; } set { \_intSub1 = value; } } public int Sub2 { get { return \_intSub2; } set { \_intSub2 = value; } }
}
Thanks for replying. I have solved it using the same property. I just gave the xml attribute to the property definning its type and its working now. :)