Adding information to an array within a class - c#

I have converted some xml into classes using xsd and now I am having an issue getting some data into the array that exists in the class to add a list of items. I am trying to get the info into OrderItemsItem.
A bit stumped, any help appreciated (not really keen on making any changes to the classes if I can get away with it, convert to List<> etc) .
This is the code to add the info:
OrderItemsItem orderItemsItem = new OrderItemsItem();
orderItemsItem.CostCentre = "sfsdf";
orderItemsItem.DeliveryDate = "2014-01-05";
orderItemsItem.Fund = "G";
orderItemsItem.ExternalLineRef = "1";
orderItemsItem.ItemName = "dfss";
orderItemsItem.LineNo = "1";
orderItemsItem.ProdId = "dfsf";
orderItemsItem.Project = "";
orderItemsItem.QuantityOrdered = "2";
orderItemsItem.UnitCost = "10";
Order order = new Order();
OrderItemsItem [] items = {orderItemsItem};
order.Items.Item = items;
The Error I am receiving is happening on -> order.Items.Item = items;:
System.NullReferenceException: Object reference not set to an instance
of an object.
This is the Class:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18444
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XXXXXXXXX", IsNullable=false)]
public partial class Order {
private string referenceField;
private string notesField;
private string orderDateField;
private string statusField;
private OrderItems itemsField;
private OrderBuyerDetails buyerDetailsField;
/// <remarks/>
public string Reference {
get {
return this.referenceField;
}
set {
this.referenceField = value;
}
}
/// <remarks/>
public string Notes {
get {
return this.notesField;
}
set {
this.notesField = value;
}
}
/// <remarks/>
public string OrderDate {
get {
return this.orderDateField;
}
set {
this.orderDateField = value;
}
}
/// <remarks/>
public string Status {
get {
return this.statusField;
}
set {
this.statusField = value;
}
}
/// <remarks/>
public OrderItems Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
/// <remarks/>
public OrderBuyerDetails BuyerDetails {
get {
return this.buyerDetailsField;
}
set {
this.buyerDetailsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderItems {
private OrderItemsItem[] itemField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Item")]
public OrderItemsItem[] Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderItemsItem {
private string lineNoField;
private string externalLineRefField;
private string prodIdField;
private string itemNameField;
private string quantityOrderedField;
private string unitCostField;
private string deliveryDateField;
private string costCentreField;
private string projectField;
private string fundField;
/// <remarks/>
public string LineNo {
get {
return this.lineNoField;
}
set {
this.lineNoField = value;
}
}
/// <remarks/>
public string ExternalLineRef {
get {
return this.externalLineRefField;
}
set {
this.externalLineRefField = value;
}
}
/// <remarks/>
public string ProdId {
get {
return this.prodIdField;
}
set {
this.prodIdField = value;
}
}
/// <remarks/>
public string ItemName {
get {
return this.itemNameField;
}
set {
this.itemNameField = value;
}
}
/// <remarks/>
public string QuantityOrdered {
get {
return this.quantityOrderedField;
}
set {
this.quantityOrderedField = value;
}
}
/// <remarks/>
public string UnitCost {
get {
return this.unitCostField;
}
set {
this.unitCostField = value;
}
}
/// <remarks/>
public string DeliveryDate {
get {
return this.deliveryDateField;
}
set {
this.deliveryDateField = value;
}
}
/// <remarks/>
public string CostCentre {
get {
return this.costCentreField;
}
set {
this.costCentreField = value;
}
}
/// <remarks/>
public string Project {
get {
return this.projectField;
}
set {
this.projectField = value;
}
}
/// <remarks/>
public string Fund {
get {
return this.fundField;
}
set {
this.fundField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderBuyerDetails {
private string nameField;
private string emailField;
private OrderBuyerDetailsBillingAddress billingAddressField;
/// <remarks/>
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
public string Email {
get {
return this.emailField;
}
set {
this.emailField = value;
}
}
/// <remarks/>
public OrderBuyerDetailsBillingAddress BillingAddress {
get {
return this.billingAddressField;
}
set {
this.billingAddressField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderBuyerDetailsBillingAddress {
private string nameField;
private string address1Field;
private string address2Field;
private string address3Field;
private string placeField;
private string countyField;
private string postCodeField;
private string countryField;
private string emailField;
/// <remarks/>
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
public string Address1 {
get {
return this.address1Field;
}
set {
this.address1Field = value;
}
}
/// <remarks/>
public string Address2 {
get {
return this.address2Field;
}
set {
this.address2Field = value;
}
}
/// <remarks/>
public string Address3 {
get {
return this.address3Field;
}
set {
this.address3Field = value;
}
}
/// <remarks/>
public string Place {
get {
return this.placeField;
}
set {
this.placeField = value;
}
}
/// <remarks/>
public string County {
get {
return this.countyField;
}
set {
this.countyField = value;
}
}
/// <remarks/>
public string PostCode {
get {
return this.postCodeField;
}
set {
this.postCodeField = value;
}
}
/// <remarks/>
public string Country {
get {
return this.countryField;
}
set {
this.countryField = value;
}
}
/// <remarks/>
public string Email {
get {
return this.emailField;
}
set {
this.emailField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XXXXXXXXX", IsNullable=false)]
public partial class Post_Printondemand_Create_Full_Order_DataSet {
private Order[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Order")]
public Order[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
Thank you

order.Items.Item = items
The error must be that some order among your orders have its Items collection null (and thus making it not possible to access its "Item" property).

My suspicion is that in your example order.Items is null - you will need to attach a debugger and examine the variables yourself to determine the root cause of your null ref. Ensure that the array is not null before working with it.
in your example you're overriding an element of an array. This will either replace the existing element (if it exists) or (I believe) throw an IndexOutOfBoundsException if it doesn't.
Arrays are generally for fixed lengths of data, ones which you don't append/remove data from frequently. Lists are a better collection to use as they have Add() and Remove() functions for specifically this reason.
It is possible to "append" to C# arrays
order.Items = Order.Items.Union(new Item[]{myNewItem}));
However it's not very nice and creates a new array by merging the old ones! My advice... make the collection a list!

Related

Manually populate object generated fom xsd in C#

Sorry, new to all this...
I have an XSD file which I used to create classes in C# using XSD.exe.
The challenge now is trying to figure out how to manually populate the object so I can then serialise to XML (I won't have a source XML file so I need to manually set the properties via an alternate source).
Below is a sample classes generated from the XSD file. I'm able to set the filesummary and debtor information but no idea how to set the debtorcontact information.
<?xml version="1.0" encoding="utf-8"?>
<FileSummary FileDate="1900-01-01T01:01:01+08:00" OpeningBalance="1" Invoices="1" DebitNotes="1" CreditNotes="1" CashReceipts="1" OtherPositive="1" OtherNegative="1" ClosingBalance="1">
<Debtor DebtorId="DebtorId2" DebtorName="DebtorName2" ExcludedDebtor="ExcludedDebtor2" CreditLimit="-79228162514264337593543950335" DebtorStatus="DebtorStatus2" TradingTerms="TradingTerms2" OverduePeriod="4294967295" HeadOfficeCode="HeadOfficeCode2" HeadOfficeName="HeadOfficeName2" DebtorCountry="DebtorCountry2" ABN="ABN2" ARBN="ARBN2" ACN="ACN2" LegalName="LegalName2" MainTradingName="MainTradingName2" OtherTradingName="OtherTradingName2">
<DebtorContact Surname="Surname4" FirstName="FirstName4" OtherNames="OtherNames4" Phone="Phone4" Mobile="Mobile4" Fax="Fax4" Email="Email4" Salutation="Salutation4" JobTitle="JobTitle4" ContactAddress="ContactAddress4" ContactSuburb="ContactSuburb4" ContactCountry="ContactCountry4" ContactState="ContactState4" ContactPostcode="ContactPostcode4" />
<DebtorContact Surname="Surname5" FirstName="FirstName5" OtherNames="OtherNames5" Phone="Phone5" Mobile="Mobile5" Fax="Fax5" Email="Email5" Salutation="Salutation5" JobTitle="JobTitle5" ContactAddress="ContactAddress5" ContactSuburb="ContactSuburb5" ContactCountry="ContactCountry5" ContactState="ContactState5" ContactPostcode="ContactPostcode5" />
</Debtor>
</FileSummary>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.6.1055.0.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class FileSummary {
private FileSummaryDebtor[] debtorField;
private System.DateTime fileDateField;
private decimal openingBalanceField;
private decimal invoicesField;
private decimal debitNotesField;
private decimal creditNotesField;
private decimal cashReceiptsField;
private decimal otherPositiveField;
private decimal otherNegativeField;
private decimal closingBalanceField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Debtor")]
public FileSummaryDebtor[] Debtor {
get {
return this.debtorField;
}
set {
this.debtorField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime FileDate {
get {
return this.fileDateField;
}
set {
this.fileDateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal OpeningBalance {
get {
return this.openingBalanceField;
}
set {
this.openingBalanceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal Invoices {
get {
return this.invoicesField;
}
set {
this.invoicesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal DebitNotes {
get {
return this.debitNotesField;
}
set {
this.debitNotesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal CreditNotes {
get {
return this.creditNotesField;
}
set {
this.creditNotesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal CashReceipts {
get {
return this.cashReceiptsField;
}
set {
this.cashReceiptsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal OtherPositive {
get {
return this.otherPositiveField;
}
set {
this.otherPositiveField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal OtherNegative {
get {
return this.otherNegativeField;
}
set {
this.otherNegativeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal ClosingBalance {
get {
return this.closingBalanceField;
}
set {
this.closingBalanceField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class FileSummaryDebtor {
private object[] itemsField;
private string debtorIdField;
private string debtorNameField;
private string excludedDebtorField;
private decimal creditLimitField;
private string debtorStatusField;
private string tradingTermsField;
private uint overduePeriodField;
private string headOfficeCodeField;
private string headOfficeNameField;
private string debtorCountryField;
private string aBNField;
private string aRBNField;
private string aCNField;
private string legalNameField;
private string mainTradingNameField;
private string otherTradingNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DebtorContact", typeof(FileSummaryDebtorDebtorContact))]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DebtorId {
get {
return this.debtorIdField;
}
set {
this.debtorIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DebtorName {
get {
return this.debtorNameField;
}
set {
this.debtorNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ExcludedDebtor {
get {
return this.excludedDebtorField;
}
set {
this.excludedDebtorField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal CreditLimit {
get {
return this.creditLimitField;
}
set {
this.creditLimitField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DebtorStatus {
get {
return this.debtorStatusField;
}
set {
this.debtorStatusField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string TradingTerms {
get {
return this.tradingTermsField;
}
set {
this.tradingTermsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public uint OverduePeriod {
get {
return this.overduePeriodField;
}
set {
this.overduePeriodField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string HeadOfficeCode {
get {
return this.headOfficeCodeField;
}
set {
this.headOfficeCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string HeadOfficeName {
get {
return this.headOfficeNameField;
}
set {
this.headOfficeNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DebtorCountry {
get {
return this.debtorCountryField;
}
set {
this.debtorCountryField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ABN {
get {
return this.aBNField;
}
set {
this.aBNField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ARBN {
get {
return this.aRBNField;
}
set {
this.aRBNField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ACN {
get {
return this.aCNField;
}
set {
this.aCNField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string LegalName {
get {
return this.legalNameField;
}
set {
this.legalNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string MainTradingName {
get {
return this.mainTradingNameField;
}
set {
this.mainTradingNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string OtherTradingName {
get {
return this.otherTradingNameField;
}
set {
this.otherTradingNameField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class FileSummaryDebtorDebtorContact {
private string surnameField;
private string firstNameField;
private string otherNamesField;
private string phoneField;
private string mobileField;
private string faxField;
private string emailField;
private string salutationField;
private string jobTitleField;
private string contactAddressField;
private string contactSuburbField;
private string contactCountryField;
private string contactStateField;
private string contactPostcodeField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Surname {
get {
return this.surnameField;
}
set {
this.surnameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string FirstName {
get {
return this.firstNameField;
}
set {
this.firstNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string OtherNames {
get {
return this.otherNamesField;
}
set {
this.otherNamesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Phone {
get {
return this.phoneField;
}
set {
this.phoneField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Mobile {
get {
return this.mobileField;
}
set {
this.mobileField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Fax {
get {
return this.faxField;
}
set {
this.faxField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Email {
get {
return this.emailField;
}
set {
this.emailField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Salutation {
get {
return this.salutationField;
}
set {
this.salutationField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string JobTitle {
get {
return this.jobTitleField;
}
set {
this.jobTitleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ContactAddress {
get {
return this.contactAddressField;
}
set {
this.contactAddressField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ContactSuburb {
get {
return this.contactSuburbField;
}
set {
this.contactSuburbField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ContactCountry {
get {
return this.contactCountryField;
}
set {
this.contactCountryField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ContactState {
get {
return this.contactStateField;
}
set {
this.contactStateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ContactPostcode {
get {
return this.contactPostcodeField;
}
set {
this.contactPostcodeField = value;
}
}
}
using System;
namespace CoinsAssetWatchXMLGen
{
class Program
{
static void Main(string[] args)
{
FileSummary fileSummary = new FileSummary();
fileSummary.FileDate = DateTime.Today;
FileSummaryDebtor debtor = new FileSummaryDebtor;
debtor.DebtorId = "11";
debtor.DebtorName = "fred";
debtor.DebtorStatus = "active";
debtor.ABN = "ABC1234567";
fileSummary.Debtor[1] = debtor;
//how do I associate this following with a debtor for XML??
FileSummaryDebtorDebtorContact contact = new FileSummaryDebtorDebtorContact();
}
}
}
The approach I took was to create a dataset classes using xsd.exe /d.
Once the data set classes were created
I initially populated the data table as follows just to get a better understanding of the data set structure:
NewDataSet newDataSet = new NewDataSet();
newDataSet.ReadXml("FileSummary.xml");

XML members initialization for XmlSerializer using C#

I have below XML and need to deserialized to get the value of "ThreadGroup.num_threads","ThreadGroup.ramp_time","HTTPSampler.path" and "HTTPSampler.domain".
<TestPlan>
<hashTree>
<hashTree>
<ThreadGroup>
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<stringProp name="ThreadGroup.num_threads">10</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<longProp name="ThreadGroup.start_time">1517853259000</longProp>
<longProp name="ThreadGroup.end_time">1517853259000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
</ThreadGroup>
<hashTree>
<hashTree>
<HTTPSamplerProxy>
<stringProp name="HTTPSampler.domain">www.abc.com/abc-service-api</stringProp>
<stringProp name="HTTPSampler.port"></stringProp>
<stringProp name="HTTPSampler.protocol"></stringProp>
<stringProp name="HTTPSampler.contentEncoding"></stringProp>
<stringProp name="HTTPSampler.path">/v1/test/test?debug=false</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<boolProp name="HTTPSampler.follow_redirects">false</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<stringProp name="HTTPSampler.embedded_url_re"></stringProp>
<stringProp name="HTTPSampler.connect_timeout"></stringProp>
<stringProp name="HTTPSampler.response_timeout"></stringProp>
</HTTPSamplerProxy>
</hashTree>
</hashTree>
</hashTree>
</hashTree>
</TestPlan>
The code I am using mentioned below.
public class xmlData
{
[Serializable, XmlRoot("jmeterTestPlan")]
public partial class jmeterTestPlan
{
private jmeterTestPlanHashTree hashTreeField;
/// <remarks/>
public jmeterTestPlanHashTree hashTree
{
get
{
return this.hashTreeField;
}
set
{
this.hashTreeField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class jmeterTestPlanHashTree
{
private jmeterTestPlanHashTreeHashTree hashTreeField;
/// <remarks/>
public jmeterTestPlanHashTreeHashTree hashTree
{
get
{
return this.hashTreeField;
}
set
{
this.hashTreeField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class jmeterTestPlanHashTreeHashTree
{
private jmeterTestPlanHashTreeHashTreeThreadGroup threadGroupField;
private jmeterTestPlanHashTreeHashTreeHashTree hashTreeField;
/// <remarks/>
public jmeterTestPlanHashTreeHashTreeThreadGroup ThreadGroup
{
get
{
return this.threadGroupField;
}
set
{
this.threadGroupField = value;
}
}
/// <remarks/>
public jmeterTestPlanHashTreeHashTreeHashTree hashTree
{
get
{
return this.hashTreeField;
}
set
{
this.hashTreeField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class jmeterTestPlanHashTreeHashTreeThreadGroup
{
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("boolProp", typeof(jmeterTestPlanHashTreeHashTreeThreadGroupBoolProp))]
[System.Xml.Serialization.XmlElementAttribute("longProp", typeof(jmeterTestPlanHashTreeHashTreeThreadGroupLongProp))]
[System.Xml.Serialization.XmlElementAttribute("stringProp", typeof(jmeterTestPlanHashTreeHashTreeThreadGroupStringProp))]
public object[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class jmeterTestPlanHashTreeHashTreeThreadGroupBoolProp
{
private string nameField;
private bool valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public bool Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class jmeterTestPlanHashTreeHashTreeThreadGroupLongProp
{
private string nameField;
private ulong valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public ulong Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class jmeterTestPlanHashTreeHashTreeThreadGroupStringProp
{
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class jmeterTestPlanHashTreeHashTreeHashTree
{
private jmeterTestPlanHashTreeHashTreeHashTreeHashTree hashTreeField;
/// <remarks/>
public jmeterTestPlanHashTreeHashTreeHashTreeHashTree hashTree
{
get
{
return this.hashTreeField;
}
set
{
this.hashTreeField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class jmeterTestPlanHashTreeHashTreeHashTreeHashTree
{
private jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxy hTTPSamplerProxyField;
/// <remarks/>
public jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxy HTTPSamplerProxy
{
get
{
return this.hTTPSamplerProxyField;
}
set
{
this.hTTPSamplerProxyField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxy
{
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("boolProp", typeof(jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxyBoolProp))]
[System.Xml.Serialization.XmlElementAttribute("stringProp", typeof(jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxyStringProp))]
public object[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxyBoolProp
{
private string nameField;
private bool valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public bool Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxyStringProp
{
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
}
the above code is giving required value with a lot of additional attributes value and the code seems lengthy too. However, I need those 4 value. Please suggest any better solution.
UPDATED (2018-09-10)
I've updated this to get it easy to use.
Before I started, I decided that you probably wanted the possibility of longProps in the HTTPSamplerProxy section. It also made the coding (much) easier and cleaner. I've tested it without any longProps just to make sure it worked with the existing XML the way you'd expect.
The original process
The updates to the original description are italicized
What I did was to use the standard XSD.exe tool to take your source XML file (with an extra longProp in the HTTPSamplerProxy section) and create an XSD. Then I used XSD.exe again to create a (very ugly) C# file. At that point, the root of the whole mess is the TestPlan class. Here's what XSD.exe produced (updated - the only change to the newly-emitted XSD.exe code was a namespace declaration at the top):
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class hashTree {
private hashTreeHTTPSamplerProxy[] hTTPSamplerProxyField;
private hashTree[] hashTree1Field;
private hashTreeThreadGroup[] threadGroupField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("HTTPSamplerProxy", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public hashTreeHTTPSamplerProxy[] HTTPSamplerProxy {
get {
return this.hTTPSamplerProxyField;
}
set {
this.hTTPSamplerProxyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("hashTree")]
public hashTree[] hashTree1 {
get {
return this.hashTree1Field;
}
set {
this.hashTree1Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ThreadGroup", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public hashTreeThreadGroup[] ThreadGroup {
get {
return this.threadGroupField;
}
set {
this.threadGroupField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class hashTreeHTTPSamplerProxy {
private longProp[] longPropField;
private stringProp[] stringPropField;
private boolProp[] boolPropField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("longProp", IsNullable = true)]
public longProp[] longProp {
get {
return this.longPropField;
}
set {
this.longPropField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("stringProp", IsNullable = true)]
public stringProp[] stringProp {
get {
return this.stringPropField;
}
set {
this.stringPropField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("boolProp", IsNullable = true)]
public boolProp[] boolProp {
get {
return this.boolPropField;
}
set {
this.boolPropField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public partial class longProp {
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public partial class stringProp {
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public partial class boolProp {
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class hashTreeThreadGroup {
private stringProp[] stringPropField;
private longProp[] longPropField;
private boolProp[] boolPropField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("stringProp", IsNullable = true)]
public stringProp[] stringProp {
get {
return this.stringPropField;
}
set {
this.stringPropField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("longProp", IsNullable = true)]
public longProp[] longProp {
get {
return this.longPropField;
}
set {
this.longPropField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("boolProp", IsNullable = true)]
public boolProp[] boolProp {
get {
return this.boolPropField;
}
set {
this.boolPropField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class TestPlan {
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("boolProp", typeof(boolProp), IsNullable = true)]
[System.Xml.Serialization.XmlElementAttribute("hashTree", typeof(hashTree))]
[System.Xml.Serialization.XmlElementAttribute("longProp", typeof(longProp), IsNullable = true)]
[System.Xml.Serialization.XmlElementAttribute("stringProp", typeof(stringProp), IsNullable = true)]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
The rest of this is mostly new
The code emitted by XSD.exe creates a set of partial classes. I extended the partial classes in a few ways in a separate source file. But, before I started, I declared an interface and a static worker class.
public interface IGrouping {
boolProp[] boolProp { get; }
stringProp[] stringProp { get; }
longProp[] longProp { get; }
Dictionary<string, bool?> BoolProperties { get; }
Dictionary<string, long?> LongProperties { get; }
Dictionary<string, string> StringProperties { get; }
}
and
public static class PropertyGrouper {
public static void GroupProperties(IGrouping itemToGroup) {
//has this been done before, if yes, return
if (itemToGroup.StringProperties.Count > 0 || itemToGroup.BoolProperties.Count > 0 || itemToGroup.LongProperties.Count > 0 ) {
return;
}
//otherwise
if (itemToGroup.boolProp != null) {
foreach (var bProp in itemToGroup.boolProp) {
var succeeded = bool.TryParse(bProp.Value, out var bValue);
itemToGroup.BoolProperties.Add(bProp.name, succeeded ? bValue : (bool?)null);
}
}
if (itemToGroup.longProp != null) {
foreach (var lProp in itemToGroup.longProp) {
var succeeded = long.TryParse(lProp.Value, out var lValue);
itemToGroup.LongProperties.Add(lProp.name, succeeded ? lValue : (long?)null);
}
}
if (itemToGroup.stringProp != null) {
foreach (var sProp in itemToGroup.stringProp) {
itemToGroup.StringProperties.Add(sProp.name, sProp.Value);
}
}
}
}
With those in place, I extended each of the XSD.exe-emitted classes. The TestPlan class was easy - I just added a typed property:
public partial class TestPlan {
[XmlIgnore]
public hashTree HashTree => Items[0] as hashTree;
}
The hashTreeThreadGroup and hashTreeHTTPSamplerProxy classes were extended in the same way (remember, I didn't name any of these classes, XSD.exe named them from your XML) . Each was declared to implement the IGrouping interface, and each got 3 dictionaries of properties (as required by IGrouping). The three arrays in the IGrouping interface were in the XSD-emitted code:
public partial class hashTreeThreadGroup : IGrouping {
[XmlIgnore]
public Dictionary<string, bool?> BoolProperties { get; } = new Dictionary<string, bool?>();
[XmlIgnore]
public Dictionary<string, long?> LongProperties { get; } = new Dictionary<string, long?>();
[XmlIgnore]
public Dictionary<string, string> StringProperties { get; } = new Dictionary<string, string>();
}
public partial class hashTreeHTTPSamplerProxy : IGrouping {
[XmlIgnore]
public Dictionary<string, bool?> BoolProperties { get; } = new Dictionary<string, bool?>();
[XmlIgnore]
public Dictionary<string, long?> LongProperties { get; } = new Dictionary<string, long?>();
[XmlIgnore]
public Dictionary<string, string> StringProperties { get; } = new Dictionary<string, string>();
}
Finally, I extended hashTree class. I added three typed properties, each with a null check to make things clean. The ThreadGroupItem and HttpSamplerProxyItem properties each get a call to PropertyGrouper.GroupProperties. The first time this is called, the properties in the XmlSerializer-created property arrays are copied into Dictionaries.
public partial class hashTree {
[XmlIgnore]
public hashTree HashTree {
get {
if (hashTree1 != null) {
return hashTree1[0] as hashTree;
} else {
return null;
}
}
}
[XmlIgnore]
public hashTreeThreadGroup ThreadGroupItem {
get {
if (ThreadGroup != null) {
var threadGroup = ThreadGroup[0]; // as hashTreeThreadGroup;
PropertyGrouper.GroupProperties(threadGroup);
return threadGroup;
} else {
return null;
}
}
}
[XmlIgnore]
public hashTreeHTTPSamplerProxy HttpSamplerProxyItem {
get {
if (HTTPSamplerProxy != null) {
var httpSamplerProxy = HTTPSamplerProxy[0];
PropertyGrouper.GroupProperties(httpSamplerProxy);
return httpSamplerProxy;
} else {
return null;
}
}
}
}
With that all in place, I ran the same code.
TestPlan result;
using (var stream = new FileStream("source.xml", FileMode.Open, FileAccess.Read)) {
var serializer = new XmlSerializer(typeof(TestPlan));
result = (TestPlan)serializer.Deserialize(stream);
}
This code is how I accessed your data with my first example.
var numThreadsParsed = long.TryParse((((XmlSerializeForm.hashTree)result.Items[0]).hashTree1[0].ThreadGroup[0].stringProp[1].Value), out var numThreads);
var httpSamplerPath = ((XmlSerializeForm.hashTree)result.Items[0]).hashTree1[0].hashTree1[0].hashTree1[0].HTTPSamplerProxy[0].stringProp[4].Value;
But, with the few simple additions I made (well, the code isn't that complicated, but getting it right was), accessing the properties is much cleaner:
string numThreadsParsed = result.HashTree.HashTree.ThreadGroupItem.StringProperties["ThreadGroup.num_threads"];
long? startTime = result.HashTree.HashTree.ThreadGroupItem.LongProperties["ThreadGroup.start_time"];
string httpSamplerPath = result.HashTree.HashTree.HashTree.HashTree.HttpSamplerProxyItem.StringProperties["HTTPSampler.path"];
bool? useKeepAlive = result.HashTree.HashTree.HashTree.HashTree.HttpSamplerProxyItem.BoolProperties["HTTPSampler.use_keepalive"];
There you go!
With external lib Cinchoo ETL - an open source library, you can grab the chosen node values easily as below
Define .NET type
public class TestPlan
{
[ChoXmlNodeRecordField(XPath = #"/ThreadGroup/stringProp[#name=""ThreadGroup.on_sample_error""]")]
public string NumThreads { get; set; }
[ChoXmlNodeRecordField(XPath = #"/ThreadGroup/stringProp[#name=""ThreadGroup.ramp_time""]")]
public int RampTime { get; set; }
[ChoXmlNodeRecordField(XPath = #"/hashTree/hashTree/HTTPSamplerProxy/stringProp[#name=""HTTPSampler.path""]")]
public string Path { get; set; }
[ChoXmlNodeRecordField(XPath = #"/hashTree/hashTree/HTTPSamplerProxy/stringProp[#name=""HTTPSampler.domain""]")]
public string Domain { get; set; }
}
Then deserialize the input xml using Cinchoo ETL as below
static void Main()
{
using (var p = new ChoXmlReader<TestPlan>("*** XML file path ***")
.WithXPath("/TestPlan/hashTree/hashTree")
)
{
foreach (var rec in p)
Console.WriteLine(rec.Dump());
}
}
Output:
-- ChoXmlReaderTest.Program+TestPlan State --
NumThreads: continue
RampTime: 1
Path: /v1/test/test?debug=false
Domain: www.abc.com/abc-service-api
Hope it helps.
Disclaimer: I'm the author of this library.
Use xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlData data = new XmlData(FILENAME);
}
}
public class XmlData
{
public int? num_threads { get; set;}
public int? ramp_time { get;set;}
List<SamplerProxy> HTTPSamplerProxies { get;set;}
public XmlData(string filename)
{
XDocument doc = XDocument.Load(filename);
XElement threadGroup = doc.Descendants("ThreadGroup").FirstOrDefault();
num_threads = (int?)threadGroup.Elements("stringProp").Where(x => (string)x.Attribute("name") == "ThreadGroup.num_threads").FirstOrDefault();
ramp_time = (int?)threadGroup.Elements("stringProp").Where(x => (string)x.Attribute("name") == "ThreadGroup.ramp_time").FirstOrDefault();
HTTPSamplerProxies = doc.Descendants("HTTPSamplerProxy").Select(x => new SamplerProxy() {
path = (string)x.Elements("stringProp").Where(y => (string)y.Attribute("name") == "HTTPSampler.path").FirstOrDefault(),
domain = (string)x.Elements("stringProp").Where(y => (string)y.Attribute("name") == "HTTPSampler.domain").FirstOrDefault()
}).ToList();
}
}
public class SamplerProxy
{
public string path { get; set; }
public string domain { get; set; }
}
}

Creating a model class for XML

How to create a model class for the below xml.
<?xml version="1.0"?>
<NOTIFICATION>
<NOTIF_HEADER>
<SOURCE>FCDB</SOURCE>
<MSGID>585995798502256</MSGID>
<TXN_ID>UTP</TXN_ID>
</NOTIF_HEADER>
<NOTIF_BODY>
<UserDetail>
<USER_NAME>Mr Customer</USER_NAME>
<USER_EMAIL>MrCustomer#email.com</USER_EMAIL>
<USER_MO>0500000007</USER_MO>
</UserDetail>
<CAS_Token>
<PASS>
<![CDATA[[PASS]]]>
</PASS>
<GEMALTO>N</GEMALTO>
</CAS_Token>
</NOTIF_BODY>
</NOTIFICATION>
I want to serialize this xml. For that I tried to create model class, but didn't worked.
Here is your class:
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class NOTIFICATION
{
private NOTIFICATIONNOTIF_HEADER nOTIF_HEADERField;
private NOTIFICATIONNOTIF_BODY nOTIF_BODYField;
/// <remarks/>
public NOTIFICATIONNOTIF_HEADER NOTIF_HEADER
{
get
{
return this.nOTIF_HEADERField;
}
set
{
this.nOTIF_HEADERField = value;
}
}
/// <remarks/>
public NOTIFICATIONNOTIF_BODY NOTIF_BODY
{
get
{
return this.nOTIF_BODYField;
}
set
{
this.nOTIF_BODYField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class NOTIFICATIONNOTIF_HEADER
{
private string sOURCEField;
private ulong mSGIDField;
private string tXN_IDField;
/// <remarks/>
public string SOURCE
{
get
{
return this.sOURCEField;
}
set
{
this.sOURCEField = value;
}
}
/// <remarks/>
public ulong MSGID
{
get
{
return this.mSGIDField;
}
set
{
this.mSGIDField = value;
}
}
/// <remarks/>
public string TXN_ID
{
get
{
return this.tXN_IDField;
}
set
{
this.tXN_IDField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class NOTIFICATIONNOTIF_BODY
{
private NOTIFICATIONNOTIF_BODYUserDetail userDetailField;
private NOTIFICATIONNOTIF_BODYCAS_Token cAS_TokenField;
/// <remarks/>
public NOTIFICATIONNOTIF_BODYUserDetail UserDetail
{
get
{
return this.userDetailField;
}
set
{
this.userDetailField = value;
}
}
/// <remarks/>
public NOTIFICATIONNOTIF_BODYCAS_Token CAS_Token
{
get
{
return this.cAS_TokenField;
}
set
{
this.cAS_TokenField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class NOTIFICATIONNOTIF_BODYUserDetail
{
private string uSER_NAMEField;
private string uSER_EMAILField;
private uint uSER_MOField;
/// <remarks/>
public string USER_NAME
{
get
{
return this.uSER_NAMEField;
}
set
{
this.uSER_NAMEField = value;
}
}
/// <remarks/>
public string USER_EMAIL
{
get
{
return this.uSER_EMAILField;
}
set
{
this.uSER_EMAILField = value;
}
}
/// <remarks/>
public uint USER_MO
{
get
{
return this.uSER_MOField;
}
set
{
this.uSER_MOField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class NOTIFICATIONNOTIF_BODYCAS_Token
{
private string pASSField;
private string gEMALTOField;
/// <remarks/>
public string PASS
{
get
{
return this.pASSField;
}
set
{
this.pASSField = value;
}
}
/// <remarks/>
public string GEMALTO
{
get
{
return this.gEMALTOField;
}
set
{
this.gEMALTOField = value;
}
}
}
You can generate in yourself with VS:
Edit -> Paste Special -> Paste XML as classes

The same table 'tableName' cannot be the child table in two nested relations

Using XSD.EXE I generated a *.cs file to serialize XML. When I serialize I get the error
"The same table 'DefaultFont' cannot be the child table in two nested relations."
Why do I receive the error? Can I fix the error without using XSLT transforms?
Code to serialize
StreamReader fs = new StreamReader(#"C:\path\DocumentSample.xml");
XmlSerializer serializer = new XmlSerializer(typeof(PhysDocDocument));
var result = serializer.Deserialize(fs) as PhysDocDocument;//exception thrown here
Sample XML
<PhysDocDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PhysDocNode HeaderVisible="true" Display="Summary" IsCollapsed="false" CopyForwardEnabled="true" IsHidden="false" Borders="false">
<LinkedPluginId>00000000-0000-0000-0000-000000000000</LinkedPluginId>
<Plugin xsi:type="PromptPlugin" ID="2446441e-6eb2-49ef-b8e3-28f638755b75">
<CopyForwardChecked>true</CopyForwardChecked>
<PromptBase xsi:type="MemoPrompt">
<Label>Comment</Label>
<DocumentValue>
<Paragraphs />
<DefaultFont FontFamily="Arial" SizeInPoints="10" Style="Regular" />
</DocumentValue>
<ShowLabel>false</ShowLabel>
<RenderHeader>false</RenderHeader>
</PromptBase>
</Plugin>
</PhysDocNode>
</PhysDocDocument>
Generated Code
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.17929
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class PhysDocDocument
{
private PhysDocDocumentPhysDocNode[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("PhysDocNode", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNode[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNode
{
private string linkedPluginIdField;
private PhysDocDocumentPhysDocNodePlugin[] pluginField;
private string headerVisibleField;
private string displayField;
private string isCollapsedField;
private string copyForwardEnabledField;
private string isHiddenField;
private string bordersField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LinkedPluginId
{
get
{
return this.linkedPluginIdField;
}
set
{
this.linkedPluginIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Plugin", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePlugin[] Plugin
{
get
{
return this.pluginField;
}
set
{
this.pluginField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string HeaderVisible
{
get
{
return this.headerVisibleField;
}
set
{
this.headerVisibleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Display
{
get
{
return this.displayField;
}
set
{
this.displayField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string IsCollapsed
{
get
{
return this.isCollapsedField;
}
set
{
this.isCollapsedField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string CopyForwardEnabled
{
get
{
return this.copyForwardEnabledField;
}
set
{
this.copyForwardEnabledField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string IsHidden
{
get
{
return this.isHiddenField;
}
set
{
this.isHiddenField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Borders
{
get
{
return this.bordersField;
}
set
{
this.bordersField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePlugin
{
private string copyForwardCheckedField;
private PhysDocDocumentPhysDocNodePluginPromptBase[] promptBaseField;
private string idField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CopyForwardChecked
{
get
{
return this.copyForwardCheckedField;
}
set
{
this.copyForwardCheckedField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("PromptBase", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBase[] PromptBase
{
get
{
return this.promptBaseField;
}
set
{
this.promptBaseField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ID
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBase
{
private string labelField;
private string showLabelField;
private string renderHeaderField;
private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] documentValueField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Label
{
get
{
return this.labelField;
}
set
{
this.labelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ShowLabel
{
get
{
return this.showLabelField;
}
set
{
this.showLabelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string RenderHeader
{
get
{
return this.renderHeaderField;
}
set
{
this.renderHeaderField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DocumentValue", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] DocumentValue
{
get
{
return this.documentValueField;
}
set
{
this.documentValueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue
{
private string paragraphsField;
private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] defaultFontField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Paragraphs
{
get
{
return this.paragraphsField;
}
set
{
this.paragraphsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DefaultFont", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] DefaultFont
{
get
{
return this.defaultFontField;
}
set
{
this.defaultFontField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont
{
private string fontFamilyField;
private string sizeInPointsField;
private string styleField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string FontFamily
{
get
{
return this.fontFamilyField;
}
set
{
this.fontFamilyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string SizeInPoints
{
get
{
return this.sizeInPointsField;
}
set
{
this.sizeInPointsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Style
{
get
{
return this.styleField;
}
set
{
this.styleField = value;
}
}
}
Some of your generated classes have AnonymousType=true set in XmlTypeAttribute.
When I modify this and add real class names - the error goes away. Here's full working sample (note that I added 'MemoPrompt' and 'PromptPlugin' class names to XmlTypeAttribute):
using System.Xml.Serialization;
using System.IO;
using System;
class App
{
static void Main()
{
StreamReader fs = new StreamReader(#"sample.xml");
XmlSerializer serializer = new XmlSerializer(typeof(PhysDocDocument));
var result = serializer.Deserialize(fs) as PhysDocDocument;
Console.WriteLine(result);
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class PhysDocDocument
{
private PhysDocDocumentPhysDocNode[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("PhysDocNode", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNode[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNode
{
private string linkedPluginIdField;
private PhysDocDocumentPhysDocNodePlugin[] pluginField;
private string headerVisibleField;
private string displayField;
private string isCollapsedField;
private string copyForwardEnabledField;
private string isHiddenField;
private string bordersField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LinkedPluginId
{
get
{
return this.linkedPluginIdField;
}
set
{
this.linkedPluginIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Plugin", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePlugin[] Plugin
{
get
{
return this.pluginField;
}
set
{
this.pluginField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string HeaderVisible
{
get
{
return this.headerVisibleField;
}
set
{
this.headerVisibleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Display
{
get
{
return this.displayField;
}
set
{
this.displayField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string IsCollapsed
{
get
{
return this.isCollapsedField;
}
set
{
this.isCollapsedField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string CopyForwardEnabled
{
get
{
return this.copyForwardEnabledField;
}
set
{
this.copyForwardEnabledField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string IsHidden
{
get
{
return this.isHiddenField;
}
set
{
this.isHiddenField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Borders
{
get
{
return this.bordersField;
}
set
{
this.bordersField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute("PromptPlugin")]
public partial class PhysDocDocumentPhysDocNodePlugin
{
private string copyForwardCheckedField;
private PhysDocDocumentPhysDocNodePluginPromptBase[] promptBaseField;
private string idField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CopyForwardChecked
{
get
{
return this.copyForwardCheckedField;
}
set
{
this.copyForwardCheckedField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("PromptBase", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBase[] PromptBase
{
get
{
return this.promptBaseField;
}
set
{
this.promptBaseField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ID
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute("MemoPrompt")]
public partial class PhysDocDocumentPhysDocNodePluginPromptBase
{
private string labelField;
private string showLabelField;
private string renderHeaderField;
private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] documentValueField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Label
{
get
{
return this.labelField;
}
set
{
this.labelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ShowLabel
{
get
{
return this.showLabelField;
}
set
{
this.showLabelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string RenderHeader
{
get
{
return this.renderHeaderField;
}
set
{
this.renderHeaderField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DocumentValue", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] DocumentValue
{
get
{
return this.documentValueField;
}
set
{
this.documentValueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue
{
private string paragraphsField;
private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] defaultFontField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Paragraphs
{
get
{
return this.paragraphsField;
}
set
{
this.paragraphsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DefaultFont", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] DefaultFont
{
get
{
return this.defaultFontField;
}
set
{
this.defaultFontField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont
{
private string fontFamilyField;
private string sizeInPointsField;
private string styleField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string FontFamily
{
get
{
return this.fontFamilyField;
}
set
{
this.fontFamilyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string SizeInPoints
{
get
{
return this.sizeInPointsField;
}
set
{
this.sizeInPointsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Style
{
get
{
return this.styleField;
}
set
{
this.styleField = value;
}
}
}

XmlSerialize - Desrialize external XML

I have many files, with the format of:
<?xml version="1.0" encoding="utf-8"?>
<Words>
<word>
<Eng>chain</Eng>
<EngEnd>chained</EngEnd>
<PartOfSpeechEng>verb</PartOfSpeechEng>
<Heb>לקשור עם שרשרת</Heb>
<EngInHeb>צֵ'ין</EngInHeb>
<PartOfSpeechHeb>פועל</PartOfSpeechHeb>
<DicNumber>27</DicNumber>
<Arb>سلسلة</Arb>
<EngInArb>
</EngInArb>
</word>
<word>
<Eng>growl</Eng>
<EngEnd>growls</EngEnd>
<PartOfSpeechEng>verb</PartOfSpeechEng>
<Heb>לנהום</Heb>
<EngInHeb>גְרַאוּל</EngInHeb>
<PartOfSpeechHeb>פועל</PartOfSpeechHeb>
<DicNumber>3</DicNumber>
<Arb>دمدمة/تذمـُّر</Arb>
<EngInArb>
</EngInArb>
</word>
</Words>
I failed to deserialize it into array, can someone help me deserialize it
Thanks
Generated using xsd.exe using schema created by this online tool.
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Words {
private WordsWord[] wordField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("word")]
public WordsWord[] word {
get {
return this.wordField;
}
set {
this.wordField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class WordsWord {
private string engField;
private string engEndField;
private string partOfSpeechEngField;
private string hebField;
private string engInHebField;
private string partOfSpeechHebField;
private int dicNumberField;
private string arbField;
private string engInArbField;
/// <remarks/>
public string Eng {
get {
return this.engField;
}
set {
this.engField = value;
}
}
/// <remarks/>
public string EngEnd {
get {
return this.engEndField;
}
set {
this.engEndField = value;
}
}
/// <remarks/>
public string PartOfSpeechEng {
get {
return this.partOfSpeechEngField;
}
set {
this.partOfSpeechEngField = value;
}
}
/// <remarks/>
public string Heb {
get {
return this.hebField;
}
set {
this.hebField = value;
}
}
/// <remarks/>
public string EngInHeb {
get {
return this.engInHebField;
}
set {
this.engInHebField = value;
}
}
/// <remarks/>
public string PartOfSpeechHeb {
get {
return this.partOfSpeechHebField;
}
set {
this.partOfSpeechHebField = value;
}
}
/// <remarks/>
public int DicNumber {
get {
return this.dicNumberField;
}
set {
this.dicNumberField = value;
}
}
/// <remarks/>
public string Arb {
get {
return this.arbField;
}
set {
this.arbField = value;
}
}
/// <remarks/>
public string EngInArb {
get {
return this.engInArbField;
}
set {
this.engInArbField = value;
}
}
}
and you can use it like this
XmlSerializer mySerializer = new XmlSerializer(typeof(Words));
FileStream myFileStream = new FileStream("path_to_xml",FileMode.Open);
Words words = (Words)mySerializer.Deserialize(myFileStream);

Categories

Resources