xml serialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • virendra.chandra

    #1

    xml serialization

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

    public class testforserializ ation
    {
    public testforserializ ation()
    {
    //
    // 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(t ypeof(testclass ));
    //string _path =
    TextWriter w = new StreamWriter( @"c:\list.xm l" );
    //XmlTextWriter xmlTextWriter = new XmlTextWriter(w ,
    Encoding.UTF8);

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

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


    classes are :

    [XmlRootAttribut e(ElementName=" WildAnimal", IsNullable=fals e)]
    public class testclass
    {
    public testclass()
    {
    //
    // TODO: Add constructor logic here
    //
    }
    private String animalName;
    private String foodTypeCategor y;
    private Boolean isDomesticed;
    private String placeOfExistenc e;
    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 FoodTypeCategor y
    {
    get
    {
    return foodTypeCategor y;
    }
    set
    {
    foodTypeCategor y = value;
    }
    }
    public Boolean IsDomesticed
    {
    get
    {
    return isDomesticed;
    }
    set
    {
    isDomesticed = value;
    }
    }

    public String PlaceOfExistenc e
    {
    get
    {
    return placeOfExistenc e;
    }
    set
    {
    placeOfExistenc e = 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

  • Jon Skeet [C# MVP]

    #2
    Re: xml serialization

    virendra.chandr a <virendra.chand ra@gmail.com> wrote:[color=blue]
    > i wrote a method for writing a xml file. but it's throwing a exception[/color]

    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.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    Working...