实体转xml 并以string输出
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Header>
<MessageReferenceNumber>I9DeoogUJg8ZBfA</MessageReferenceNumber>
<DocumentName>无车承运人电子路单</DocumentName>
<DocumentVersionNumber>2015WCCYR</DocumentVersionNumber>
<SenderCode>9285</SenderCode>
<RecipientCode>9396</RecipientCode>
<MessageSendingDateTime>9396</MessageSendingDateTime>
<MessageFunctionCode>9</MessageFunctionCode>
</Header>
<Body>
<OriginalDocumentNumber>31314534223</OriginalDocumentNumber>
<ShippingNoteNumber>532135</ShippingNoteNumber>
<Carrier>浙江未名物流有限公司</Carrier>
<UnifiedSocialCreditIdentifier>11991000010647510A</UnifiedSocialCreditIdentifier>
<PermitNumber>310112002939</PermitNumber>
<ConsignmentDateTime>20161229145036</ConsignmentDateTime>
<BusinessTypeCode>1002996</BusinessTypeCode>
<DespatchActualDateTime>20161229165036</DespatchActualDateTime>
<GoodsReceiptDateTime>20161231165036</GoodsReceiptDateTime>
<ConsignorInfo>
<Consignor>浙江新华书店</Consignor>
<PersonalIdentityDocument>330102198402124417</PersonalIdentityDocument>
<PlaceOfLoading>浙江省杭州市江干区</PlaceOfLoading>
<CountrySubdivisionCode>330104</CountrySubdivisionCode>
</ConsignorInfo>
<ConsigneeInfo>
<Consignee>台州新华书店</Consignee>
<GoodsReceiptPlace>浙江省台州市椒江区</GoodsReceiptPlace>
<CountrySubdivisionCode>331002</CountrySubdivisionCode>
</ConsigneeInfo>
<PriceInfo>
<TotalMonetaryAmount>430.012</TotalMonetaryAmount>
<Remark>人民币</Remark>
</PriceInfo>
<VehicleInfo>
<LicensePlateTypeCode>01</LicensePlateTypeCode>
<VehicleNumber>浙A32153</VehicleNumber>
<VehicleClassificationCode>H01</VehicleClassificationCode>
<VehicleTonnage>20.00</VehicleTonnage>
<RoadTransportCertificateNumber>330111003790</RoadTransportCertificateNumber>
<TrailerVehiclePlateNumber>浙RR527挂</TrailerVehiclePlateNumber>
<Owner>温州市侯海货运有限公司</Owner>
<PermitNumber>330301000307</PermitNumber>
<Driver>
<NameOfPerson>张三</NameOfPerson>
<QualificationCertificateNumber>431224198708273098</QualificationCertificateNumber>
<TelephoneNumber>15167338765</TelephoneNumber>
</Driver>
<GoodsInfo>
<DescriptionOfGoods>教科书</DescriptionOfGoods>
<CargoTypeClassificationCode>999</CargoTypeClassificationCode>
<GoodsItemGrossWeight>151.333</GoodsItemGrossWeight>
<Cube>11.3333</Cube>
<TotalNumberOfPackages>12</TotalNumberOfPackages>
</GoodsInfo>
</VehicleInfo>
<FreeText>文本</FreeText>
</Body>
</Root>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace test_jiekou.models
{
public class GeneraterXML
{
public Root GetRoot() {
ConsignorInfo consignorinfo = new ConsignorInfo();
consignorinfo.Consignor = "浙江新华书店";
consignorinfo.PersonalIdentityDocument = "330102198402124417";
consignorinfo.PlaceOfLoading = "浙江省杭州市江干区";
consignorinfo.CountrySubdivisionCode = "330104";
ConsigneeInfo consigneeinfo = new ConsigneeInfo();
consigneeinfo.Consignee = "台州新华书店";
consigneeinfo.GoodsReceiptPlace = "浙江省台州市椒江区";
consigneeinfo.CountrySubdivisionCode = "331002";
PriceInfo priceinfo = new PriceInfo();
priceinfo.TotalMonetaryAmount = "430.012";
priceinfo.Remark = "人民币";
Driver driver = new Driver();
driver.NameOfPerson = "张三";
driver.QualificationCertificateNumber = "431224198708273098";
driver.TelephoneNumber = "15167338765";
GoodsInfo goodsinfo = new GoodsInfo();
goodsinfo.DescriptionOfGoods = "教科书";
goodsinfo.CargoTypeClassificationCode = "999";
goodsinfo.GoodsItemGrossWeight = "151.333";
goodsinfo.Cube = "11.3333";
goodsinfo.TotalNumberOfPackagese = "12";
VehicleInfo vehicleinfo = new VehicleInfo();
vehicleinfo.LicensePlateTypeCode = "01";
vehicleinfo.VehicleNumber = "浙A32153";
vehicleinfo.VehicleClassificationCode = "H01";
vehicleinfo.VehicleTonnage = "20.00";
vehicleinfo.RoadTransportCertificateNumber = "330111003790";
vehicleinfo.TrailerVehiclePlateNumber = "浙RR527挂";
vehicleinfo.Owner = "温州市侯海货运有限公司";
vehicleinfo.PermitNumber = "330301000307";
Driver[] arraydriver = new Driver[1];
arraydriver[0] = driver;
vehicleinfo.driver = arraydriver;
GoodsInfo[] arraygoodsinfo = new GoodsInfo[1];
arraygoodsinfo[0] = goodsinfo;
vehicleinfo.goodsinfo = arraygoodsinfo;
Body body = new Body();
body.OriginalDocumentNumber = "31314534223";
body.ShippingNoteNumber = "532135";
body.Carrier = "浙江未名物流有限公司";
body.UnifiedSocialCreditIdentifier = "11991000010647510A";
body.PermitNumber = "310112002939";
body.ConsignmentDateTime = "20161229145036";
body.BusinessTypeCode = "1002996";
body.DespatchActualDateTime = "20161229165036";
body.GoodsReceiptDateTime = "20161231165036";
body.consigneeinfo = consigneeinfo;
body.consignorinfo = consignorinfo;
body.priceinfo = priceinfo;
VehicleInfo[] arrayvehicleinfo = new VehicleInfo[1];
arrayvehicleinfo[0] = vehicleinfo;
body.vehicleinfo = arrayvehicleinfo;
body.FreeText = "文本";
Header header = new Header();
header.MessageReferenceNumber = "I9DeoogUJg8ZBfA";
header.DocumentName = "无车承运人电子路单";
header.DocumentVersionNumber = "2015WCCYR";
header.SenderCod = "9285";
header.RecipientCode = "9396";
header.MessageSendingDateTime = "9396";
header.MessageFunctionCode = "9";
Root root = new Root();
root.header = header;
root.body = body;
return root;
}
public string zhuanXML() {
Root Md_Root = GetRoot();
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration declaration= xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
xmlDoc.AppendChild(declaration);
XmlNode XRoot = xmlDoc.CreateElement("Root");
XmlNode XHeader = xmlDoc.CreateElement("Header");
XmlNode XBody = xmlDoc.CreateElement("Body");
Header header = Md_Root.header;
XmlNode MessageReferenceNumber = xmlDoc.CreateElement("MessageReferenceNumber");
XmlNode DocumentName = xmlDoc.CreateElement("DocumentName");
XmlNode DocumentVersionNumber = xmlDoc.CreateElement("DocumentVersionNumber");
XmlNode SenderCode = xmlDoc.CreateElement("SenderCode");
XmlNode RecipientCode = xmlDoc.CreateElement("RecipientCode");
XmlNode MessageSendingDateTime = xmlDoc.CreateElement("MessageSendingDateTime");
XmlNode MessageFunctionCode = xmlDoc.CreateElement("MessageFunctionCode");
MessageReferenceNumber.InnerText = header.MessageReferenceNumber;
DocumentName.InnerText = header.DocumentName;
DocumentVersionNumber.InnerText = header.DocumentVersionNumber;
SenderCode.InnerText = header.SenderCod;
RecipientCode.InnerText = header.RecipientCode;
MessageSendingDateTime.InnerText = header.MessageSendingDateTime;
MessageFunctionCode.InnerText = header.MessageFunctionCode;
XHeader.AppendChild(MessageReferenceNumber);
XHeader.AppendChild(DocumentName);
XHeader.AppendChild(DocumentVersionNumber);
XHeader.AppendChild(SenderCode);
XHeader.AppendChild(RecipientCode);
XHeader.AppendChild(MessageSendingDateTime);
XHeader.AppendChild(MessageFunctionCode);
Body body = Md_Root.body;
XmlNode OriginalDocumentNumber = xmlDoc.CreateElement("OriginalDocumentNumber");
XmlNode ShippingNoteNumber = xmlDoc.CreateElement("ShippingNoteNumber");
XmlNode Carrier = xmlDoc.CreateElement("Carrier");
XmlNode UnifiedSocialCreditIdentifier = xmlDoc.CreateElement("UnifiedSocialCreditIdentifier");
XmlNode PermitNumber = xmlDoc.CreateElement("PermitNumber");
XmlNode ConsignmentDateTime = xmlDoc.CreateElement("ConsignmentDateTime");
XmlNode BusinessTypeCode = xmlDoc.CreateElement("BusinessTypeCode");
XmlNode DespatchActualDateTime = xmlDoc.CreateElement("DespatchActualDateTime");
XmlNode GoodsReceiptDateTime = xmlDoc.CreateElement("GoodsReceiptDateTime");
OriginalDocumentNumber.InnerText = body.OriginalDocumentNumber;
ShippingNoteNumber.InnerText = body.ShippingNoteNumber;
Carrier.InnerText = body.Carrier;
UnifiedSocialCreditIdentifier.InnerText = body.UnifiedSocialCreditIdentifier;
PermitNumber.InnerText = body.PermitNumber;
ConsignmentDateTime.InnerText = body.ConsignmentDateTime;
BusinessTypeCode.InnerText = body.BusinessTypeCode;
DespatchActualDateTime.InnerText = body.DespatchActualDateTime;
GoodsReceiptDateTime.InnerText = body.GoodsReceiptDateTime;
XBody.AppendChild(OriginalDocumentNumber);
XBody.AppendChild(ShippingNoteNumber);
XBody.AppendChild(Carrier);
XBody.AppendChild(UnifiedSocialCreditIdentifier);
XBody.AppendChild(PermitNumber);
XBody.AppendChild(ConsignmentDateTime);
XBody.AppendChild(BusinessTypeCode);
XBody.AppendChild(DespatchActualDateTime);
XBody.AppendChild(GoodsReceiptDateTime);
ConsignorInfo consignorinfo = body.consignorinfo;
XmlNode Xconsignorinfo = xmlDoc.CreateElement("ConsignorInfo");
XmlNode Consignor = xmlDoc.CreateElement("Consignor");
XmlNode PersonalIdentityDocument = xmlDoc.CreateElement("PersonalIdentityDocument");
XmlNode PlaceOfLoading = xmlDoc.CreateElement("PlaceOfLoading");
XmlNode CountrySubdivisionCode = xmlDoc.CreateElement("CountrySubdivisionCode");
Consignor.InnerText = consignorinfo.Consignor;
PersonalIdentityDocument.InnerText = consignorinfo.PersonalIdentityDocument;
PlaceOfLoading.InnerText = consignorinfo.PlaceOfLoading;
CountrySubdivisionCode.InnerText = consignorinfo.CountrySubdivisionCode;
Xconsignorinfo.AppendChild(Consignor);
Xconsignorinfo.AppendChild(PersonalIdentityDocument);
Xconsignorinfo.AppendChild(PlaceOfLoading);
Xconsignorinfo.AppendChild(CountrySubdivisionCode);
XBody.AppendChild(Xconsignorinfo);
ConsigneeInfo consigneeinfo = body.consigneeinfo;
XmlNode Xconsigneeinfo = xmlDoc.CreateElement("ConsigneeInfo");
XmlNode Consignee = xmlDoc.CreateElement("Consignee");
XmlNode GoodsReceiptPlace = xmlDoc.CreateElement("GoodsReceiptPlace");
XmlNode CountrySubdivisionCode2 = xmlDoc.CreateElement("CountrySubdivisionCode");
Consignee.InnerText = consigneeinfo.Consignee;
GoodsReceiptPlace.InnerText = consigneeinfo.GoodsReceiptPlace;
CountrySubdivisionCode2.InnerText = consigneeinfo.CountrySubdivisionCode;
Xconsigneeinfo.AppendChild(Consignee);
Xconsigneeinfo.AppendChild(GoodsReceiptPlace);
Xconsigneeinfo.AppendChild(CountrySubdivisionCode2);
XBody.AppendChild(Xconsigneeinfo);
PriceInfo priceinfo = body.priceinfo;
XmlNode XPriceInfo = xmlDoc.CreateElement("PriceInfo");
XmlNode TotalMonetaryAmount = xmlDoc.CreateElement("TotalMonetaryAmount");
XmlNode Remark = xmlDoc.CreateElement("Remark");
TotalMonetaryAmount.InnerText = priceinfo.TotalMonetaryAmount;
Remark.InnerText = priceinfo.Remark;
XPriceInfo.AppendChild(TotalMonetaryAmount);
XPriceInfo.AppendChild(Remark);
XBody.AppendChild(XPriceInfo);
foreach (VehicleInfo vehicleinfo in body.vehicleinfo)
{
XmlNode XVehicleInfo = xmlDoc.CreateElement("VehicleInfo");
XmlNode LicensePlateTypeCode = xmlDoc.CreateElement("LicensePlateTypeCode");
XmlNode VehicleNumber = xmlDoc.CreateElement("VehicleNumber");
XmlNode VehicleClassificationCode = xmlDoc.CreateElement("VehicleClassificationCode");
XmlNode VehicleTonnage = xmlDoc.CreateElement("VehicleTonnage");
XmlNode RoadTransportCertificateNumber = xmlDoc.CreateElement("RoadTransportCertificateNumber");
XmlNode TrailerVehiclePlateNumber = xmlDoc.CreateElement("TrailerVehiclePlateNumber");
XmlNode Owner = xmlDoc.CreateElement("Owner");
XmlNode VPermitNumber = xmlDoc.CreateElement("PermitNumber");
LicensePlateTypeCode.InnerText = vehicleinfo.LicensePlateTypeCode;
VehicleNumber.InnerText = vehicleinfo.VehicleNumber;
VehicleClassificationCode.InnerText = vehicleinfo.VehicleClassificationCode;
VehicleTonnage.InnerText = vehicleinfo.VehicleTonnage;
RoadTransportCertificateNumber.InnerText = vehicleinfo.RoadTransportCertificateNumber;
TrailerVehiclePlateNumber.InnerText = vehicleinfo.TrailerVehiclePlateNumber;
Owner.InnerText = vehicleinfo.Owner;
VPermitNumber.InnerText = vehicleinfo.PermitNumber;
XVehicleInfo.AppendChild(LicensePlateTypeCode);
XVehicleInfo.AppendChild(VehicleNumber);
XVehicleInfo.AppendChild(VehicleClassificationCode);
XVehicleInfo.AppendChild(VehicleTonnage);
XVehicleInfo.AppendChild(RoadTransportCertificateNumber);
XVehicleInfo.AppendChild(TrailerVehiclePlateNumber);
XVehicleInfo.AppendChild(Owner);
XVehicleInfo.AppendChild(Owner);
XVehicleInfo.AppendChild(VPermitNumber);
foreach (Driver driver in vehicleinfo.driver)
{
XmlNode Xdriver = xmlDoc.CreateElement("Driver");
XmlNode NameOfPerson = xmlDoc.CreateElement("NameOfPerson");
XmlNode QualificationCertificateNumber = xmlDoc.CreateElement("QualificationCertificateNumber");
XmlNode TelephoneNumber = xmlDoc.CreateElement("TelephoneNumber");
NameOfPerson.InnerText = driver.NameOfPerson;
QualificationCertificateNumber.InnerText = driver.QualificationCertificateNumber;
TelephoneNumber.InnerText = driver.TelephoneNumber;
Xdriver.AppendChild(NameOfPerson);
Xdriver.AppendChild(QualificationCertificateNumber);
Xdriver.AppendChild(TelephoneNumber);
XVehicleInfo.AppendChild(Xdriver);
}
foreach (GoodsInfo goodsinfo in vehicleinfo.goodsinfo)
{
XmlNode Xgoodsinfo = xmlDoc.CreateElement("GoodsInfo");
XmlNode DescriptionOfGoods = xmlDoc.CreateElement("DescriptionOfGoods");
XmlNode CargoTypeClassificationCode = xmlDoc.CreateElement("CargoTypeClassificationCode");
XmlNode GoodsItemGrossWeight = xmlDoc.CreateElement("GoodsItemGrossWeight");
XmlNode Cube = xmlDoc.CreateElement("Cube");
XmlNode TotalNumberOfPackages = xmlDoc.CreateElement("TotalNumberOfPackages");
DescriptionOfGoods.InnerText = goodsinfo.DescriptionOfGoods;
CargoTypeClassificationCode.InnerText = goodsinfo.CargoTypeClassificationCode;
GoodsItemGrossWeight.InnerText = goodsinfo.GoodsItemGrossWeight;
Cube.InnerText = goodsinfo.Cube;
TotalNumberOfPackages.InnerText = goodsinfo.TotalNumberOfPackagese;
Xgoodsinfo.AppendChild(DescriptionOfGoods);
Xgoodsinfo.AppendChild(CargoTypeClassificationCode);
Xgoodsinfo.AppendChild(GoodsItemGrossWeight);
Xgoodsinfo.AppendChild(Cube);
Xgoodsinfo.AppendChild(TotalNumberOfPackages);
XVehicleInfo.AppendChild(Xgoodsinfo);
}
XBody.AppendChild(XVehicleInfo);
}
XmlNode Xfreetext = xmlDoc.CreateElement("FreeText");
Xfreetext.InnerText = body.FreeText;
XBody.AppendChild(Xfreetext);
XRoot.AppendChild(XHeader);
XRoot.AppendChild(XBody);
xmlDoc.AppendChild(XRoot);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
xmlDoc.Save(ms);
System.IO.StreamReader sr = new System.IO.StreamReader(ms, System.Text.Encoding.UTF8);
ms.Position = 0;
string xmlString = sr.ReadToEnd();
return xmlString;
}
}
}
实体转xml 并以string输出的更多相关文章
- C# 实体集合和实体转换成相应的string、XDocument、XElement、XDocument
https://msdn.microsoft.com/zh-cn/library/system.xml.linq.xelement(v=vs.110).aspx XElement.Parse 方法 ( ...
- XML解析之sax解析案例(一)读取contact.xml文件,完整输出文档内容
一.新建Demo2类: import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPar ...
- XML读取(string形式进行读取)
#region 测试XML二进制读取 string strXmlData = "<xml><ToUserName><![CDATA[gh_ef65912f88f ...
- C#中实体集合和实体转换成相应的string、XDocument、XElement
C#中实体集合和实体转换成相应的string.XDocument.XElement public class SimpleXmlConverter { public static string ToX ...
- string 转化xml && xml转化为string
一.使用最原始的javax.xml.parsers,标准的jdk api // 字符串转XML [java] view plaincopyprint? String xmlStr = \". ...
- jdom处理的XML Document 和String 之间的相互转化
package util; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileWriter; ...
- 生成XML文件,通过实体生成XML文件
实体 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xm ...
- spring代码异常捕获到logback logging.config=logback-spring.xml文件中不能输出异常e.printStackTrace
在spring中使用logging.config=logback-spring.xml将日志转存到了文件中.但是代码中的捕获的异常无法用 e.printStackTrace 打印到文件中.使用如下方法 ...
- xml和json格式输出
<?php class Response{ const JSON ='json'; /* * 按综合方式输出通信数据 * @param integer $ ...
随机推荐
- hybrid programming based on python and C/C++
Python/C API Reference Manual¶ https://docs.python.org/3/c-api/index.html Extending and Embedding th ...
- .NET基础之this关键字
1.限定类似名称隐藏的成员 public Employee(string name, string alias) { // Use this to qualify the fields, name a ...
- python习题实例(上)_update18/07/03
用以记录python学习过程中做过的小习题~ ヾ(◍°∇°◍)ノ゙ 1.生成两个列表,分别存放将100以内的偶数&奇数 odd_number=[] even_number=[] for i i ...
- Elasticsearch索引别名、Filtered索引别名、Template
在使用elasticsearch的时候,经常会遇到需要淘汰掉历史数据的场景. 为了方便数据淘汰,并使得数据管理更加灵活,我们经常会以时间为粒度建立索引,例如: 每个月建立一个索引:monthly-20 ...
- appium+java报错之nodejs报错
$ gulp(node:784) fs: re-evaluating native module sources is not supported. If you areusing the grace ...
- 5)协程二(yeild from)
一:yield from说明 python从3.3版本开始使用yield from 替代yield yield from 结构会在内部自动捕获 StopIteration 异常. 这种处理方式与 ...
- kindeditor用法简单介绍(转)
1,首先去官网下载http://www.kindsoft.net/ 2,解压之后如图所示: 由于本人做的是用的是JSP,所以ASP,PHP什么的就用不上了,直接把那些去掉然后将整个文件夹扔进Myecl ...
- nginx 配置白名单
在http 模块 增加 geo $remote_addr $ip_whitelist{ default 0; include white_ip.conf; } 在location 模块 增加 (注意i ...
- java虚拟机内存不足,“Could not create the Java Virtual Machine”问题解决方案
java虚拟机内存不足,"Could not create the Java Virtual Machine"问题解决方案 在运行java程序时,遇到问题"Could n ...
- mysql 安装问题二:mysqld: Can't create directory 'E:\Software\mysql-5.7.24-winx64\data\' (Errcode: 2 - No such file or directory)
原因:my.ini文件中的basedir(设置mysql的安装目录).datadir(设置mysql数据库的数据的存放目录)与MySQL解压后的路径不一致 解决办法: 将basedir=E:\Soft ...