xml操作学习

1.OpDetail.cs
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace WindowsApplication1
ExpandedBlockStart.gifContractedBlock.gif
{
    [XmlRoot(
"opDetail")]
    
public class OpDetail
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private List<RecordInfo> _recordInfo = new List<RecordInfo>();

        [XmlElement(
"recordInfo")]
        
public List<RecordInfo> RecordInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _recordInfo; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _recordInfo = value; }
        }

    }

}

2.RecordInfo.cs
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace WindowsApplication1
ExpandedBlockStart.gifContractedBlock.gif
{
    [XmlType(
"recordInfo")]
    
public class RecordInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private string _fieldChName;
        
private string _fieldEnName;
        
private List<string> _fieldContent=new List<string>();
        
private List<string> _fieldDesc = new List<string>();
        
private List<ListFieldInfo> _info = new List<ListFieldInfo>();

        [XmlElement(
"fieldChName")]
        
public string FieldChName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _fieldChName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _fieldChName = value; }
        }

        [XmlElement(
"fieldEnName")]
        
public string FieldEnName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _fieldEnName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _fieldEnName = value; }
        }


        [XmlElement(
"fieldContent")]
        
public List<string> FieldContent
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _fieldContent; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _fieldContent = value; }
        }

        [XmlElement(
"fieldDesc")]
        
public List<string> FieldDesc
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _fieldDesc; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _fieldDesc = value; }
        }


        [XmlArray(
"ListInfo")]
        [XmlArrayItem(
"ListFieldInfo")]
        
public List<ListFieldInfo> Info
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _info; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _info = value; }
        }

    }

}

3.ListFieldInfo.cs
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace WindowsApplication1
ExpandedBlockStart.gifContractedBlock.gif
{
    [XmlType(
"ListFieldInfo")]
    
public class ListFieldInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private string _listFieldChName;
        [XmlElement(
"fieldChName")]
        
public string ListFieldChName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _listFieldChName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _listFieldChName = value; }
        }

        
private string _listFieldEnName;
        [XmlElement(
"fieldEnName")]
        
public string ListFieldEnName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _listFieldEnName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _listFieldEnName = value; }
        }

        
private List<string> _listFieldContent=new List<string>();
        [XmlElement(
"fieldContent")]
        
public List<string> ListFieldContent
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _listFieldContent; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _listFieldContent = value; }
        }

        
private List<string> _listFieldDesc=new List<string>();
        [XmlElement(
"fieldDesc")]
        
public List<string> ListFieldDesc
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _listFieldDesc; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _listFieldDesc = value; }
        }

    }

}

4.button1_Click
ContractedBlock.gif ExpandedBlockStart.gif Code
  private void button1_Click(object sender, EventArgs e)
        {
            XmlSerializer mySerializer 
= new XmlSerializer(typeof(OpDetail));
            TextWriter myWriter 
= new StreamWriter("myFileName.xml");

            OpDetail detail 
= new OpDetail();

            RecordInfo record1 
= new RecordInfo();
            record1.FieldChName 
= "撤销点是否撤销";
            record1.FieldEnName 
= "wetherDisconnet";
            record1.FieldContent.Add(
"");
            record1.FieldDesc.Add(
"desc1");
            record1.FieldDesc.Add(
"desc2");


            ListFieldInfo field1 
= new ListFieldInfo();
            field1.ListFieldChName 
= "中国";
            field1.ListFieldEnName 
= "china";
            field1.ListFieldContent.Add(
"field1Content1");
            field1.ListFieldContent.Add(
"field1Content2");
            field1.ListFieldDesc.Add(
"field1Desc1");
            field1.ListFieldDesc.Add(
"field1Desc2");

            ListFieldInfo field2 
= new ListFieldInfo();
            field2.ListFieldChName 
= "美国";
            field2.ListFieldEnName 
= "Amercia";


            ListFieldInfo field3 
= new ListFieldInfo();
            field3.ListFieldChName 
= "日本";
            field3.ListFieldEnName 
= "Japen";
            field3.ListFieldContent.Add(
"field3Content3");

            record1.Info.Add(field1);
            record1.Info.Add(field2);
            record1.Info.Add(field3);

            detail.RecordInfo.Add(record1);
            mySerializer.Serialize(myWriter, detail);
            myWriter.Close();

            richTextBox1.Text 
= File.ReadAllText("myFileName.xml");
        }

转载于:https://www.cnblogs.com/liuke209/archive/2009/07/25/1530635.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值