xml读写文件实例

在某个通讯中需要向服务器发送请求xml,格式例子如下:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
  <HEADER>
    <TRANNO>001</TRANNO>   
  </HEADER>
  <BODY>
    <BATCH>Y</BATCH>
    <TASKLOG>
      <APPNO>0000000001</APPNO>
    </TASKLOG>
    <TASKLOG>
      <APPNO>0000000002</APPNO>
    </TASKLOG>
  </BODY>
</ROOT>
服务器反馈信息如下:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
  <HEADER>
    <TRANNO>001</TRANNO>   
  </HEADER>
  <BODY>
    <BATCH>Y</BATCH>
    <TASKLOG>
      <APPNO>0000000001</APPNO>   
      <STATUS>001</STATUS>
    </TASKLOG>
    <TASKLOG>
      <APPNO>0000000002</APPNO>     
      <STATUS>002</STATUS>
    </TASKLOG>
  </BODY>
</ROOT>
1、创建xml节点头的实体类、请求实体类、接收实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XXYYZZ
{
    public abstract class Header
    {
        /// <summary>
        /// 交易号
        /// </summary>
        public string Tranno { get; set; }
        /// <summary>       
        /// 是否批:Y是 N否
        /// </summary>
        public string Batch { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XXYYZZ
{
    public class ReqTasklog : Header
    {
        public List<ReqTasklogBody> ReqTasklogBodyList;
    }

    public class ReqTasklogBody
    {
        /// <summary>
        ///申请编号
        /// </summary>        
        public string Appno { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XXYYZZ
{
    public class RspTasklog : Header
    {
        public List<RspTasklogBody> rspTasklogBodyList;
    }

    public class RspTasklogBody
    {
        /// <summary>
        ///申请编号
        /// </summary>        
        public string Appno { get; set; }
       
        /// <summary>
        /// 状态
        /// </summary>
        public string Status { get; set; }
    }
}
2、创建一个生成xml头节点的类XmlHeader
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XXYYZZ
{
    public class XmlHeader
    {
        /// <summary>
        /// 头节点
        /// </summary>
        /// <param name="model"></param>
        /// <param name="doc"></param>
        /// <returns></returns>
        public XmlElement CreateHeaderNode(Header model,XmlDocument doc)
        {
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(dec);
            XmlElement root = doc.CreateElement("ROOT");
            doc.AppendChild(root);
            //头节点
            XmlElement header = doc.CreateElement("HEADER");
            root.AppendChild(header);
            header.AppendChild(CreateNode(doc, "TRANNO", model.Tranno));
           
            //内容节点
            XmlElement body = doc.CreateElement("BODY");
            root.AppendChild(body);

            return body;
        }
        /// <summary>
        /// 创建节点
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public XmlElement CreateNode(XmlDocument doc, string name, string value)
        {
            XmlElement element = doc.CreateElement(name);
            element.InnerText = value;
            return element;
        }
    }
}
3、创建一个将实体转为XML的类ModelToXml,继承XmlHeader
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XXYYZZ
{
    public class ModelToXml : XmlHeader
    {     
        /// <summary>
        /// 请求查询XX状态
        /// </summary>
        /// <param name="model"></param>
        /// <param name="fullFileName"></param>
        public void ReqTasklogSave(ReqTasklog model, string fullFileName)
        {
            XmlDocument doc = new XmlDocument();
            XmlElement body = CreateHeaderNode(model, doc);
            //是否批量          
            string batchValue = model.ReqTasklogBodyList.Count > 1 ? "Y" : "N";
            body.AppendChild(CreateNode(doc, "BATCH", batchValue));
                       
            foreach (ReqTasklogBody reqTasklogBody in model.ReqTasklogBodyList)
            {
                XmlElement bodyLoanapp = doc.CreateElement("TASKLOG");
                body.AppendChild(bodyLoanapp);
                bodyLoanapp.AppendChild(CreateNode(doc, "APPNO", reqTasklogBody.Appno));
            }
         
            doc.Save(fullFileName);
        }   
    }
}
测试:
  private void button2_Click(object sender, EventArgs e)
        {
            ReqTasklog model = new ReqTasklog()
            {
                Tranno = "001",               
                ReqTasklogBodyList = new List<ReqTasklogBody>()
                {
                    new ReqTasklogBody()
                    {
                        Appno = "0000000001"
                    },
                    new ReqTasklogBody()
                    {
                        Appno = "0000000002"
                    }
                }
            };
            string filename = "ReqTasklog.xml";
            ModelToXml createXml = new ModelToXml();
            createXml.ReqTasklogSave(model, filename);
        }
4、创建一个将xml转为实体的类GetRspTasklog
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace XXYYZZ
{
    public class XmlToModel
    {
        public RspTasklog GetRspTasklog(string filepath)
        {
            RspTasklog model = new RspTasklog();
            XmlDocument xDoc = new XmlDocument();
            using (StreamReader sr = new StreamReader(filepath, Encoding.UTF8))
            {
                xDoc.Load(sr);

                model.Tranno = GetNodeText(xDoc, "//TRANNO");               
                model.Batch = GetNodeText(xDoc, "//BATCH");

                model.rspTasklogBodyList = new List<RspTasklogBody>();
                XmlNodeList nodeList = xDoc.SelectNodes("/ROOT/BODY/TASKLOG");
                foreach (XmlNode node in nodeList)
                {
                    RspTasklogBody body = new RspTasklogBody()
                    {
                         Appno = node["APPNO"].InnerText,                        
                         Status = node["STATUS"].InnerText
                    };
                    model.rspTasklogBodyList.Add(body);
                }              
            }
            return model;
        }

        private string GetNodeText(XmlDocument xDoc, string xpath)
        {
            XmlNode xNode = xDoc.SelectSingleNode(xpath);
            return (xNode != null) ? xNode.InnerText : "";             
        }       
    }
}
测试:
private void button5_Click(object sender, EventArgs e)
        {
            XmlToModel getxml = new XmlToModel();
            RspTasklog model = getxml.GetRspTasklog("RspTasklog.xml");
        }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值