使用c#中的xsd模式将数据写入xml文件

时间:2013-05-29 05:41:38

标签: c# xml xml-serialization xsd

我是使用xml架构的新手。

我已经设法使用xsd.exe为我的架构创建了一个类,我已设法生成以下代码。

static void Main(string[] args)
{
    var MarketDeclaration_detail = new TransactionsPalletsPalletMarketDeclaration { Set = "", Declaration = "" };
    var MarketDeclaration_class = new TransactionsPalletsPalletMarketDeclaration[] { MarketDeclaration_detail };

    var PalletLine_detail = new TransactionsPalletsPalletPalletLine { ProductLineNo = "", Quantity = "" };
    var PalletLine_class = new TransactionsPalletsPalletPalletLine[] { PalletLine_detail };

    var pallet_detail = new TransactionsPalletsPallet { AccreditDate = "", Available = "", Customer = "", MarketDeclaration = MarketDeclaration_class, MixedPltIndicator = "",
                                                        PalletLine = PalletLine_class, PalletNo = "", PalletType = "", PartPltIndicator = "", PltSubmisProfile = "", SLOC = "", Status = "", StorageType = "",
                                                        Trader = "", TransferInNo = "", TransferOutNo = "", TransferOutOrder = "", TransferOutOrderLine = ""};

    var pallet_class = new TransactionsPalletsPallet[] { pallet_detail };

    var Materials_detail = new TransactionsReferencesMaterialsMaterial { MaterialNo = "", Trader = "", Brand = "", Variety = "", Size = "", Pack = "", GrowingMethod = "", Grade = "", PackTreatment = "", MarketAttribute = "", Count =""};
    var Materials_class = new TransactionsReferencesMaterialsMaterial[] { Materials_detail };

    var ProductLines_detail = new TransactionsReferencesProductLinesProductLine { ColourBand = "", Customer = "", ExpiryDate = "", Grower = "", HarvestDate = "", MaterialNo = "", MgmtArea = "", Owner = "", 
                                                                                  PackDate = "", Packer = "", PackWeight = "", ProblemRisk = "", ProductionProgram = "", ProductLineNo = "", RPIN = "", Run = "", 
                                                                                  SGContract = "", SGContractLine = "", Subdivision = "", SubmisProfile = "", SupplyGroup = "", Trader = ""};
    var ProductLines_class = new TransactionsReferencesProductLinesProductLine[] { ProductLines_detail };

    var TransfersOut_detail = new TransactionsReferencesTransfersOutTransfer { CarrierID = "", ContainerNo = "", DestinationPort = "", DischargePort = "", DispatchDate = "", DispatchTime = "", ETA = "", From = "", LoadPort = "", ReleaseNo = "",
                                                                               SealNo = "", Temperature1 = "", Temperature2 = "", TempLogger = "", To = "", TransferNo = "", TransferRef = "", TruckID = "", Vessel = "", Voyage = ""};
    // var TransfersOut_class = new TransactionsReferencesTransfersOutTransfer[] { TransfersOut_detail };
    TransfersOut();

    var references_detail = new TransactionsReferences { Materials = Materials_class, ProductLines = ProductLines_class, TransfersOut = TransfersOut_class };
    var references_class = new TransactionsReferences[] { references_detail };

    var data1 = new Transactions { Pallets = pallet_class, References = references_class, Serialnumber = "", Timestamp = "" };

    var serializer1 = new XmlSerializer(typeof(Transactions));

    using (var stream1 = new StreamWriter("C:\\Dave\\xml\\djb2.xml"))
        serializer1.Serialize(stream1,data1);
}

public static TransactionsReferencesTransfersOutTransfer[] TransfersOut_class;

public static void TransfersOut()
{
    TransactionsReferencesTransfersOutTransfer[] TransfersOut_detail1 = new TransactionsReferencesTransfersOutTransfer[3];            

    for (int i = 0; i < 3; i++)
    {
       TransfersOut_detail1[i] = new TransactionsReferencesTransfersOutTransfer { CarrierID = Convert.ToString(i) };

       TransfersOut_class = new TransactionsReferencesTransfersOutTransfer[] { TransfersOut_detail1[0], TransfersOut_detail1[1], TransfersOut_detail1[2] };
    }
}

一切正常。

问题/问题是

使用我的TransfersOut方法,我给for循环限制为3,因为我也使用TransferOut_details。但是我不知道在从数据库返回数据之前会有多少。 3用作测试。

我不确定如何对此进行编码,因此我会将所有行返回/写入XML文件。

任何想法/帮助将不胜感激。

我注意到我是否已经以最好的方式对此进行了编码。

1 个答案:

答案 0 :(得分:0)

行。我弄错了,并且已经解决了这个问题,但是这产生了一个我试图解决的新问题,这可能会生成一个单独的问题,对象引用没有设置为对象的实例。