Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement XML Serialization in C #

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how C#implements XML serialization. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

What specific methods are used to implement C#XML serialization techniques? What do we need to pay attention to in the specific operation process? So here is a Demo to show you, hoping to help you understand C#XML serialization technology.

First, we define an object that needs to be serialized:

using System; namespace XMLSerializer { ///summary $>///test class////summary $> public class TestXML { public string name; public string sex; public string age; } }

Then we can use this class to construct objects in our programs, C#XML Serialization.

private void button1_Click( object sender, System.EventArgs e) { TestXML a = new TestXML(); a.name = tbName.Text; a.sex = tbSex.Text; a.age = tbAge.Text; SaveFileDialog of = new SaveFileDialog(); of.Filter = " XML Document| *.XML"; if( of.ShowDialog() == DialogResult.OK ) { try { Stream s = of.OpenFile(); new XmlSerializer( a.GetType() ).Serialize( s, a ); s.Close(); } catch( Exception ex ) { MessageBox.Show( ex.Message ); } } }

C#XML serialization deserializes objects from XML documents

private void button2_Click(object sender, System.EventArgs e) { OpenFileDialog o = new OpenFileDialog(); o.Filter = " XML file| *.XML| all files| *.* "; if( o.ShowDialog() == DialogResult.OK ) { try { XmlSerializer xs = new XmlSerializer( typeof( TestXML ) ); Stream s = o.OpenFile(); TestXML a = xs.Deserialize( s ) as TestXML; tbName.Text = a.name; tbSex.Text = a.sex; tbAge.Text = a.age; s.Close(); } catch( Exception ex ) { MessageBox.Show( ex.Message ); Thank you all for reading! About "C#how to achieve XML serialization" this article is shared here, I hope the above content can be of some help to everyone, so that we can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report