Can someone help me to deserialize that? I tried it with xsd.exe but the part does not serialize the entries to an array of entry.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns3:OverrideEntriesRequest xmlns:ns3="urn:Bol:Main:S1:Public" xmlns="urn:Bol:Main:M1:Public" xmlns:ns2="urn:Bol:Main:Public">
<ns3:SessionHeader>
<ns2:AppLogin>user</ns2:AppLogin>
<ns2:AppPassword>password</ns2:AppPassword>
</ns3:SessionHeader>
<ns3:DataCategoryPath>\\Path\\ToData</ns3:DataCategoryPath>
<ns3:Entries>
<Entry>
<EntryName>Key1</EntryName>
<Attributes>
<EntryAttribute>
<Name>Attr1</Name>
</EntryAttribute>
<EntryAttribute>
<Name>Attr2</Name>
<Value>v1</Value>
</EntryAttribute>
<EntryAttribute>
<Name>Attr3</Name>
<Value>V2</Value>
</EntryAttribute>
</Attributes>
</Entry>
<Entry>
<EntryName>Key1</EntryName>
<Attributes>
<EntryAttribute>
<Name>Attr1</Name>
<Value>Val1</Value>
</EntryAttribute>
<EntryAttribute>
<Name>Attr2</Name>
<Value>Val2</Value>
</EntryAttribute>
<EntryAttribute>
<Name>Attr3</Name>
<Value>Val3</Value>
</EntryAttribute>
<EntryAttribute>
<Name>Attr4</Name>
<Value>Val4</Value>
</EntryAttribute>
<EntryAttribute>
<Name>Attr5</Name>
<Value>Val5</Value>
</EntryAttribute>
</Attributes>
</Entry>
</ns3:Entries>
</ns3:OverrideEntriesRequest>
</soapenv:Body>
</soapenv:Envelope>
use this classes to deserialize your xml
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/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://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{
private OverrideEntriesRequest overrideEntriesRequestField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:Bol:Main:S1:Public")]
public OverrideEntriesRequest OverrideEntriesRequest
{
get
{
return this.overrideEntriesRequestField;
}
set
{
this.overrideEntriesRequestField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:Bol:Main:S1:Public")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:Bol:Main:S1:Public", IsNullable = false)]
public partial class OverrideEntriesRequest
{
private OverrideEntriesRequestSessionHeader sessionHeaderField;
private string dataCategoryPathField;
private Entry[] entriesField;
/// <remarks/>
public OverrideEntriesRequestSessionHeader SessionHeader
{
get
{
return this.sessionHeaderField;
}
set
{
this.sessionHeaderField = value;
}
}
/// <remarks/>
public string DataCategoryPath
{
get
{
return this.dataCategoryPathField;
}
set
{
this.dataCategoryPathField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("Entry", Namespace = "urn:Bol:Main:M1:Public", IsNullable = false)]
public Entry[] Entries
{
get
{
return this.entriesField;
}
set
{
this.entriesField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:Bol:Main:S1:Public")]
public partial class OverrideEntriesRequestSessionHeader
{
private string appLoginField;
private string appPasswordField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:Bol:Main:Public")]
public string AppLogin
{
get
{
return this.appLoginField;
}
set
{
this.appLoginField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:Bol:Main:Public")]
public string AppPassword
{
get
{
return this.appPasswordField;
}
set
{
this.appPasswordField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:Bol:Main:M1:Public")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:Bol:Main:M1:Public", IsNullable = false)]
public partial class Entry
{
private string entryNameField;
private EntryEntryAttribute[] attributesField;
/// <remarks/>
public string EntryName
{
get
{
return this.entryNameField;
}
set
{
this.entryNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("EntryAttribute", IsNullable = false)]
public EntryEntryAttribute[] Attributes
{
get
{
return this.attributesField;
}
set
{
this.attributesField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:Bol:Main:M1:Public")]
public partial class EntryEntryAttribute
{
private string nameField;
private string valueField;
/// <remarks/>
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
and use the below code to deserialize
XmlSerializer deserializer = new XmlSerializer(typeof(Envelope));
TextReader reader = new StreamReader(#"D:\1.xml");
object obj = deserializer.Deserialize(reader);
Envelope XmlData = (Envelope)obj;
reader.Close();
Hope this helps.
Related
I'm trying to deserialize an XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:catalogProductListResponse>
<storeView SOAP-ENC:arrayType="ns1:catalogProductEntity[609]" xsi:type="ns1:catalogProductEntityArray">
<item xsi:type="ns1:catalogProductEntity">
<product_id xsi:type="xsd:string">36</product_id>
<sku xsi:type="xsd:string">000975</sku>
<name xsi:type="xsd:string">Beauty Dermo HTM - Vacuoterapia</name>
<set xsi:type="xsd:string">4</set>
<type xsi:type="xsd:string">simple</type>
<category_ids SOAP-ENC:arrayType="xsd:string[9]" xsi:type="ns1:ArrayOfString">
<item xsi:type="xsd:string">4</item>
</category_ids>
<website_ids SOAP-ENC:arrayType="xsd:string[1]" xsi:type="ns1:ArrayOfString">
<item xsi:type="xsd:string">1</item>
</website_ids>
</item>
<item xsi:type="ns1:catalogProductEntity">
<product_id xsi:type="xsd:string">37</product_id>
<sku xsi:type="xsd:string">004279</sku>
<name xsi:type="xsd:string">Effect HTM - RadiofrequĂȘncia</name>
<set xsi:type="xsd:string">4</set>
<type xsi:type="xsd:string">simple</type>
<category_ids SOAP-ENC:arrayType="xsd:string[8]" xsi:type="ns1:ArrayOfString">
<item xsi:type="xsd:string">4</item>
</category_ids>
<website_ids SOAP-ENC:arrayType="xsd:string[1]" xsi:type="ns1:ArrayOfString">
<item xsi:type="xsd:string">1</item>
</website_ids>
</item>
...[OTHER MANY ITENS]
</storeView>
</ns1:catalogProductListResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
In this class
public class ProductListResponse
{
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{
private EnvelopeBody bodyField;
private string encodingStyleField;
/// <remarks/>
public EnvelopeBody Body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public string encodingStyle
{
get
{
return this.encodingStyleField;
}
set
{
this.encodingStyleField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{
private catalogProductListResponse catalogProductListResponseField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:Magento")]
public catalogProductListResponse catalogProductListResponse
{
get
{
return this.catalogProductListResponseField;
}
set
{
this.catalogProductListResponseField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:Magento")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:Magento", IsNullable = false)]
public partial class catalogProductListResponse
{
private storeView storeViewField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("storeView")]
public storeView storeView
{
get
{
return this.storeViewField;
}
set
{
this.storeViewField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "catalogProductEntityArray")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class storeView
{
private storeViewItem[] itemField;
private string arrayTypeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("item")]
public storeViewItem[] item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public string arrayType
{
get
{
return this.arrayTypeField;
}
set
{
this.arrayTypeField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "catalogProductEntity")]
public partial class storeViewItem
{
private ushort product_idField;
private string skuField;
private string nameField;
private byte setField;
private string typeField;
private storeViewItemCategory_ids category_idsField;
private storeViewItemWebsite_ids website_idsField;
/// <remarks/>
public ushort product_id
{
get
{
return this.product_idField;
}
set
{
this.product_idField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string sku
{
get
{
return this.skuField;
}
set
{
this.skuField = value;
}
}
/// <remarks/>
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
public byte set
{
get
{
return this.setField;
}
set
{
this.setField = value;
}
}
/// <remarks/>
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
public storeViewItemCategory_ids category_ids
{
get
{
return this.category_idsField;
}
set
{
this.category_idsField = value;
}
}
/// <remarks/>
public storeViewItemWebsite_ids website_ids
{
get
{
return this.website_idsField;
}
set
{
this.website_idsField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class storeViewItemCategory_ids
{
private byte[] itemField;
private string arrayTypeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("item")]
public byte[] item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public string arrayType
{
get
{
return this.arrayTypeField;
}
set
{
this.arrayTypeField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class storeViewItemWebsite_ids
{
private byte itemField;
private string arrayTypeField;
/// <remarks/>
public byte item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public string arrayType
{
get
{
return this.arrayTypeField;
}
set
{
this.arrayTypeField = value;
}
}
}
}
With the code
var deserializedDocument = SerializacaoHelper.Deserialize<ProductListResponse.Envelope>(xmlDocument);
public static T Deserialize<T>(XmlDocument xmlDoc)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
MemoryStream xmlStream = new MemoryStream();
xmlDoc.Save(xmlStream);
xmlStream.Position = 0;
T obj = (T)serializer.Deserialize(xmlStream);
xmlStream.Flush();
return obj;
}
The class was generated with the Visual Studio automatic generator. To simulate it is due, with the XML in the clipboard, to follow the steps Edit > Paste Special > Paste XML as Classes.
The function that deserializes is being executed, however, the storeView attribute is null, even having child nodes inside it in the XML.
Where is the error? In class? In the deserialization code? I can not understand why the StoreView attribute is null if in XML it has data.
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'm having trouble de-serializing an XML document.
<rules version="61">
<emie>
<domain exclude="false">facebook.com</domain>
<domain exclude="false">google.com</domain>
<domain exclude="false">bbc.co.uk</domain>
</emie>
<docMode>
<domain docMode="7">outlook.com</domain>
<domain docMode="7">yahoo.com</domain>
</docMode>
</rules>
I've copied and pasted into visual studio - using the Paste XML as class and that's created the following class
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class rules
{
private rulesDomain[] emieField;
private rulesDomain1[] docModeField;
private byte versionField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("domain", IsNullable = false)]
public rulesDomain[] emie
{
get
{
return this.emieField;
}
set
{
this.emieField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("domain", IsNullable = false)]
public rulesDomain1[] docMode
{
get
{
return this.docModeField;
}
set
{
this.docModeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte version
{
get
{
return this.versionField;
}
set
{
this.versionField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rulesDomain
{
private bool excludeField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool exclude
{
get
{
return this.excludeField;
}
set
{
this.excludeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rulesDomain1
{
private string docModeField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string docMode
{
get
{
return this.docModeField;
}
set
{
this.docModeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
I then run the following code to deserialise
XmlSerializer mySerializer = new XmlSerializer(typeof(XmlSerializer));
Classes.rules r;
using (StreamReader reader = new StreamReader(#"file.xml"))
{
r = (Classes.rules)mySerializer.Deserialize(reader);
}
It's errors on the line r =... {" was not expected."}
I'm pretty certain the error lies in the class and the declarations at the top but I haven't been able to change it so it works, could anyone help?
Change
XmlSerializer mySerializer = new XmlSerializer(typeof(XmlSerializer));
to
XmlSerializer mySerializer = new XmlSerializer(typeof(Classes.rules));
Its parameter is type of the object that this XmlSerializer can serialize.
After Posting to a certain URL, I get the following Response:
<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode>123</faultcode>
<faultstring>Lorem Ipsum</faultstring>
<detail>
<ns1:Service xmlns:ns1="http://www.bla.org/">
<messageId>321</messageId>
<text>Test Text</text>
<variables>19</variables>
<variables>20</variables>
</ns1:Service>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
I want to Parse it and use the Values
I tried to Parse the XmlString to the following Class:
public class ClassXml
{
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/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://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{
private EnvelopeBodyFault faultField;
/// <remarks/>
public EnvelopeBodyFault Fault
{
get
{
return this.faultField;
}
set
{
this.faultField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBodyFault
{
private string faultcodeField;
private string faultstringField;
private detail detailField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
public string faultcode
{
get
{
return this.faultcodeField;
}
set
{
this.faultcodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
public string faultstring
{
get
{
return this.faultstringField;
}
set
{
this.faultstringField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
public detail detail
{
get
{
return this.detailField;
}
set
{
this.detailField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class detail
{
private Service serviceField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.bla.org/")]
public Service Service
{
get
{
return this.serviceField;
}
set
{
this.serviceField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.bla.org/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.bla.org/", IsNullable = false)]
public partial class Service
{
private string messageIdField;
private string textField;
private ulong[] variablesField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
public string messageId
{
get
{
return this.messageIdField;
}
set
{
this.messageIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
public string text
{
get
{
return this.textField;
}
set
{
this.textField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("variables", Namespace = "")]
public ulong[] variables
{
get
{
return this.variablesField;
}
set
{
this.variablesField = value;
}
}
}
}
But I got the error:
Exception Occured while trying to parse XML String
<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'> was not expected.
Apparently I can't parse an XML with namespaces like I parse a normal XML
How can I parse this one and get the values?
Any help would be appreciated
I have the following xml doc that I am trying to parse:
<report>
<fruit name="Apple" count="5"/>
<vegetable name="Potato" count="2"/>
<vegetable name="Tomato" count="3"/>
<fruit name="Orange" count="0"/>
</report>
I have the following class to deserialize:
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class report
{
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("fruit", typeof(reportFruit))]
[System.Xml.Serialization.XmlElementAttribute("vegetable", typeof(reportVegetable))]
public object[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class reportFruit
{
private string nameField;
private byte countField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte count
{
get
{
return this.countField;
}
set
{
this.countField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class reportVegetable
{
private string nameField;
private byte countField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte count
{
get
{
return this.countField;
}
set
{
this.countField = value;
}
}
}
How can I iterate through the fruits and vegetables after it has been deserialized?
foreach (string name in instance.Items) // doesnt work
You can do this:
for(int i = 0; i < instance.Items.Length; i++)
{
object item = instance.Items[i];
if(item is reportFruit) {
// it is a fruit!!!
reportFruit fruit = (reportFruit)item;
}
if(item is reportVegetable) {
// it is not a fruit :'(
reportVegetable vegetable = (reportVegetable)item;
}
}