I need to find the tag <ns2:Response>400 or 200 etc</n2:Response> using service trace viewer it look something like this!!
<SOAP-ENV:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-14799">
<ns2:SendInvoice xmlns:ns2="http://www.zadrwan.com/services/" xmlns:ns3="http://www.zadrwan.com/services/DocumentSendTo" xmlns:ns4="http://www.zadrwan.com/services/VersionRequest">
<ns2:Response>200</ns2:Response>
<ns2:Comments>Success!.</ns2:Comments>
</ns2:SendInvoice>
</SOAP-ENV:Body>
Or is there another way to get a variable without using an XML reader (to read all the document) or, in this case, a text reader (I'm redesigning a VB project that used a StreamReader)?
There are plenty of ways. Since there is a schema and you can use the xsd.exe tool to get classes I would use code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(TimestampType));
TimestampType timeStamp = (TimestampType)serializer.Deserialize(reader);
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.6421
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
//
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xs" +
"d")]
[System.Xml.Serialization.XmlRootAttribute("Timestamp", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xs" +
"d", IsNullable=false)]
public partial class TimestampType {
private AttributedDateTime createdField;
private AttributedDateTime expiresField;
private System.Xml.XmlElement[] itemsField;
private string idField;
private System.Xml.XmlAttribute[] anyAttrField;
/// <remarks/>
public AttributedDateTime Created {
get {
return this.createdField;
}
set {
this.createdField = value;
}
}
/// <remarks/>
public AttributedDateTime Expires {
get {
return this.expiresField;
}
set {
this.expiresField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlElement[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified, DataType="ID")]
public string Id {
get {
return this.idField;
}
set {
this.idField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAnyAttributeAttribute()]
public System.Xml.XmlAttribute[] AnyAttr {
get {
return this.anyAttrField;
}
set {
this.anyAttrField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xs" +
"d")]
[System.Xml.Serialization.XmlRootAttribute("Expires", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xs" +
"d", IsNullable=false)]
public partial class AttributedDateTime {
private string idField;
private System.Xml.XmlAttribute[] anyAttrField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified, DataType="ID")]
public string Id {
get {
return this.idField;
}
set {
this.idField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAnyAttributeAttribute()]
public System.Xml.XmlAttribute[] AnyAttr {
get {
return this.anyAttrField;
}
set {
this.anyAttrField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
}
Related
I am trying to deserialize the following XML
<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<EnvelopeVersion>2.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>DRUG_DATA</Class>
<Qualifier>response</Qualifier>
<Function>submit</Function>
<CorrelationID>BD694DAAA26AA6068EAAAE5C7746CE54</CorrelationID>
<Transformation>XML</Transformation>
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID />
<Authentication>
<Method />
<Role />
<Value />
</Authentication>
</IDAuthentication>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys>
<Key Type="SpokeName" />
</Keys>
</GovTalkDetails>
<Body>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
<S:Body>
<ns2:getGenericDrugsResponse xmlns:ns2="http://webservice.sirkb/">
<return>
<DRUG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:genericDrug">
<GENERIC_DRUG_ID>147</GENERIC_DRUG_ID>
<GENERIC_DRUG_NAME>Amoxicilline 125mg/5ml - 60ml</GENERIC_DRUG_NAME>
</DRUG>
<DRUG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:genericDrug">
<GENERIC_DRUG_ID>149</GENERIC_DRUG_ID>
<GENERIC_DRUG_NAME>Amoxicilline 250mg/5ml - 60ml</GENERIC_DRUG_NAME>
</DRUG>
<DRUG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:genericDrug">
<DOSAGE>10mg/5ml</DOSAGE>
<GENERIC_DRUG_ID>2312</GENERIC_DRUG_ID>
<GENERIC_DRUG_NAME>Vinorelbine (as vinorelbine tartrate)</GENERIC_DRUG_NAME>
<PHARMACEUTICAL_FORM>concentrate for solution for infusion</PHARMACEUTICAL_FORM>
</DRUG>
<DRUG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:genericDrug">
<DOSAGE>100 u/ml (approximately 0.58mg)</DOSAGE>
<GENERIC_DRUG_ID>2313</GENERIC_DRUG_ID>
<GENERIC_DRUG_NAME>Laronidase</GENERIC_DRUG_NAME>
<PHARMACEUTICAL_FORM>concentrate for solution for infusion</PHARMACEUTICAL_FORM>
</DRUG>
<RETURN_STATUS>SUCCESS</RETURN_STATUS>
</return>
</ns2:getGenericDrugsResponse>
</S:Body>
</S:Envelope>
</Body>
</GovTalkMessage>
I have generated the classes through Paste XML as Classes feature of Visual Studio 2017. (Using the xsd.exe is the same) I have not changed the generated classes.
They have the following form:
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope", IsNullable = false)]
public partial class GovTalkMessage
{
private decimal envelopeVersionField;
private GovTalkMessageHeader headerField;
private GovTalkMessageGovTalkDetails govTalkDetailsField;
private GovTalkMessageBody bodyField;
/// <remarks/>
public decimal EnvelopeVersion
{
get
{
return this.envelopeVersionField;
}
set
{
this.envelopeVersionField = value;
}
}
/// <remarks/>
public GovTalkMessageHeader Header
{
get
{
return this.headerField;
}
set
{
this.headerField = value;
}
}
/// <remarks/>
public GovTalkMessageGovTalkDetails GovTalkDetails
{
get
{
return this.govTalkDetailsField;
}
set
{
this.govTalkDetailsField = value;
}
}
/// <remarks/>
public GovTalkMessageBody Body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class GovTalkMessageHeader
{
private GovTalkMessageHeaderMessageDetails messageDetailsField;
private GovTalkMessageHeaderSenderDetails senderDetailsField;
/// <remarks/>
public GovTalkMessageHeaderMessageDetails MessageDetails
{
get
{
return this.messageDetailsField;
}
set
{
this.messageDetailsField = value;
}
}
/// <remarks/>
public GovTalkMessageHeaderSenderDetails SenderDetails
{
get
{
return this.senderDetailsField;
}
set
{
this.senderDetailsField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class GovTalkMessageHeaderMessageDetails
{
private string classField;
private string qualifierField;
private string functionField;
private string correlationIDField;
private string transformationField;
/// <remarks/>
public string Class
{
get
{
return this.classField;
}
set
{
this.classField = value;
}
}
/// <remarks/>
public string Qualifier
{
get
{
return this.qualifierField;
}
set
{
this.qualifierField = value;
}
}
/// <remarks/>
public string Function
{
get
{
return this.functionField;
}
set
{
this.functionField = value;
}
}
/// <remarks/>
public string CorrelationID
{
get
{
return this.correlationIDField;
}
set
{
this.correlationIDField = value;
}
}
/// <remarks/>
public string Transformation
{
get
{
return this.transformationField;
}
set
{
this.transformationField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class GovTalkMessageHeaderSenderDetails
{
private GovTalkMessageHeaderSenderDetailsIDAuthentication iDAuthenticationField;
/// <remarks/>
public GovTalkMessageHeaderSenderDetailsIDAuthentication IDAuthentication
{
get
{
return this.iDAuthenticationField;
}
set
{
this.iDAuthenticationField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class GovTalkMessageHeaderSenderDetailsIDAuthentication
{
private object senderIDField;
private GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication authenticationField;
/// <remarks/>
public object SenderID
{
get
{
return this.senderIDField;
}
set
{
this.senderIDField = value;
}
}
/// <remarks/>
public GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication Authentication
{
get
{
return this.authenticationField;
}
set
{
this.authenticationField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication
{
private object methodField;
private object roleField;
private object valueField;
/// <remarks/>
public object Method
{
get
{
return this.methodField;
}
set
{
this.methodField = value;
}
}
/// <remarks/>
public object Role
{
get
{
return this.roleField;
}
set
{
this.roleField = value;
}
}
/// <remarks/>
public object Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class GovTalkMessageGovTalkDetails
{
private GovTalkMessageGovTalkDetailsKeys keysField;
/// <remarks/>
public GovTalkMessageGovTalkDetailsKeys Keys
{
get
{
return this.keysField;
}
set
{
this.keysField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class GovTalkMessageGovTalkDetailsKeys
{
private GovTalkMessageGovTalkDetailsKeysKey keyField;
/// <remarks/>
public GovTalkMessageGovTalkDetailsKeysKey Key
{
get
{
return this.keyField;
}
set
{
this.keyField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class GovTalkMessageGovTalkDetailsKeysKey
{
private string typeField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class GovTalkMessageBody
{
private Envelope envelopeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public Envelope Envelope
{
get
{
return this.envelopeField;
}
set
{
this.envelopeField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2003/05/soap-envelope")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.w3.org/2003/05/soap-envelope", IsNullable = false)]
public partial class Envelope
{
private EnvelopeBody bodyField;
/// <remarks/>
public EnvelopeBody Body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public partial class EnvelopeBody
{
private getGenericDrugsResponse getGenericDrugsResponseField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://webservice.sirkb/")]
public getGenericDrugsResponse getGenericDrugsResponse
{
get
{
return this.getGenericDrugsResponseField;
}
set
{
this.getGenericDrugsResponseField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://webservice.sirkb/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://webservice.sirkb/", IsNullable = false)]
public partial class getGenericDrugsResponse
{
private #return returnField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public #return #return
{
get
{
return this.returnField;
}
set
{
this.returnField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope", IsNullable = false)]
public partial class #return
{
private returnDRUG[] dRUGField;
private string rETURN_STATUSField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DRUG")]
public returnDRUG[] DRUG
{
get
{
return this.dRUGField;
}
set
{
this.dRUGField = value;
}
}
/// <remarks/>
public string RETURN_STATUS
{
get
{
return this.rETURN_STATUSField;
}
set
{
this.rETURN_STATUSField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class returnDRUG
{
private string dOSAGEField;
private ushort gENERIC_DRUG_IDField;
private string gENERIC_DRUG_NAMEField;
private string pHARMACEUTICAL_FORMField;
/// <remarks/>
public string DOSAGE
{
get
{
return this.dOSAGEField;
}
set
{
this.dOSAGEField = value;
}
}
/// <remarks/>
public ushort GENERIC_DRUG_ID
{
get
{
return this.gENERIC_DRUG_IDField;
}
set
{
this.gENERIC_DRUG_IDField = value;
}
}
/// <remarks/>
public string GENERIC_DRUG_NAME
{
get
{
return this.gENERIC_DRUG_NAMEField;
}
set
{
this.gENERIC_DRUG_NAMEField = value;
}
}
/// <remarks/>
public string PHARMACEUTICAL_FORM
{
get
{
return this.pHARMACEUTICAL_FORMField;
}
set
{
this.pHARMACEUTICAL_FORMField = value;
}
}
}
I get the following error:
System.InvalidOperationException: 'There is an error in XML document (33, 14).'
Inner Exception
InvalidOperationException: The specified type was not recognized:
name='genericDrug', namespace='http://webservice.sirkb/', at http://www.govtalk.gov.uk/CM/envelope'>.
I have noticed from this thread that if I remove xsi:type="ns2:genericDrug" from the XML file I can deserialize the XML. I can't modify the XML because it is the response I get for the request. It is not a good practice to do a string replace on XML, so I am looking for a better solution.
This might be a duplicate question from this one, but since I am not able to solve the problem I am posting it again because it is difficult to get help in the comment section.
Based on the above question I have tried to change the annotation of public partial class returnDRUG
from
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
to
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.w3.org/2001/XMLSchema-instance", TypeName = "genericDrug")]
but still cant parse the XML.
The parsing code is simple
var deserializer = new XmlSerializer(typeof(GovTalkMessage));
TextReader textReader = new StreamReader("drug.xml"); //saved response in file for simplicity
GovTalkMessage response;
response = (GovTalkMessage)deserializer.Deserialize(textReader);
textReader.Close();
What can I do to deserialize the XML in the GovTalkMessage object?
You need to change the decoration on the returnDRUG class
from this
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class returnDRUG
to this
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "genericDrug", Namespace = "http://webservice.sirkb/")]
public partial class returnDRUG
Specify it's type as "genericDrug", and the crucial bit, correct it's namespace to "http://webservice.sirkb/"
I've just used you code and managed to de-serialize using this change.
The explanation is that if you take a look at the definition of DRUG you can see that it's type is defined as "genericDRUG" in the namespace alias "ns2"
<DRUG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:genericDrug">
if you look at the definition of the ns2 alias you can see that it's "http://webservice.sirkb/"
<ns2:getGenericDrugsResponse xmlns:ns2="http://webservice.sirkb/">
Some addition to prevent default values for DRUG objects
Every property of the returnDrug class should have the namespace http://www.govtalk.gov.uk/CM/envelope.
The complete class should have the following form:
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "genericDrug", Namespace = "http://webservice.sirkb/")]
public partial class returnDRUG
{
private string dOSAGEField;
private ushort gENERIC_DRUG_IDField;
private string gENERIC_DRUG_NAMEField;
private string pHARMACEUTICAL_FORMField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public string DOSAGE
{
get
{
return this.dOSAGEField;
}
set
{
this.dOSAGEField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public ushort GENERIC_DRUG_ID
{
get
{
return this.gENERIC_DRUG_IDField;
}
set
{
this.gENERIC_DRUG_IDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public string GENERIC_DRUG_NAME
{
get
{
return this.gENERIC_DRUG_NAMEField;
}
set
{
this.gENERIC_DRUG_NAMEField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public string PHARMACEUTICAL_FORM
{
get
{
return this.pHARMACEUTICAL_FORMField;
}
set
{
this.pHARMACEUTICAL_FORMField = value;
}
}
}
I've generated a xsd file from xml and then priceResponse.cs from xsd file using xsd.exe.
Here is priceResponse.cs code:
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.6.1055.0.
//
/// <uwagi/>
[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 document {
private object[] itemsField;
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute("DATASETS", typeof(documentDATASETS), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("ERROR", typeof(documentERROR), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("PRICE", typeof(documentPRICE), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <uwagi/>
[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 documentDATASETS {
private string cOUNTRYField;
private string cURRENCYField;
private string pOSTCODEMASKField;
private string tOWNGROUPField;
private string sERVICEField;
private string oPTIONField;
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string COUNTRY {
get {
return this.cOUNTRYField;
}
set {
this.cOUNTRYField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CURRENCY {
get {
return this.cURRENCYField;
}
set {
this.cURRENCYField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string POSTCODEMASK {
get {
return this.pOSTCODEMASKField;
}
set {
this.pOSTCODEMASKField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TOWNGROUP {
get {
return this.tOWNGROUPField;
}
set {
this.tOWNGROUPField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string SERVICE {
get {
return this.sERVICEField;
}
set {
this.sERVICEField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string OPTION {
get {
return this.oPTIONField;
}
set {
this.oPTIONField = value;
}
}
}
/// <uwagi/>
[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 documentERROR {
private string cODEField;
private string dESCRIPTIONField;
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CODE {
get {
return this.cODEField;
}
set {
this.cODEField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string DESCRIPTION {
get {
return this.dESCRIPTIONField;
}
set {
this.dESCRIPTIONField = value;
}
}
}
/// <uwagi/>
[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 documentPRICE {
private string rATEIDField;
private string sERVICEField;
private string sERVICEDESCField;
private string oPTIONField;
private string oPTIONDESCField;
private string cURRENCYField;
private string rATEField;
private string rESULTField;
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string RATEID {
get {
return this.rATEIDField;
}
set {
this.rATEIDField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string SERVICE {
get {
return this.sERVICEField;
}
set {
this.sERVICEField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string SERVICEDESC {
get {
return this.sERVICEDESCField;
}
set {
this.sERVICEDESCField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string OPTION {
get {
return this.oPTIONField;
}
set {
this.oPTIONField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string OPTIONDESC {
get {
return this.oPTIONDESCField;
}
set {
this.oPTIONDESCField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CURRENCY {
get {
return this.cURRENCYField;
}
set {
this.cURRENCYField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string RATE {
get {
return this.rATEField;
}
set {
this.rATEField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string RESULT {
get {
return this.rESULTField;
}
set {
this.rESULTField = value;
}
}
}
I'm trying to deserialize priceResponse.xml but It seems like it is stuck. I think there is problem with namespaces and I'm doing deserialization wrong.
Here is xml code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document>
<DATASETS>
<COUNTRY>UTD</COUNTRY>
<CURRENCY>UTD</CURRENCY>
<POSTCODEMASK>UTD</POSTCODEMASK>
<TOWNGROUP>UTD</TOWNGROUP>
<SERVICE>UTD</SERVICE>
<OPTION>UTD</OPTION>
</DATASETS>
<ERROR>
<CODE>P13</CODE>
<DESCRIPTION>RATEID: 1 - Standard Rates</DESCRIPTION>
</ERROR>
<PRICE>
<RATEID>1</RATEID>
<SERVICE>09N</SERVICE>
<SERVICEDESC>9:00 Express</SERVICEDESC>
<OPTION>NONE</OPTION>
<OPTIONDESC>NONE</OPTIONDESC>
<CURRENCY>PLN</CURRENCY>
<RATE>712.93</RATE>
<RESULT>Y</RESULT>
</PRICE>
<PRICE>
<RATEID>1</RATEID>
<SERVICE>10N</SERVICE>
<SERVICEDESC>10:00 Express</SERVICEDESC>
<OPTION>NONE</OPTION>
<OPTIONDESC>NONE</OPTIONDESC>
<CURRENCY>PLN</CURRENCY>
<RATE>706.14</RATE>
<RESULT>Y</RESULT>
</PRICE>
<PRICE>
<RATEID>1</RATEID>
<SERVICE>12N</SERVICE>
<SERVICEDESC>12:00 Express</SERVICEDESC>
<OPTION>NONE</OPTION>
<OPTIONDESC>NONE</OPTIONDESC>
<CURRENCY>PLN</CURRENCY>
<RATE>689.84</RATE>
<RESULT>Y</RESULT>
</PRICE>
<PRICE>
<RATEID>1</RATEID>
<SERVICE>15N</SERVICE>
<SERVICEDESC>Express</SERVICEDESC>
<OPTION>NONE</OPTION>
<OPTIONDESC>NONE</OPTIONDESC>
<CURRENCY>PLN</CURRENCY>
<RATE>670.03</RATE>
<RESULT>Y</RESULT>
</PRICE>
</document>
And finally deserialization code:
File.WriteAllText("priceResponse.xml", x);
//Console.WriteLine(x);
var ser = new XmlSerializer(typeof(document),new XmlRootAttribute("documentPRICE"));
using (var reader = XmlReader.Create("priceResponse.xml"))
{
var wrapper = (document)ser.Deserialize(reader);
foreach (documentPRICE item in wrapper.Items)
{
Console.WriteLine(item.OPTIONDESC);
}
}
I would like to get each items in PRICE element, but I can't deserialize xml. What am I doing wrong ?
In this line of code,
var ser = new XmlSerializer(typeof(document),new XmlRootAttribute("documentPRICE"));
you have serialized the document object but are keeping the root of the xml as documentPRICE. And here,
var wrapper = (document)ser.Deserialize(reader);
you are trying to deserialize it to document object which does not make sense.
The xml created after serializing in your case has documentPRICE as the root object but you are trying to deserialize the same to document object, which is wrong. What you need to do is to keep document as the root while serializing ,as below.
var ser = new XmlSerializer(typeof(document), new XmlRootAttribute("document"));
And it should work.
I might going wrong direction, but I want to create generic classes for following XML structure.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<success>true</success>
<data>
<item>
<Barcode>20450004941980</Barcode>
<ChildDocuments>
<success>true</success>
<data>
<item>
<StateId>10</StateId>
</item>
</data>
<errors />
<warnings />
<info />
</ChildDocuments>
</item>
</data>
<errors />
<warnings />
<info />
</root>
There could be more elements in as well as in ChildDocuments. I've created following structure:
[Serializable()]
[System.Xml.Serialization.XmlRoot("root")]
public class XmlRoot<T>
{
public XmlRoot()
{
DataArray = new List<T>();
}
[XmlElement("data")]
public List<T> DataArray { get; set; }
}
[Serializable()]
public class XmlRootData<T>
{
public XmlRootData()
{
ItemArray = new List<T>();
}
[XmlElement("item")]
public List<T> ItemArray { get; set; }
}
and now I am thinking how to create something generic for ChildDocument. Basically it has the same generic structure as the root document.So far I know that there could be only 1 level for ChildDocument, so I can create 2 more generic classes, something like:
[Serializable()]
public class XmlRootData<T,U>
{
public XmlRootData()
{
ItemArray = new List<T>();
}
[XmlElement("item")]
public List<T> ItemArray { get; set; }
public List<XmlRoot<U>> ChildDataRoot { get; set; }
}
I can create 2 more extra classes for Child inner xml, however I am not 100% sure that there could be max 2 levels...
If generating class thru tools for your xml is an option then you can follow below steps. It will help if you have real xml file with complete data. I have given a shot based on the xml file passed in the question.
Create XSD from XML file
From XSD Create Class for it
And below is the generated Class, now based on this class you can adjust your class or use the below class as is. You can also rename the properties and customize all the names by decorating them with appropriate attributes.
//------------------------------------------------------------------------------
// <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.81.0.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.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 root {
private bool successField;
private rootData dataField;
private object errorsField;
private object warningsField;
private object infoField;
/// <remarks/>
public bool success {
get {
return this.successField;
}
set {
this.successField = value;
}
}
/// <remarks/>
public rootData data {
get {
return this.dataField;
}
set {
this.dataField = value;
}
}
/// <remarks/>
public object errors {
get {
return this.errorsField;
}
set {
this.errorsField = value;
}
}
/// <remarks/>
public object warnings {
get {
return this.warningsField;
}
set {
this.warningsField = value;
}
}
/// <remarks/>
public object info {
get {
return this.infoField;
}
set {
this.infoField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootData {
private rootDataItem itemField;
/// <remarks/>
public rootDataItem item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDataItem {
private ulong barcodeField;
private rootDataItemChildDocuments childDocumentsField;
/// <remarks/>
public ulong Barcode {
get {
return this.barcodeField;
}
set {
this.barcodeField = value;
}
}
/// <remarks/>
public rootDataItemChildDocuments ChildDocuments {
get {
return this.childDocumentsField;
}
set {
this.childDocumentsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDataItemChildDocuments {
private bool successField;
private rootDataItemChildDocumentsData dataField;
private object errorsField;
private object warningsField;
private object infoField;
/// <remarks/>
public bool success {
get {
return this.successField;
}
set {
this.successField = value;
}
}
/// <remarks/>
public rootDataItemChildDocumentsData data {
get {
return this.dataField;
}
set {
this.dataField = value;
}
}
/// <remarks/>
public object errors {
get {
return this.errorsField;
}
set {
this.errorsField = value;
}
}
/// <remarks/>
public object warnings {
get {
return this.warningsField;
}
set {
this.warningsField = value;
}
}
/// <remarks/>
public object info {
get {
return this.infoField;
}
set {
this.infoField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDataItemChildDocumentsData {
private rootDataItemChildDocumentsDataItem itemField;
/// <remarks/>
public rootDataItemChildDocumentsDataItem item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDataItemChildDocumentsDataItem {
private byte stateIdField;
/// <remarks/>
public byte StateId {
get {
return this.stateIdField;
}
set {
this.stateIdField = value;
}
}
}
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!
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);