本文共 6070 字,大约阅读时间需要 20 分钟。
在某个通讯中需要向服务器发送请求xml,格式例子如下:服务器反馈信息如下:001 Y 0000000001 0000000002
1、创建xml节点头的实体类、请求实体类、接收实体类001 Y 0000000001 001 0000000002 002
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 ListReqTasklogBodyList; } 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 List2、创建一个生成xml头节点的类XmlHeaderrspTasklogBodyList; } public class RspTasklogBody { /// ///申请编号 /// public string Appno { get; set; } ////// 状态 /// public string Status { get; set; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;namespace XXYYZZ{ public class XmlHeader { ///3、创建一个将实体转为XML的类ModelToXml,继承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; } }}
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 List4、创建一个将xml转为实体的类GetRspTasklog() { new ReqTasklogBody() { Appno = "0000000001" }, new ReqTasklogBody() { Appno = "0000000002" } } }; string filename = "ReqTasklog.xml"; ModelToXml createXml = new ModelToXml(); createXml.ReqTasklogSave(model, filename); }
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/