xml读写文件实例

前端之家收集整理的这篇文章主要介绍了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");
        }
原文链接:https://www.f2er.com/xml/300297.html

猜你在找的XML相关文章