xml序列化时出现错误 [英] Getting error at the time of xml serialization
问题描述
我想按如下所示进行序列化后获取xml
<?xml version ="1.0" encoding ="windows-1252"?>
< OpenShipments xmlns ="x-schema:C:\ UPSLabel \ OpenShipments.xdr">
< OpenShipment ShipmentOption ="" ProcessStatus =""
< ShipTo>
< CompanyOrName> DARMOT Sp. z o.o</CompanyOrName>
<注意> DARMOT Sp. z o.o</Attention>
< Address1> Ojca Damiana Tynieckiego 46</Address1>
< Address2></Address2>
< Address3> Dar 3owo</Address3>
< CountryTerritory> PL</CountryTerritory>
< PostalCode> 76-150</PostalCode>
< CityOrTown> Dar 3owo</CityOrTown>
< StateProvinceCounty></StateProvinceCounty>
<电话> 943143185</电话>
</ShipTo>
< ShipmentInformation>
< ServiceType> UPS标准</ServiceType>
< NumberOfPackages> 1</NumberOfPackages>
< DescriptionOfGoods>再制造的汽车零件</DescriptionOfGoods>
< BillingOption> PP</BillingOption>
</ShipmentInformation>
<包裹>
< PackageType> CP</PackageType>
< Weight> 1</Weight>
< Reference1> OUR:AWP0021</Reference1>
< Reference2>工作#41149</Reference2>
< DeclaredValue>
<金额> 999</金额>
</DeclaredValue>
</包装>
</OpenShipment>
</OpenShipments>
为了从我的班级获得xml序列化之后的上述xml,我编写了几个班级来完成这项工作.
我完整的C#代码是:
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用System.IO;
使用System.Xml;
使用System.Xml.Serialization;
命名空间XML2List
{
公共局部类ShipMain:表格
{
公共ShipMain()
{
InitializeComponent();
}
公共字符串Serialize< T>(T obj)
{
字符串xmlString = null;
试试
{
MemoryStream memoryStream =新的MemoryStream();
XmlSerializer xs =新的XmlSerializer(typeof(T));
XmlTextWriter xmlTextWriter =新的XmlTextWriter(memoryStream,Encoding.UTF8);
xs.Serialize(xmlTextWriter,obj);
memoryStream =(MemoryStream)xmlTextWriter.BaseStream;
xmlString = UTF8ByteArrayToString(memoryStream.ToArray());返回xmlString;
}
catch(异常例外)
{
字符串xx = ex.Message.ToString();
}
返回xmlString;
}
公共字符串UTF8ByteArrayToString(byte []个字符)
{
UTF8Encoding编码=新的UTF8Encoding();
字符串ConstructedString = encoding.GetString(characters);
return(constructedString);
}
私有void ShipMain_Load(对象发送者,EventArgs e)
{
OpenShipments op =新的OpenShipments();
op.xmlns = @"C:\ UPSLabel \ OpenShipments.xdr";
op.OpenShipment.ShipmentOption =";
op.OpenShipment.ProcessStatus =";
op.OpenShipment.ShipTo.CompanyOrName ="DARMOT Sp. z o.o";
op.OpenShipment.ShipTo.Attention ="DARMOT Sp. z o.o";
op.OpenShipment.ShipTo.Address1 ="Ojca Damiana Tynieckiego 46";
op.OpenShipment.ShipTo.Address2 =";
op.OpenShipment.ShipTo.Address3 ="Dar³ owo";
op.OpenShipment.ShipTo.CountryTerritory ="PL";
op.OpenShipment.ShipTo.PostalCode ="76-150&";
op.OpenShipment.ShipTo.CityOrTown ="Dar³ owo";
op.OpenShipment.ShipTo.StateProvinceCounty ="Dar³ owo";
op.OpenShipment.ShipTo.Telephone ="943143185";
op.OpenShipment.ShipmentInformation.ServiceType ="UPS标准";
op.OpenShipment.ShipmentInformation.NumberOfPackages ="1";
op.OpenShipment.ShipmentInformation.DescriptionOfGoods =再制造的汽车零件";
op.OpenShipment.ShipmentInformation.BillingOption ="PP";
op.OpenShipment.Package.PackageType ="CP";
op.OpenShipment.Package.Weight ="1";
op.OpenShipment.Package.Reference1 ="OUR:AWP0021";
op.OpenShipment.Package.Reference2 =作业#41149";
op.OpenShipment.Package.DeclaredValue.Amount ="999";
字符串strRetXML = Serialize(op);
}
}
公共类OpenShipments
{
字符串_xmlns =";
私人OpenShipment _OpenShipment = null;
[XmlAttribute]
公用字符串xmlns
{
得到{return _xmlns; }
设置{_xmlns ="x-schema:" +值; }
}
[XmlElement("OpenShipment"))
公开OpenShipment OpenShipment
{
得到{return _OpenShipment; }
设置{_OpenShipment =值; }
}
公共OpenShipments()
{
OpenShipment =新的OpenShipment();
}
}
公共类OpenShipment
{
字符串_ShipmentOption =";
字符串_ProcessStatus =";
私人ShipTo _ShipTo = null;
私人ShipmentInformation _ShipmentInformation = null;
private Package _Package = null;
公共OpenShipment()
{
_ShipTo = new ShipTo();
_ShipmentInformation = new ShipmentInformation();
_Package = new Package();
}
[XmlElement("ShipTo"))
公共ShipTo ShipTo
{
得到{return _ShipTo; }
设置{_ShipTo = value; }
}
[XmlElement("ShipmentInformation"))
公共ShipmentInformation ShipmentInformation
{
得到{return _ShipmentInformation; }
设置{_ShipmentInformation =值; }
}
[XmlElement("Package"))]
公共包装包装
{
得到{return _Package; }
设置{_Package = value; }
}
[XmlAttribute]
公共字符串ShipmentOption
{
得到{return _ShipmentOption; }
设置{_ShipmentOption = 价值; }
}
[XmlAttribute]
公共字符串ProcessStatus
{
得到{return _ProcessStatus; }
设置{_ProcessStatus =值; }
}
}
公共类ShipTo
{
[XmlText]
公用字符串CompanyOrName {get;放; }
[XmlText]
公共字符串注意{get;放; }
[XmlText]
公共字符串Address1 {get;放; }
[XmlText]
公共字符串Address2 {get;放; }
[XmlText]
公共字符串Address3 {get;放; }
[XmlText]
公共字符串CountryTerritory {get;放; }
[XmlText]
公共字符串PostalCode {get;放; }
[XmlText]
公共字符串CityOrTown {get;放; }
[XmlText]
公共字符串StateProvinceCounty {get;放; }
[XmlText]
公共字符串电话{get;放; }
}
公共类ShipmentInformation
{
[XmlText]
公共字符串ServiceType {放; }
[XmlText]
公共字符串NumberOfPackages {get;放; }
[XmlText]
公共字符串DescriptionOfGoods {get;放; }
[XmlText]
公共字符串BillingOption {get;放; }
}
公共类包
{
private DeclaredValue _DeclaredValue = null;
公共Package()
{
_DeclaredValue = new DeclaredValue();
}
[XmlElement("DeclaredValue"")]
公共DeclaredValue DeclaredValue
{
得到{return _DeclaredValue; }
设置{_DeclaredValue =值; }
}
[XmlText]
公共字符串PackageType {放; }
[XmlText]
公共字符串Weight {get;放; }
[XmlText]
公共字符串Reference1 {get;放; }
[XmlText]
公共字符串Reference2 {get;放; }
}
公共类DeclaredValue
{
[XmlText]
公用字符串Amount {get;放; }
}
}
我编写的上面的代码获得了我在顶部给出的xml输出.当我尝试序列化该类时,我就得到了.什么时候XmlSerializer xs =新的XmlSerializer(typeof(T));该行执行然后发生异常.错误是-
错误,反映了类型'XML2List.OpenShipments'."
我无法检测到为什么在xml序列化时发生错误.
事件我不确定我编写类的方式,该类将正确生成我在顶部显示的所需xml.
所以请有人告诉我如何解决错误并在序列化后获取xml.
还告诉我,在序列化之后,我将以编写代码的方式获得所需的xml输出.
请详细讨论.
i want to get xml after serialization as follows
<?xml version="1.0" encoding="windows-1252"?>
<OpenShipments xmlns="x-schema:C:\UPSLabel\OpenShipments.xdr">
<OpenShipment ShipmentOption="" ProcessStatus="">
<ShipTo>
<CompanyOrName>DARMOT Sp. z o.o</CompanyOrName>
<Attention>DARMOT Sp. z o.o</Attention>
<Address1>Ojca Damiana Tynieckiego 46</Address1>
<Address2></Address2>
<Address3>DarÂ3owo</Address3>
<CountryTerritory>PL</CountryTerritory>
<PostalCode>76-150</PostalCode>
<CityOrTown>DarÂ3owo</CityOrTown>
<StateProvinceCounty></StateProvinceCounty>
<Telephone>943143185</Telephone>
</ShipTo>
<ShipmentInformation>
<ServiceType>UPS Standard</ServiceType>
<NumberOfPackages>1</NumberOfPackages>
<DescriptionOfGoods>Remanufactured auto parts</DescriptionOfGoods>
<BillingOption>PP</BillingOption>
</ShipmentInformation>
<Package>
<PackageType>CP</PackageType>
<Weight>1</Weight>
<Reference1>OUR:AWP0021</Reference1>
<Reference2>Job # 41149</Reference2>
<DeclaredValue>
<Amount>999</Amount>
</DeclaredValue>
</Package>
</OpenShipment>
</OpenShipments>
To get the above xml after xml serialization from my class I wrote few class to get the job done.
My full c# code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace XML2List
{
public partial class ShipMain : Form
{
public ShipMain()
{
InitializeComponent();
}
public string Serialize<T>(T obj)
{
string xmlString = null;
try
{
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(T));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, obj);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
xmlString = UTF8ByteArrayToString(memoryStream.ToArray()); return xmlString;
}
catch (Exception ex)
{
string xx = ex.Message.ToString();
}
return xmlString;
}
public string UTF8ByteArrayToString(byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}
private void ShipMain_Load(object sender, EventArgs e)
{
OpenShipments op = new OpenShipments();
op.xmlns=@"C:\UPSLabel\OpenShipments.xdr";
op.OpenShipment.ShipmentOption = "";
op.OpenShipment.ProcessStatus = "";
op.OpenShipment.ShipTo.CompanyOrName = "DARMOT Sp. z o.o";
op.OpenShipment.ShipTo.Attention = "DARMOT Sp. z o.o";
op.OpenShipment.ShipTo.Address1 = "Ojca Damiana Tynieckiego 46";
op.OpenShipment.ShipTo.Address2 = "";
op.OpenShipment.ShipTo.Address3 = "Dar³owo";
op.OpenShipment.ShipTo.CountryTerritory = "PL";
op.OpenShipment.ShipTo.PostalCode = "76-150";
op.OpenShipment.ShipTo.CityOrTown = "Dar³owo";
op.OpenShipment.ShipTo.StateProvinceCounty = "Dar³owo";
op.OpenShipment.ShipTo.Telephone = "943143185";
op.OpenShipment.ShipmentInformation.ServiceType = "UPS Standard";
op.OpenShipment.ShipmentInformation.NumberOfPackages = "1";
op.OpenShipment.ShipmentInformation.DescriptionOfGoods = "Remanufactured auto parts";
op.OpenShipment.ShipmentInformation.BillingOption = "PP";
op.OpenShipment.Package.PackageType = "CP";
op.OpenShipment.Package.Weight = "1";
op.OpenShipment.Package.Reference1 = "OUR:AWP0021";
op.OpenShipment.Package.Reference2 = "Job # 41149";
op.OpenShipment.Package.DeclaredValue.Amount = "999";
string strRetXML=Serialize(op);
}
}
public class OpenShipments
{
string _xmlns = "";
private OpenShipment _OpenShipment = null;
[XmlAttribute]
public string xmlns
{
get { return _xmlns; }
set { _xmlns = "x-schema:" + value; }
}
[XmlElement("OpenShipment")]
public OpenShipment OpenShipment
{
get { return _OpenShipment; }
set { _OpenShipment = value; }
}
public OpenShipments()
{
OpenShipment = new OpenShipment();
}
}
public class OpenShipment
{
string _ShipmentOption = "";
string _ProcessStatus = "";
private ShipTo _ShipTo = null;
private ShipmentInformation _ShipmentInformation = null;
private Package _Package = null;
public OpenShipment()
{
_ShipTo = new ShipTo();
_ShipmentInformation = new ShipmentInformation();
_Package = new Package();
}
[XmlElement("ShipTo")]
public ShipTo ShipTo
{
get { return _ShipTo; }
set { _ShipTo = value; }
}
[XmlElement("ShipmentInformation")]
public ShipmentInformation ShipmentInformation
{
get { return _ShipmentInformation; }
set { _ShipmentInformation = value; }
}
[XmlElement("Package")]
public Package Package
{
get { return _Package; }
set { _Package = value; }
}
[XmlAttribute]
public string ShipmentOption
{
get { return _ShipmentOption; }
set { _ShipmentOption = value; }
}
[XmlAttribute]
public string ProcessStatus
{
get { return _ProcessStatus; }
set { _ProcessStatus = value; }
}
}
public class ShipTo
{
[XmlText]
public string CompanyOrName { get; set; }
[XmlText]
public string Attention { get; set; }
[XmlText]
public string Address1 { get; set; }
[XmlText]
public string Address2 { get; set; }
[XmlText]
public string Address3 { get; set; }
[XmlText]
public string CountryTerritory { get; set; }
[XmlText]
public string PostalCode { get; set; }
[XmlText]
public string CityOrTown { get; set; }
[XmlText]
public string StateProvinceCounty { get; set; }
[XmlText]
public string Telephone { get; set; }
}
public class ShipmentInformation
{
[XmlText]
public string ServiceType { get; set; }
[XmlText]
public string NumberOfPackages { get; set; }
[XmlText]
public string DescriptionOfGoods { get; set; }
[XmlText]
public string BillingOption { get; set; }
}
public class Package
{
private DeclaredValue _DeclaredValue = null;
public Package()
{
_DeclaredValue = new DeclaredValue();
}
[XmlElement("DeclaredValue")]
public DeclaredValue DeclaredValue
{
get { return _DeclaredValue; }
set { _DeclaredValue = value; }
}
[XmlText]
public string PackageType { get; set; }
[XmlText]
public string Weight { get; set; }
[XmlText]
public string Reference1 { get; set; }
[XmlText]
public string Reference2 { get; set; }
}
public class DeclaredValue
{
[XmlText]
public string Amount { get; set; }
}
}
The above code i wrote to get the xml output which i gave at the top. when i am trying to serialize the class then i am getting. when XmlSerializer xs = new XmlSerializer(typeof(T)); the line execute then exception occur. the error is -- "There was an
error reflecting type 'XML2List.OpenShipments'."
i am not being able to detect why the error occur at the time of xml serialization.
event i am not sure the way i wrote the class that will right to generate my desired xml which i have shown at the top.
so please some one tell me how to fix the error and get the xml after serialization.
also tell me will i get my desired xml output after the serialization the way i wrote the code.
please discuss in detail.
推荐答案
使用可序列化的类属性.例如:
Use Serializable attribute for the classes. Ex:
[Serializable]
public class Test {
}
谢谢
这篇关于xml序列化时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!