[英]writing data to an xml file using an xsd schema in c#
我是使用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文件。
任何想法/帮助将不胜感激。
我注意到,如果我以最佳方式对此进行了编码,我肯定会注意到。
好。 我弄错了,并且已经解决了这个问题,但是这产生了一个我要解决的新问题,这可能会产生一个关于对象引用(未设置为对象实例)的单独问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.