SoFunction
Updated on 2025-04-13

xml serialization and deserialization page 2/2



Deserialization is to read the xml file and automatically match its value to the public attributes, methods or fields in the class, which is the inverse operation above. C# Copy Code
webinfo info = new webinfo();    

//Create an XmlSerializer using webinfo class
XmlSerializer ser = new XmlSerializer(typeof(webinfo));    

string path = ("");    

//Stream is used to provide a general view of byte sequences. An XML file will be opened here
Stream file = new FileStream(path, , );    

//Deserialize the byte sequence (stream)
info = (webinfo)(file);    

("Webmaster: " +  + "<br>");
("Site name: " +  + "<br>");
("Domain name:" + );

Output result:


In order to better encapsulate and protect the members and methods of the class, we rewrite the class webinfo into: Collapse and expand C# copy code
public class webinfo    
{    
//Webmaster
    private string userName;    
    public string UserName    
    {    
        get   
        {    
            return userName;    
        }    
        set   
        {    
            userName = value;    
        }    
    }    

//Site name
    private string webName;    
    public string WebName    
    {    
        get   
        {    
            return webName;    
        }    
        set   
        {    
            webName = value;    
        }    
    }    

//Domain name
    private string webUrl;    
    public string WebUrl    
    {    
        get   
        {    
            return webUrl;    
        }    
        set   
        {    
            webUrl = value;    
        }    
    }    
}   
The difference when using is just a small change. For details, please see below: C# Copy Code
webinfo info = new webinfo();        
= "I";--> = "I";
= "Script"; --> = "Script";
= "https://";  -->//Write it yourself
Previous page12Read the full text