Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
sir ! i have written the code for xml serialization by using soap formattor.
but the code is showing exception "Invalid operation exception".
the code is below:
C#
using System;
using System.Collections;
using System.Xml.Serialization;
using System.Xml;
using System.Text;
using System.IO;
namespace serializablexml
{
    [Serializable]
    public class company
    {
        [SoapAttribute(AttributeName = "company")]
        public string companyname;
        [SoapElement(ElementName = "Corporate Office")]
        public string corporate_office;
        [SoapIgnore]
        public string comID;

        public override string ToString()
        {
            string send;
            send = "\n Comapany name" + this.companyname;
            send += "\n Corporate Office" + this.corporate_office;
            send += "\n Comapny ID:" + this.comID;
            return send;
        }
        public static void Main()
        {
            try
            {
                string filename = "D:\\company.soap";
                XmlTypeMapping mymapping = (new SoapReflectionImporter().ImportTypeMapping(typeof(company)));
                XmlSerializer serializer = new XmlSerializer(mymapping);
                company mycompany = new company();
                mycompany.companyname = "AC.ltd";
                mycompany.comID = "124543";
                mycompany.corporate_office = "Delhi";
                XmlTextWriter writer = new XmlTextWriter(filename, Encoding.ASCII);
                writer.Formatting = Formatting.Indented;
                writer.WriteStartElement("Ccompany Info");
                serializer.Serialize(writer, mycompany);
                writer.WriteEndElement();
                writer.Close();
                XmlTextReader reader = new XmlTextReader(filename);
                reader.ReadStartElement("Company Info");
                mycompany=(company)serializer.Deserialize(reader);
                reader.ReadEndElement();
                reader.Close();
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("=========================================================");
                Console.WriteLine(ex.Data);
                Console.WriteLine("=========================================================");
                Console.WriteLine(ex.Source);
                Console.WriteLine("==========================================================");
                Console.WriteLine(ex.TargetSite);
                Console.WriteLine("=========================================================");
                Console.WriteLine(ex.StackTrace);
                Console.ReadLine();
                
            }
            catch (XmlException ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("=========================================================");
                Console.WriteLine(ex.Data);
                Console.WriteLine("=========================================================");
                Console.WriteLine(ex.Source);
                Console.WriteLine("==========================================================");
                Console.WriteLine(ex.TargetSite);
                Console.WriteLine("=========================================================");
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine("==========================================================");
                Console.WriteLine(ex.LineNumber);
                Console.WriteLine("===========================================================");
                Console.WriteLine(ex.LinePosition);
 
                Console.ReadLine();
            }
        }
    }



}
Posted
Updated 3-Feb-14 8:28am
v2
Comments
Kornfeld Eliyahu Peter 3-Feb-14 14:50pm    
Can you pinpoint the line where the error is?
tusharkaushik 4-Feb-14 11:30am    
XmlTypeMapping mymapping = (new SoapReflectionImporter().ImportTypeMapping(typeof(company)));
Kornfeld Eliyahu Peter 4-Feb-14 12:49pm    
I just spotted - all your Main is INSIDE the company class!!!
Fix the code...
tusharkaushik 5-Feb-14 1:43am    
i fixed the error but no result !
this is the code:
using System;
using System.Collections;
using System.Xml.Serialization;
using System.Xml;
using System.Text;
using System.IO;
namespace serializablexml
{
[Serializable]
public class company
{
[SoapAttribute(AttributeName = "company")]
public string companyname;
[SoapElement(ElementName = "Corporate Office")]
public string corporate_office;
[SoapIgnore]
public string comID;

public override string ToString()
{
string send;
send = "\n Comapany name" + this.companyname;
send += "\n Corporate Office" + this.corporate_office;
send += "\n Comapny ID:" + this.comID;
return send;
}
}
public class program
{
public static void Main()
{
company comp = new company();
comp.ToString();
try
{
string filename = "D:\\company.soap";
XmlTypeMapping mymapping = (new SoapReflectionImporter().ImportTypeMapping(typeof(company)));
XmlSerializer serializer = new XmlSerializer(mymapping);
company mycompany = new company();
mycompany.companyname = "AC.ltd";
mycompany.comID = "124543";
mycompany.corporate_office = "Delhi";
XmlTextWriter writer = new XmlTextWriter(filename, Encoding.ASCII);
writer.Formatting = Formatting.Indented;
writer.WriteStartElement("Ccompany Info");
serializer.Serialize(writer, mycompany);
writer.WriteEndElement();
writer.Close();
XmlTextReader reader = new XmlTextReader(filename);
reader.ReadStartElement("Company Info");
mycompany = (company)serializer.Deserialize(reader);
reader.ReadEndElement();
reader.Close();
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("=========================================================");
Console.WriteLine(ex.Data);
Console.WriteLine("=========================================================");
Console.WriteLine(ex.Source);
Console.WriteLine("==========================================================");
Console.WriteLine(ex.TargetSite);
Console.WriteLine("=========================================================");
Console.WriteLine(ex.StackTrace);
Console.ReadLine();

}
catch (XmlException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("=========================================================");
Console.WriteLine(ex.Data);
Console.WriteLine("=========================================================");
Console.WriteLine(ex.Source);
Console.WriteLine("==========================================================");
Console.WriteLine(ex.TargetSite);
Console.WriteLine("=========================================================");
Console.WriteLine(ex.StackTrace);
Console.WriteLine("==========================================================");
Console.WriteLine(ex.LineNumber);
Console.WriteLine("===========================================================");
Console.WriteLine(ex.LinePosition);

Console.ReadLine();
}

}
}
}
tusharkaushik 5-Feb-14 10:59am    
i cant understand what u want to say!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900