Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have this xml file, and I want to deserialize this xml file to a class which has exactly the same properties like this xml file, I know serialization and deserialization, but I don't know how to map the properties exactly!!! 



XML
<?xml version="1.0" encoding="utf-8" ?> 
- <XML_FILE_FOR_SOME_INPUTS>
- <Printer1>
  <Title>behnoud</Title> 
  <PhoneNumber>22174960</PhoneNumber> 
  <Site>www.sbu.com</Site> 
  <PrinterPort>abc</PrinterPort> 
  </Printer1>
- <Printer2>
  <BillAcceptorPort>abc</BillAcceptorPort> 
  </Printer2>
  </XML_FILE_FOR_SOME_INPUTS>
Posted

C#
public class InputToolsClass
    {
        public InputToolsClass()
        {

        }

        public InputToolsClass(string companytitle, string phonenumber, string site, string printer1, string Printer2 )
        {
            this.Title = title;
            this.PhoneNumber = phonenumber;
            this.Site = site;
            this.Printer1 = printer1;
            this.Printer2 = Printer2 ;
        }

        public string Title { get; set; }
        public string PhoneNumber { get; set; }
        public string Site { get; set; }
        public string Printer1 { get; set; }
        public string Printer2 { get; set; }
}


public void SetAllProperties()
        {
            XmlSerializer ser = new XmlSerializer(typeof(InputToolsClass));

            InputToolsClass inputs;

            using (StreamReader reader = new StreamReader(@"C:\Desktop\Inputs.xml"))
            {

                inputs = (InputToolsClass)ser.Deserialize(reader);
            }
        }
 
Share this answer
 
v2
Comments
[no name] 16-Jul-13 23:37pm    
What is InputToolsClass?
Behno0o0oD 17-Jul-13 1:17am    
It is the class containing the props which I want to map to, from a xml file
[no name] 17-Jul-13 1:26am    
I know that. The answer (to your own question) is incomplete without showing it.
Behno0o0oD 17-Jul-13 1:30am    
public class InputToolsClass
{
public InputToolsClass()
{

}
public InputToolsClass(string companytitle, string phonenumber, string site, string printer1, string Printer2 )
{
this.Title = title;
this.PhoneNumber = phonenumber;
this.Site = site;
this.Printer1 = printer1;
this.Printer2 = Printer2 ;
}

public string Title { get; set; }

public string PhoneNumber { get; set; }

public string Site { get; set; }

public string Printer1 { get; set; }

public string Printer2 { get; set; }
}
C#
XmlRootAttribute root = new XmlRootAttribute("XML_FILE_FOR_SOME_INPUTS");


alose it is essential to add XmlRootAttribute.
 
Share this answer
 

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