使用c#中的XSD模式将数据写入XML文件

本文关键字:XML 文件 数据 中的 XSD 模式 使用 | 更新日期: 2023-09-27 18:16:02

我是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文件。

如有任何建议或帮助,我将不胜感激。

我不确定我是否以最好的方式编写了这个代码。

使用c#中的XSD模式将数据写入XML文件

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