在xml序列化时获取错误

时间:2011-05-03 13:33:21

标签: c# serialization 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>
        <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>

为了从我的类中获取xml序列化后的上述xml,我写了几个类来完成工作。

我的完整c#代码是:

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; }
}
}

上面的代码我写的是获取我在顶部给出的xml输出。当我试图序列化课程,然后我得到。当XmlSerializer xs = new XmlSerializer(typeof(T));行执行然后发生异常。错误是 - “有一个错误反映了类型'XML2List.OpenShipments'。”

我无法检测xml序列化时出错的原因。 事件我不确定我写这个类的方式,它会生成我在顶部显示的所需xml。

所以请有人告诉我如何修复错误并在序列化后获取xml。 还告诉我,在编写代码之后,我将获得所需的xml输出。

请详细讨论。

1 个答案:

答案 0 :(得分:2)

您需要检查内部异常。

xmlns保留字,不应在OpenShipments上声明为属性。


更新

根据documentation

  

只有一个实例   可以应用XmlTextAttribute类   在课堂上。

您已将XmlText定义为ShipTo的所有属性,因此无法序列化。您只需要在一个属性上定义它。