开发者

Getting error at the time of xml serialization

开发者 https://www.devze.com 2023-03-03 01:34 出处:网络
i want to get xml after serialization as follows <?xml version=\"1.0\" encoding=\"windows-1252\"?>

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>

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.


You need to check inner exceptions.

xmlns is reserved word and should not be declared as a property on OpenShipments.


UPDATE

According to documentation:

Only one instance of the XmlTextAttribute class can be applied in a class.

You have defined XmlText to all properties of ShipTo hence it cannot serialise. You need to define it on only one property.

0

精彩评论

暂无评论...
验证码 换一张
取 消