博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xml读写文件实例
阅读量:2120 次
发布时间:2019-04-30

本文共 6070 字,大约阅读时间需要 20 分钟。

在某个通讯中需要向服务器发送请求xml,格式例子如下:
001
Y
0000000001
0000000002
服务器反馈信息如下:
001
Y
0000000001
001
0000000002
002
1、创建xml节点头的实体类、请求实体类、接收实体类
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace XXYYZZ{    public abstract class Header    {        ///         /// 交易号        ///         public string Tranno { get; set; }        ///                /// 是否批:Y是 N否        ///         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
ReqTasklogBodyList; } public class ReqTasklogBody { ///
///申请编号 /// 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
rspTasklogBodyList; } public class RspTasklogBody { ///
///申请编号 /// public string Appno { get; set; } ///
/// 状态 /// 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    {        ///         /// 头节点        ///         ///         ///         /// 
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; } /// /// 创建节点 /// /// /// /// ///
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    {             ///         /// 请求查询XX状态        ///         ///         ///         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
() { 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
(); 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");        }
 

转载地址:http://ghzrf.baihongyu.com/

你可能感兴趣的文章
ActiveMQ中Session设置的相关理解
查看>>
Linux Python 2.7.15
查看>>
Nexus配置Linux Yum Repository
查看>>
Nexus Python pip Repository
查看>>
Linux Mysql 8.0.1
查看>>
Python pymqi 连接 IBM MQ
查看>>
JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof 详解
查看>>
Java - JVM TLAB、对象在内存中安置顺序、垃圾收集、回收算法
查看>>
转: 关于Linux与JVM的内存关系分析
查看>>
(转)Java 程序员必备的高效 Intellij IDEA 插件
查看>>
局域网(内网)docker安装及代理访问
查看>>
软考 英语学习
查看>>
maven 文件上传到远程服务器目录
查看>>
shell 脚本免密远程访问
查看>>
Linux平台Oracle多个实例启动说明
查看>>
在LINUX平台上手动创建数据库(oracle 10g)(在一个oracle服务器上启动两个实例)
查看>>
Oracle 10g 下载地址
查看>>
Linux 下 新增Oracle10g 实例
查看>>
LRM-00123 ORA-01078
查看>>
ORA-01102: cannot mount database in EXCLUSIVE mode
查看>>