I have tried to read XML document and i have found a method of generating XSD file into a C# class.
XML Document :
<?xml version="1.0" standalone="yes"?>
<sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/sdnList.xsd">
<publshInformation>
<Publish_Date>12/15/2011</Publish_Date>
<Record_Count>5052</Record_Count>
</publshInformation>
<sdnEntry>
<uid>11691</uid>
<lastName>SERVICIOS DE CONTROL INTEGRAL DE OBRAS S.L. SIP SUCURSAL CARTAGENA</lastName>
<sdnType>Entity</sdnType>
<programList>
<program>SDNT</program>
</programList>
<idList>
<id>
<uid>6028</uid>
<idType>NIT #</idType>
<idNumber>900106267-0</idNumber>
<idCountry>Colombia</idCountry>
</id>
</idList>
<addressList>
<address>
<uid>17543</uid>
<address1>Carrera 3 No. 8-38</address1>
<city>Cartagena</city>
<country>Colombia</country>
</address>
<address>
<uid>17544</uid>
<address1>Carrera 4 No. 8-41</address1>
<city>Cartagena</city>
<country>Colombia</country>
</address>
</addressList>
</sdnEntry>
<sdnEntry>
<uid>11692</uid>
<lastName>INVERSIONES EL PROGRESO S.A.</lastName>
<sdnType>Entity</sdnType>
<programList>
<program>SDNT</program>
</programList>
<idList>
<id>
<uid>6027</uid>
<idType>NIT #</idType>
<idNumber>806006517-7</idNumber>
<idCountry>Colombia</idCountry>
</id>
</idList>
<akaList>
<aka>
<uid>12373</uid>
<type>a.k.a.</type>
<category>weak</category>
<lastName>I.P. S.A.</lastName>
</aka>
</akaList>
<addressList>
<address>
<uid>17540</uid>
<address1>Carrera 3 No. 8-38 Ofc. 1</address1>
<city>Cartagena</city>
<country>Colombia</country>
</address>
<address>
<uid>17541</uid>
<address1>Carrera 4 No. 8-41</address1>
<city>Cartagena</city>
<country>Colombia</country>
</address>
<address>
<uid>17542</uid>
<address1>Olaya Herrera Carrera 68 No. 32B-45</address1>
<city>Cartagena</city>
<country>Colombia</country>
</address>
</addressList>
</sdnEntry>
Generated C# Class by XSD tool
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.5448
//
// 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=2.0.50727.42.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/sdnList.xsd", IsNullable = false)]
public partial class sdnList
{
private sdnListPublshInformation publshInformationField;
private sdnListSdnEntry[] sdnEntryField;
/// <remarks/>
public sdnListPublshInformation publshInformation
{
get
{
return this.publshInformationField;
}
set
{
this.publshInformationField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("sdnEntry")]
public sdnListSdnEntry[] sdnEntry
{
get
{
return this.sdnEntryField;
}
set
{
this.sdnEntryField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListPublshInformation
{
private string publish_DateField;
private int record_CountField;
private bool record_CountFieldSpecified;
/// <remarks/>
public string Publish_Date
{
get
{
return this.publish_DateField;
}
set
{
this.publish_DateField = value;
}
}
/// <remarks/>
public int Record_Count
{
get
{
return this.record_CountField;
}
set
{
this.record_CountField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool Record_CountSpecified
{
get
{
return this.record_CountFieldSpecified;
}
set
{
this.record_CountFieldSpecified = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntry
{
private int uidField;
private string firstNameField;
private string lastNameField;
private string titleField;
private string sdnTypeField;
private string remarksField;
private string[] programListField;
private sdnListSdnEntryID[] idListField;
private sdnListSdnEntryAka[] akaListField;
private sdnListSdnEntryAddress[] addressListField;
private sdnListSdnEntryNationality[] nationalityListField;
private sdnListSdnEntryCitizenship[] citizenshipListField;
private sdnListSdnEntryDateOfBirthItem[] dateOfBirthListField;
private sdnListSdnEntryPlaceOfBirthItem[] placeOfBirthListField;
private sdnListSdnEntryVesselInfo vesselInfoField;
/// <remarks/>
public int uid
{
get
{
return this.uidField;
}
set
{
this.uidField = value;
}
}
/// <remarks/>
public string firstName
{
get
{
return this.firstNameField;
}
set
{
this.firstNameField = value;
}
}
/// <remarks/>
public string lastName
{
get
{
return this.lastNameField;
}
set
{
this.lastNameField = value;
}
}
/// <remarks/>
public string title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
/// <remarks/>
public string sdnType
{
get
{
return this.sdnTypeField;
}
set
{
this.sdnTypeField = value;
}
}
/// <remarks/>
public string remarks
{
get
{
return this.remarksField;
}
set
{
this.remarksField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("program", IsNullable = false)]
public string[] programList
{
get
{
return this.programListField;
}
set
{
this.programListField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("id", IsNullable = false)]
public sdnListSdnEntryID[] idList
{
get
{
return this.idListField;
}
set
{
this.idListField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("aka", IsNullable = false)]
public sdnListSdnEntryAka[] akaList
{
get
{
return this.akaListField;
}
set
{
this.akaListField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("address", IsNullable = false)]
public sdnListSdnEntryAddress[] addressList
{
get
{
return this.addressListField;
}
set
{
this.addressListField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("nationality", IsNullable = false)]
public sdnListSdnEntryNationality[] nationalityList
{
get
{
return this.nationalityListField;
}
set
{
this.nationalityListField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("citizenship", IsNullable = false)]
public sdnListSdnEntryCitizenship[] citizenshipList
{
get
{
return this.citizenshipListField;
}
set
{
this.citizenshipListField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("dateOfBirthItem", IsNullable = false)]
public sdnListSdnEntryDateOfBirthItem[] dateOfBirthList
{
get
{
return this.dateOfBirthListField;
}
set
{
this.dateOfBirthListField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("placeOfBirthItem", IsNullable = false)]
public sdnListSdnEntryPlaceOfBirthItem[] placeOfBirthList
{
get
{
return this.placeOfBirthListField;
}
set
{
this.placeOfBirthListField = value;
}
}
/// <remarks/>
public sdnListSdnEntryVesselInfo vesselInfo
{
get
{
return this.vesselInfoField;
}
set
{
this.vesselInfoField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryID
{
private int uidField;
private string idTypeField;
private string idNidCountryumberField;
private string idCountryField;
private string issueDateField;
private string expirationDateField;
/// <remarks/>
public int uid
{
get
{
return this.uidField;
}
set
{
this.uidField = value;
}
}
/// <remarks/>
public string idType
{
get
{
return this.idTypeField;
}
set
{
this.idTypeField = value;
}
}
/// <remarks/>
public string idNidCountryumber
{
get
{
return this.idNidCountryumberField;
}
set
{
this.idNidCountryumberField = value;
}
}
/// <remarks/>
public string idCountry
{
get
{
return this.idCountryField;
}
set
{
this.idCountryField = value;
}
}
/// <remarks/>
public string issueDate
{
get
{
return this.issueDateField;
}
set
{
this.issueDateField = value;
}
}
/// <remarks/>
public string expirationDate
{
get
{
return this.expirationDateField;
}
set
{
this.expirationDateField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryAka
{
private int uidField;
private string typeField;
private string categoryField;
private string lastNameField;
private string firstNameField;
/// <remarks/>
public int uid
{
get
{
return this.uidField;
}
set
{
this.uidField = value;
}
}
/// <remarks/>
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
public string category
{
get
{
return this.categoryField;
}
set
{
this.categoryField = value;
}
}
/// <remarks/>
public string lastName
{
get
{
return this.lastNameField;
}
set
{
this.lastNameField = value;
}
}
/// <remarks/>
public string firstName
{
get
{
return this.firstNameField;
}
set
{
this.firstNameField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryAddress
{
private int uidField;
private string address1Field;
private string address2Field;
private string address3Field;
private string cityField;
private string stateOrProvinceField;
private string postalCodeField;
private string countryField;
/// <remarks/>
public int uid
{
get
{
return this.uidField;
}
set
{
this.uidField = 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 city
{
get
{
return this.cityField;
}
set
{
this.cityField = value;
}
}
/// <remarks/>
public string stateOrProvince
{
get
{
return this.stateOrProvinceField;
}
set
{
this.stateOrProvinceField = value;
}
}
/// <remarks/>
public string postalCode
{
get
{
return this.postalCodeField;
}
set
{
this.postalCodeField = value;
}
}
/// <remarks/>
public string country
{
get
{
return this.countryField;
}
set
{
this.countryField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryNationality
{
private int uidField;
private string countryField;
private bool mainEntryField;
/// <remarks/>
public int uid
{
get
{
return this.uidField;
}
set
{
this.uidField = value;
}
}
/// <remarks/>
public string country
{
get
{
return this.countryField;
}
set
{
this.countryField = value;
}
}
/// <remarks/>
public bool mainEntry
{
get
{
return this.mainEntryField;
}
set
{
this.mainEntryField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryCitizenship
{
private int uidField;
private string countryField;
private bool mainEntryField;
/// <remarks/>
public int uid
{
get
{
return this.uidField;
}
set
{
this.uidField = value;
}
}
/// <remarks/>
public string country
{
get
{
return this.countryField;
}
set
{
this.countryField = value;
}
}
/// <remarks/>
public bool mainEntry
{
get
{
return this.mainEntryField;
}
set
{
this.mainEntryField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryDateOfBirthItem
{
private int uidField;
private string dateOfBirthField;
private bool mainEntryField;
/// <remarks/>
public int uid
{
get
{
return this.uidField;
}
set
{
this.uidField = value;
}
}
/// <remarks/>
public string dateOfBirth
{
get
{
return this.dateOfBirthField;
}
set
{
this.dateOfBirthField = value;
}
}
/// <remarks/>
public bool mainEntry
{
get
{
return this.mainEntryField;
}
set
{
this.mainEntryField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryPlaceOfBirthItem
{
private int uidField;
private string placeOfBirthField;
private bool mainEntryField;
/// <remarks/>
public int uid
{
get
{
return this.uidField;
}
set
{
this.uidField = value;
}
}
/// <remarks/>
public string placeOfBirth
{
get
{
return this.placeOfBirthField;
}
set
{
this.placeOfBirthField = value;
}
}
/// <remarks/>
public bool mainEntry
{
get
{
return this.mainEntryField;
}
set
{
this.mainEntryField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryVesselInfo
{
private string callSignField;
private string vesselTypeField;
private string vesselFlagField;
private string vesselOwnerField;
private int tonnageField;
private bool tonnageFieldSpecified;
private int grossRegisteredTonnageField;
private bool grossRegisteredTonnageFieldSpecified;
/// <remarks/>
public string callSign
{
get
{
return this.callSignField;
}
set
{
this.callSignField = value;
}
}
/// <remarks/>
public string vesselType
{
get
{
return this.vesselTypeField;
}
set
{
this.vesselTypeField = value;
}
}
/// <remarks/>
public string vesselFlag
{
get
{
return this.vesselFlagField;
}
set
{
this.vesselFlagField = value;
}
}
/// <remarks/>
public string vesselOwner
{
get
{
return this.vesselOwnerField;
}
set
{
this.vesselOwnerField = value;
}
}
/// <remarks/>
public int tonnage
{
get
{
return this.tonnageField;
}
set
{
this.tonnageField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool tonnageSpecified
{
get
{
return this.tonnageFieldSpecified;
}
set
{
this.tonnageFieldSpecified = value;
}
}
/// <remarks/>
public int grossRegisteredTonnage
{
get
{
return this.grossRegisteredTonnageField;
}
set
{
this.grossRegisteredTonnageField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool grossRegisteredTonnageSpecified
{
get
{
return this.grossRegisteredTonnageFieldSpecified;
}
set
{
this.grossRegisteredTonnageFieldSpecified = value;
}
}
}
My code :
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace Validus.New.Domain
{
class Testdeserialize
{
public void test()
{
sdnList sdnEntry = null;
string path = "D:/sdn1.xml";
XmlSerializer serializer = new XmlSerializer(typeof(sdnList));
StreamReader reader = new StreamReader(path);
sdnEntry = (sdnList)serializer.Deserialize(reader);
reader.Close();
}
}
}
But it doesn't work.
So how to deserialize this XML Document to C# class object ?
I dont think you have any issues with serializer/deserializer, rather you got problem here:
StreamReader reader = new StreamReader(path);
sdnEntry = (sdnList)serializer.Deserialize(reader);
reader.Close();
Try instead:
using(var fs = File.OpenRead(path))
{
sdnEntry = (sdnList)serializer.Deserialize(reader);
}
Related
I am using a Web Service (wsdl), and I need to add a XML(that I receive) to my request, I added the web reference to Visual Studio and it generated a few classes.
I need to add a rDE instance to my request so I tried deserializing the XML using the generated classes, it doesn't throw an Exception but all the properties are null, I have been able to send the same XML through postman so the XML is ok, I don't know if I have the correct approach here since is my first time doing this.
These are the generated classes
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="*")]
public partial class rDE {
private string dVerForField;
private DE deField;
private gCamFuFD gCamFuFDField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
public string dVerFor {
get {
return this.dVerForField;
}
set {
this.dVerForField = value;
}
}
/// <remarks/>
public DE DE {
get {
return this.deField;
}
set {
this.deField = value;
}
}
/// <remarks/>
public gCamFuFD gCamFuFD {
get {
return this.gCamFuFDField;
}
set {
this.gCamFuFDField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="*")]
public partial class DE {
private string dDVIdField;
private System.DateTime dFecFirmaField;
private bool dFecFirmaFieldSpecified;
private string dSisFactField;
private gOpeDE gOpeDEField;
private gTimb gTimbField;
private gDatGralOpe gDatGralOpeField;
private gDtipDE gDtipDEField;
private gTotSub gTotSubField;
private gCamGen gCamGenField;
private gCamDEAsoc[] gCamDEAsocField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
public string dDVId {
get {
return this.dDVIdField;
}
set {
this.dDVIdField = value;
}
}
/// <remarks/>
public System.DateTime dFecFirma {
get {
return this.dFecFirmaField;
}
set {
this.dFecFirmaField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool dFecFirmaSpecified {
get {
return this.dFecFirmaFieldSpecified;
}
set {
this.dFecFirmaFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
public string dSisFact {
get {
return this.dSisFactField;
}
set {
this.dSisFactField = value;
}
}
/// <remarks/>
public gOpeDE gOpeDE {
get {
return this.gOpeDEField;
}
set {
this.gOpeDEField = value;
}
}
/// <remarks/>
public gTimb gTimb {
get {
return this.gTimbField;
}
set {
this.gTimbField = value;
}
}
/// <remarks/>
public gDatGralOpe gDatGralOpe {
get {
return this.gDatGralOpeField;
}
set {
this.gDatGralOpeField = value;
}
}
/// <remarks/>
public gDtipDE gDtipDE {
get {
return this.gDtipDEField;
}
set {
this.gDtipDEField = value;
}
}
/// <remarks/>
public gTotSub gTotSub {
get {
return this.gTotSubField;
}
set {
this.gTotSubField = value;
}
}
/// <remarks/>
public gCamGen gCamGen {
get {
return this.gCamGenField;
}
set {
this.gCamGenField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("gCamDEAsoc")]
public gCamDEAsoc[] gCamDEAsoc {
get {
return this.gCamDEAsocField;
}
set {
this.gCamDEAsocField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="*")]
public partial class gOpeDE {
private string iTipEmiField;
private string dDesTipEmiField;
private string dCodSegField;
private string dInfoEmiField;
private string dInfoFiscField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
public string iTipEmi {
get {
return this.iTipEmiField;
}
set {
this.iTipEmiField = value;
}
}
/// <remarks/>
public string dDesTipEmi {
get {
return this.dDesTipEmiField;
}
set {
this.dDesTipEmiField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
public string dCodSeg {
get {
return this.dCodSegField;
}
set {
this.dCodSegField = value;
}
}
/// <remarks/>
public string dInfoEmi {
get {
return this.dInfoEmiField;
}
set {
this.dInfoEmiField = value;
}
}
/// <remarks/>
public string dInfoFisc {
get {
return this.dInfoFiscField;
}
set {
this.dInfoFiscField = value;
}
}
}
and this is my XML(Its Elements may vary, it can have more or less elements but this is the XML I have been using to test)
<rDE>
<DE>
<gOpeDE>
<iTipEmi>1</iTipEmi>
</gOpeDE>
<gTimb>
<iTiDE>1</iTiDE>
<dNumTim>14601619</dNumTim>
<dEst>9</dEst>
<dPunExp>9</dPunExp>
<dNumDoc>22712</dNumDoc>
</gTimb>
<gDatGralOpe>
<dFeEmiDE>12/04/2022</dFeEmiDE>
<gOpeCom>
<iTipTra>1</iTipTra>
<iTImp>1</iTImp>
<cMoneOpe>PYG</cMoneOpe>
</gOpeCom>
<gEmis>
<dRucEm>800486946</dRucEm>
<dDVEmi>4</dDVEmi>
</gEmis>
<gDatRec>
<iNatRec>1</iNatRec>
<iTiOpe>1</iTiOpe>
<dRucRec>5307630</dRucRec>
<dDVRec>3</dDVRec>
<iTiContRec>1</iTiContRec>
<dNomRec>BARRETO MENDEZ EDER MATIAS</dNomRec>
<dDirRec>A</dDirRec>
<dNumCasRec>0</dNumCasRec>
<cPaisRec>PRY</cPaisRec>
<dDesPaisRe>Paraguay</dDesPaisRe>
</gDatRec>
</gDatGralOpe>
<gDtipDE>
<gCamFE>
<iIndPres>1</iIndPres>
</gCamFE>
<gCamItem>
<dCodInt>614143297809</dCodInt>
<dDesProSer>R CAPELETIS - POLLO 500GR PAQ </dDesProSer>
<dCantProSer>3.00</dCantProSer>
<gValorItem>
<dPUniProSer>12500</dPUniProSer>
<dTotBruOpeItem>37500</dTotBruOpeItem>
<gValorRestaItem>
<dDescItem>0</dDescItem>
<dTotOpeItem>37500</dTotOpeItem>
</gValorRestaItem>
</gValorItem>
<gCamIVA>
<iAfecIVA>1</iAfecIVA>
<dTasaIVA>10</dTasaIVA>
</gCamIVA>
</gCamItem>
<gCamCond>
<iCondOpe>1</iCondOpe>
</gCamCond>
</gDtipDE>
<gTotSub>
<dSubExe>0</dSubExe>
<dSub5>0</dSub5>
<dSub10>37500</dSub10>
<dTotOpe>37500</dTotOpe>
<dRedon>0</dRedon>
</gTotSub>
</DE>
</rDE>
This is where I deserialize
public rDE deserializeXML(string xml)
{
rDE obj = new rDE();
XmlSerializer serializer = new XmlSerializer(typeof(rDE));
byte[] byteArray = Encoding.UTF8.GetBytes(xml);
MemoryStream stream = new MemoryStream(byteArray);
StreamReader reader = new StreamReader(stream);
obj = (rDE)serializer.Deserialize(reader);
return obj;
}
I am getting this error when I run my code
System.InvalidOperationException
HResult=0x80131509
Message=HamiltonXML.Program is inaccessible due to its protection
level. Only public types can be processed.
Source=System.Xmlenter code here
StackTrace:
at System.Xml.Serialization.TypeDesc.CheckSupported()
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type,
MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at HamiltonXML.Program.Main(String[] args) in C:\Users\Aube\source\repos\HamiltonProject\HamiltonProject\Program.cs:line 18
I've changed all classes to public and rechecked all of the code yet the problem still occurs. Here is the code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace HamiltonXML
{
class Program
{
static void Main(string[] args)
{
EnvironmentNodeConfigurationParameters encp = null;
string path = "C:/Users/Aube/Desktop/FreezerEnvironmentNodeParameters_Freezer1.xml";
XmlSerializer serializer = new XmlSerializer(typeof(EnvironmentNodeConfigurationParameters));
StreamReader reader = new StreamReader(path);
encp = (EnvironmentNodeConfigurationParameters)serializer.Deserialize(reader);
reader.Close();
}
// 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)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class EnvironmentNodeConfigurationParameters
{
private EnvironmentNodeConfigurationParametersComplexNodeConfigurations[] complexDeviceParameterListField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("ComplexNodeConfigurations", IsNullable = false)]
public EnvironmentNodeConfigurationParametersComplexNodeConfigurations[] ComplexDeviceParameterList
{
get
{
return this.complexDeviceParameterListField;
}
set
{
this.complexDeviceParameterListField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class EnvironmentNodeConfigurationParametersComplexNodeConfigurations
{
private EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurations[] complexNodeParameterListField;
private string subDeviceIDField;
private string subDeviceNameField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("NodeConfigurations", IsNullable = false)]
public EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurations[] ComplexNodeParameterList
{
get
{
return this.complexNodeParameterListField;
}
set
{
this.complexNodeParameterListField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string SubDeviceID
{
get
{
return this.subDeviceIDField;
}
set
{
this.subDeviceIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string SubDeviceName
{
get
{
return this.subDeviceNameField;
}
set
{
this.subDeviceNameField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurations
{
private EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParams environmentLinkParamsField;
private byte nodeIDField;
private string nodeNameField;
private string dependNodeNameField;
private string registerTypeField;
private byte startRegField;
private byte numRegsField;
private string nodeTypeField;
private string valueTypeField;
private string environmentLinkTypeField;
private string guidField;
private bool enabledField;
private string unitsField;
private bool monitoredField;
private byte reportItemField;
/// <remarks/>
public EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParams EnvironmentLinkParams
{
get
{
return this.environmentLinkParamsField;
}
set
{
this.environmentLinkParamsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte NodeID
{
get
{
return this.nodeIDField;
}
set
{
this.nodeIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string NodeName
{
get
{
return this.nodeNameField;
}
set
{
this.nodeNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DependNodeName
{
get
{
return this.dependNodeNameField;
}
set
{
this.dependNodeNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string RegisterType
{
get
{
return this.registerTypeField;
}
set
{
this.registerTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte StartReg
{
get
{
return this.startRegField;
}
set
{
this.startRegField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte NumRegs
{
get
{
return this.numRegsField;
}
set
{
this.numRegsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string NodeType
{
get
{
return this.nodeTypeField;
}
set
{
this.nodeTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ValueType
{
get
{
return this.valueTypeField;
}
set
{
this.valueTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string EnvironmentLinkType
{
get
{
return this.environmentLinkTypeField;
}
set
{
this.environmentLinkTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Guid
{
get
{
return this.guidField;
}
set
{
this.guidField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool Enabled
{
get
{
return this.enabledField;
}
set
{
this.enabledField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Units
{
get
{
return this.unitsField;
}
set
{
this.unitsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool Monitored
{
get
{
return this.monitoredField;
}
set
{
this.monitoredField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte ReportItem
{
get
{
return this.reportItemField;
}
set
{
this.reportItemField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParams
{
private EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParamsKeyValuePair keyValuePairField;
/// <remarks/>
public EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParamsKeyValuePair KeyValuePair
{
get
{
return this.keyValuePairField;
}
set
{
this.keyValuePairField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParamsKeyValuePair
{
private string keyField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Key
{
get
{
return this.keyField;
}
set
{
this.keyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
}
}
The code is supposed to deserialize an XML file.
Have you tried changing your Program class to public?
namespace HamiltonXML
{
public class Program
{
I want to get XML format data using OAI-PMH protocol. when I deserialize XML for the fisrt time using XmlSerializer, there is no problem, but when I try to get url content by a resumption token, I got the below error when trying to deserialize XML, eventhough the xmlreader correctly reads content:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
Additional information: There is an error in XML document (149, 26).
InnerException:Value was either too large or too small for an unsigned byte.
the code is like this:
//getting content for the first time
string urlContent = BaseCommons.HttpHandler.getContent(first-time-url-link);
StreamReader OAIStream = new StreamReader(new MemoryStream(
Encoding.UTF8.GetBytes(urlContent)));
var reader = XmlReader.Create(
OAIStream.BaseStream, new XmlReaderSettings() {
ConformanceLevel = ConformanceLevel.Document });
OAI.OAIPMH OAIRes = new XmlSerializer(typeof(OAI.OAIPMH)).Deserialize(
reader) as OAI.OAIPMH;
//getting content for more than once
while (OAIRes.ListRecords.resumptionToken.Value != null &&
OAIRes.ListRecords.resumptionToken.Value.ToString() != "")
{
string nextUrl = BaseCommons.HttpHandler.getContent(second-url-with-resumption +
OAIRes.ListRecords.resumptionToken.Value.ToString());
StreamReader OAIStream1 = new StreamReader(
new MemoryStream(Encoding.UTF8.GetBytes(nextUrl)));
//xmlreader correctly reads content
XmlReader reader1 = XmlReader.Create(OAIStream1.BaseStream, new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
// the error occured here
OAI.OAIPMH OAIRes1 = new XmlSerializer(typeof(OAI.OAIPMH)).Deserialize(reader1) as OAI.OAIPMH;
}
According to the Microsoft Exception explanation, I added a default constructor to my OAIPMH partial class, but the same error occurred.
the OAI class which is xml object class is like below:
public class OAI{
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
[System.Xml.Serialization.XmlRootAttribute("OAI-PMH", Namespace="http://www.openarchives.org/OAI/2.0/", IsNullable = false)]
public partial class OAIPMH
{
private System.DateTime responseDateField;
private OAIPMHRequest requestField;
private OAIPMHListRecords listRecordsField;
/// <remarks/>
public OAIPMH() { }
public System.DateTime responseDate
{
get
{
return this.responseDateField;
}
set
{
this.responseDateField = value;
}
}
/// <remarks/>
public OAIPMHRequest request
{
get
{
return this.requestField;
}
set
{
this.requestField = value;
}
}
/// <remarks/>
public OAIPMHListRecords ListRecords
{
get
{
return this.listRecordsField;
}
set
{
this.listRecordsField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
public partial class OAIPMHRequest
{
private string verbField;
private string metadataPrefixField;
private System.DateTime fromField;
private System.DateTime untilField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string verb
{
get
{
return this.verbField;
}
set
{
this.verbField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string metadataPrefix
{
get
{
return this.metadataPrefixField;
}
set
{
this.metadataPrefixField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType = "date")]
public System.DateTime from
{
get
{
return this.fromField;
}
set
{
this.fromField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType = "date")]
public System.DateTime until
{
get
{
return this.untilField;
}
set
{
this.untilField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
public partial class OAIPMHListRecords
{
private OAIPMHListRecordsRecord[] recordField;
private OAIPMHListRecordsResumptionToken resumptionTokenField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("record")]
public OAIPMHListRecordsRecord[] record
{
get
{
return this.recordField;
}
set
{
this.recordField = value;
}
}
/// <remarks/>
public OAIPMHListRecordsResumptionToken resumptionToken
{
get
{
return this.resumptionTokenField;
}
set
{
this.resumptionTokenField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
public partial class OAIPMHListRecordsRecord
{
private OAIPMHListRecordsRecordHeader headerField;
private OAIPMHListRecordsRecordMetadata metadataField;
/// <remarks/>
public OAIPMHListRecordsRecordHeader header
{
get
{
return this.headerField;
}
set
{
this.headerField = value;
}
}
/// <remarks/>
public OAIPMHListRecordsRecordMetadata metadata
{
get
{
return this.metadataField;
}
set
{
this.metadataField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
public partial class OAIPMHListRecordsRecordHeader
{
private string identifierField;
private System.DateTime datestampField;
private string setSpecField;
private string statusField;
/// <remarks/>
public string identifier
{
get
{
return this.identifierField;
}
set
{
this.identifierField = value;
}
}
/// <remarks/>
public System.DateTime datestamp
{
get
{
return this.datestampField;
}
set
{
this.datestampField = value;
}
}
/// <remarks/>
public string setSpec
{
get
{
return this.setSpecField;
}
set
{
this.setSpecField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string status
{
get
{
return this.statusField;
}
set
{
this.statusField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
public partial class OAIPMHListRecordsRecordMetadata
{
private mods modsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.loc.gov/mods/v3")]
public mods mods
{
get
{
return this.modsField;
}
set
{
this.modsField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.loc.gov/mods/v3", IsNullable = false)]
public partial class mods
{
private modsTitleInfo titleInfoField;
private modsName[] nameField;
private string typeOfResourceField;
private string genreField;
private modsOriginInfo originInfoField;
private modsLanguage languageField;
private modsAbstract[] abstractField;
private modsSubject subjectField;
private modsRelatedItem relatedItemField;
/// <remarks/>
public modsTitleInfo titleInfo
{
get
{
return this.titleInfoField;
}
set
{
this.titleInfoField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("name")]
public modsName[] name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
public string typeOfResource
{
get
{
return this.typeOfResourceField;
}
set
{
this.typeOfResourceField = value;
}
}
/// <remarks/>
public string genre
{
get
{
return this.genreField;
}
set
{
this.genreField = value;
}
}
/// <remarks/>
public modsOriginInfo originInfo
{
get
{
return this.originInfoField;
}
set
{
this.originInfoField = value;
}
}
/// <remarks/>
public modsLanguage language
{
get
{
return this.languageField;
}
set
{
this.languageField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("abstract")]
public modsAbstract[] #abstract
{
get
{
return this.abstractField;
}
set
{
this.abstractField = value;
}
}
/// <remarks/>
public modsSubject subject
{
get
{
return this.subjectField;
}
set
{
this.subjectField = value;
}
}
/// <remarks/>
public modsRelatedItem relatedItem
{
get
{
return this.relatedItemField;
}
set
{
this.relatedItemField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
public partial class modsTitleInfo
{
private string titleField;
/// <remarks/>
public string title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
public partial class modsName
{
private string namePartField;
private string typeField;
/// <remarks/>
public string namePart
{
get
{
return this.namePartField;
}
set
{
this.namePartField = value;
}
}
/// <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.loc.gov/mods/v3")]
public partial class modsOriginInfo
{
private string dateIssuedField;
/// <remarks/>
public string dateIssued
{
get
{
return this.dateIssuedField;
}
set
{
this.dateIssuedField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
public partial class modsLanguage
{
private string languageTermField;
/// <remarks/>
public string languageTerm
{
get
{
return this.languageTermField;
}
set
{
this.languageTermField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
public partial class modsAbstract
{
private string langField;
private string typeField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/XML/1998/namespace")]
public string lang
{
get
{
return this.langField;
}
set
{
this.langField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
public partial class modsSubject
{
private string topicField;
private string geographicField;
/// <remarks/>
public string topic
{
get
{
return this.topicField;
}
set
{
this.topicField = value;
}
}
/// <remarks/>
public string geographic
{
get
{
return this.geographicField;
}
set
{
this.geographicField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
public partial class modsRelatedItem
{
private modsRelatedItemTitleInfo titleInfoField;
private modsRelatedItemPart partField;
private string typeField;
/// <remarks/>
public modsRelatedItemTitleInfo titleInfo
{
get
{
return this.titleInfoField;
}
set
{
this.titleInfoField = value;
}
}
/// <remarks/>
public modsRelatedItemPart part
{
get
{
return this.partField;
}
set
{
this.partField = value;
}
}
/// <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.loc.gov/mods/v3")]
public partial class modsRelatedItemTitleInfo
{
private string titleField;
/// <remarks/>
public string title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
public partial class modsRelatedItemPart
{
private modsRelatedItemPartDetail[] detailField;
private string dateField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("detail")]
public modsRelatedItemPartDetail[] detail
{
get
{
return this.detailField;
}
set
{
this.detailField = value;
}
}
/// <remarks/>
public string date
{
get
{
return this.dateField;
}
set
{
this.dateField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
public partial class modsRelatedItemPartDetail
{
private byte numberField;
private string captionField;
private string typeField;
/// <remarks/>
public byte number
{
get
{
return this.numberField;
}
set
{
this.numberField = value;
}
}
/// <remarks/>
public string caption
{
get
{
return this.captionField;
}
set
{
this.captionField = value;
}
}
/// <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.openarchives.org/OAI/2.0/")]
public partial class OAIPMHListRecordsResumptionToken
{
private System.DateTime expirationDateField;
private ushort completeListSizeField;
private byte cursorField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime expirationDate
{
get
{
return this.expirationDateField;
}
set
{
this.expirationDateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public ushort completeListSize
{
get
{
return this.completeListSizeField;
}
set
{
this.completeListSizeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte cursor
{
get
{
return this.cursorField;
}
set
{
this.cursorField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
}
how can I deserialize xml for more than once?
Could somebody help me generate C# class from XSD(sitemap with images)?
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://example.com/sample.html</loc>
<image:image>
<image:loc>http://example.com/image.jpg</image:loc>
</image:image>
<image:image>
<image:loc>http://example.com/photo.jpg</image:loc>
</image:image>
</url>
</urlset>
Here is my classes that were generated by xsd.exe tool:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true,
Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9",
IsNullable = false)]
public partial class urlset
{
private System.Xml.XmlElement[] anyField;
private List<tUrl> urlField;
/// <remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlElement[] Any
{
get { return this.anyField; }
set { this.anyField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("url")]
public List<tUrl> url
{
get { return this.urlField; }
set { this.urlField = 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://www.sitemaps.org/schemas/sitemap/0.9")]
public partial class tUrl
{
private string locField;
private string lastmodField;
private tChangeFreq changefreqField;
private bool changefreqFieldSpecified;
private decimal priorityField;
private bool priorityFieldSpecified;
private System.Xml.XmlElement[] anyField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "anyURI")]
public string loc
{
get { return this.locField; }
set { this.locField = value; }
}
/// <remarks/>
public string lastmod
{
get { return this.lastmodField; }
set { this.lastmodField = value; }
}
/// <remarks/>
public tChangeFreq changefreq
{
get { return this.changefreqField; }
set { this.changefreqField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool changefreqSpecified
{
get { return this.changefreqFieldSpecified; }
set { this.changefreqFieldSpecified = value; }
}
/// <remarks/>
public decimal priority
{
get { return this.priorityField; }
set { this.priorityField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool prioritySpecified
{
get { return this.priorityFieldSpecified; }
set { this.priorityFieldSpecified = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlElement[] Any
{
get { return this.anyField; }
set { this.anyField = value; }
}
[System.Xml.Serialization.XmlElementAttribute(ElementName = "image", Type = typeof(image))]
public List<image> Images { get; set; }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
public enum tChangeFreq
{
/// <remarks/>
always,
/// <remarks/>
hourly,
/// <remarks/>
daily,
/// <remarks/>
weekly,
/// <remarks/>
monthly,
/// <remarks/>
yearly,
/// <remarks/>
never,
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true,
Namespace = "http://www.google.com/schemas/sitemap-image/1.1")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.google.com/schemas/sitemap-image/1.1",
IsNullable = false)]
public partial class image
{
private string locField;
private string captionField;
private string geo_locationField;
private string titleField;
private string licenseField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(ElementName = "loc", DataType = "anyURI")]
public string loc
{
get { return this.locField; }
set { this.locField = value; }
}
/// <remarks/>
public string caption
{
get { return this.captionField; }
set { this.captionField = value; }
}
/// <remarks/>
public string geo_location
{
get { return this.geo_locationField; }
set { this.geo_locationField = value; }
}
/// <remarks/>
public string title
{
get { return this.titleField; }
set { this.titleField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "anyURI")]
public string license
{
get { return this.licenseField; }
set { this.licenseField = value; }
}
}
But I have problem with prefix 'image:', I could not sort out how to add this prefix to serialized xml. If I modify Element name by adding semicolon, then it do escape 'image_x003A_image'
I also used the xsd.exe tool and had the same issue. I resolved it as follows:
In the urlset class, add the following [XmlNamespaceDeclarations] property and create a constructor that adds the "image" xml namespace:
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
public urlset()
{
xmlns.Add("image", "http://www.google.com/schemas/sitemap-image/1.1");
}
In my case I only needed a single image, so I added the following attribute to my image property in tUrl
[XmlElement(Namespace = "http://www.google.com/schemas/sitemap-image/1.1")]
public image image
{
get
{
return this.imageField;
}
set
{
this.imageField = value;
}
}
This then added the "image:" prefix to all my image elements. You should be able to do something similar for you image list to associate it with the namespace you created above.
This post seems a little old, but hope this helps someone experiencing similar headaches trying to utilize the image sitemap schema extension.
I´m trying to deserialize the following XML:
#"<Activity type=""WOActivity"">
<ActionID>1</ActionID>
<ActionLog></ActionLog>
<ActionLogSummary>Add subcomponent</ActionLogSummary>
<UserID></UserID>
<FlexFields>
<FlexField mappedTo=""STATUS"" id=""0"">WAPPR</FlexField>
<FlexField mappedTo=""WOSEQUENCE"" id=""0"">10</FlexField>
<FlexField mappedTo=""OWNERGROUP"" id=""0"">V-PSB-DE-HLC-HWSUPPORT</FlexField>
</FlexFields>
I´ve also the following class, which was generated by the xsd:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2", IsNullable = false)]
public partial class Activity {
private string actionIDField;
private string actionLogField;
private string actionLogSummaryField;
private System.DateTime logDateTimeField;
private bool logDateTimeFieldSpecified;
private System.DateTime scheduledStartDateTimeField;
private bool scheduledStartDateTimeFieldSpecified;
private System.DateTime scheduledEndDateTimeField;
private bool scheduledEndDateTimeFieldSpecified;
private System.DateTime workBeginDateTimeField;
private bool workBeginDateTimeFieldSpecified;
private System.DateTime workEndDateTimeField;
private bool workEndDateTimeFieldSpecified;
private string userIDField;
private string userNameField;
private FlexFieldsFlexField[] flexFieldsField;
private string activityTypeField;
private string typeField;
private string indexField;
/// <remarks/>
public string ActionID {
get {
return this.actionIDField;
}
set {
this.actionIDField = value;
}
}
/// <remarks/>
public string ActionLog {
get {
return this.actionLogField;
}
set {
this.actionLogField = value;
}
}
/// <remarks/>
///
[System.Xml.Serialization.XmlElement]
public string ActionLogSummary {
get {
return this.actionLogSummaryField;
}
set {
this.actionLogSummaryField = value;
}
}
/// <remarks/>
public System.DateTime LogDateTime {
get {
return this.logDateTimeField;
}
set {
this.logDateTimeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool LogDateTimeSpecified {
get {
return this.logDateTimeFieldSpecified;
}
set {
this.logDateTimeFieldSpecified = value;
}
}
/// <remarks/>
public System.DateTime ScheduledStartDateTime {
get {
return this.scheduledStartDateTimeField;
}
set {
this.scheduledStartDateTimeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool ScheduledStartDateTimeSpecified {
get {
return this.scheduledStartDateTimeFieldSpecified;
}
set {
this.scheduledStartDateTimeFieldSpecified = value;
}
}
/// <remarks/>
public System.DateTime ScheduledEndDateTime {
get {
return this.scheduledEndDateTimeField;
}
set {
this.scheduledEndDateTimeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool ScheduledEndDateTimeSpecified {
get {
return this.scheduledEndDateTimeFieldSpecified;
}
set {
this.scheduledEndDateTimeFieldSpecified = value;
}
}
/// <remarks/>
public System.DateTime WorkBeginDateTime {
get {
return this.workBeginDateTimeField;
}
set {
this.workBeginDateTimeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool WorkBeginDateTimeSpecified {
get {
return this.workBeginDateTimeFieldSpecified;
}
set {
this.workBeginDateTimeFieldSpecified = value;
}
}
/// <remarks/>
public System.DateTime WorkEndDateTime {
get {
return this.workEndDateTimeField;
}
set {
this.workEndDateTimeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool WorkEndDateTimeSpecified {
get {
return this.workEndDateTimeFieldSpecified;
}
set {
this.workEndDateTimeFieldSpecified = value;
}
}
/// <remarks/>
public string UserID {
get {
return this.userIDField;
}
set {
this.userIDField = value;
}
}
/// <remarks/>
public string UserName {
get {
return this.userNameField;
}
set {
this.userNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("FlexField", IsNullable = false)]
public FlexFieldsFlexField[] FlexFields {
get {
return this.flexFieldsField;
}
set {
this.flexFieldsField = value;
}
}
/// <remarks/>
public string ActivityType {
get {
return this.activityTypeField;
}
set {
this.activityTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type {
get {
return this.typeField;
}
set {
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType = "integer")]
public string index {
get {
return this.indexField;
}
set {
this.indexField = value;
}
}
}
I´ve made a unit test, but it fails because all the elements are null (second assert and forth). The type attribute is deserialized just fine.
What could be possibly be wrong?
PS: I´ve tried to add the namespace "http://b2b.ibm.com/schema/B2B_CDM_Incident/R2_2" to the serializer with no success...
PS2: the serialization seems to be working just fine for this class
Thanks.
string result = #"<Activity type=""WOActivity"">
<ActionID>1</ActionID>
<ActionLog></ActionLog>
<ActionLogSummary>Add subcomponent</ActionLogSummary>
<UserID></UserID>
<FlexFields>
<FlexField mappedTo=""STATUS"" id=""0"">WAPPR</FlexField>
<FlexField mappedTo=""WOSEQUENCE"" id=""0"">10</FlexField>
<FlexField mappedTo=""OWNERGROUP"" id=""0"">V-PSB-DE-HLC-HWSUPPORT</FlexField>
</FlexFields>
</Activity>";
var serializer = new XmlSerializer(typeof (Activity), new XmlRootAttribute("Activity"));
var first = (Activity)serializer.Deserialize(new XmlTextReader(new StringReader(result)));
Assert.AreEqual("WOActivity", first.type);
Assert.AreEqual("Add subcomponent", first.ActionLogSummary);
Assert.IsNotNull(first.FlexFields);
Assert.AreEqual(4, first.FlexFields.Count());
Just put the root element into the correct namespace:
string xml = #"<Activity type=""WOActivity""
xmlns=""http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2"">
...
</Activity>";
Note that because the namespace is inherited, everything else also uses that element.
This then works:
var ser = new XmlSerializer(typeof(Activity));
var activity = (Activity)ser.Deserialize(new StringReader(xml));
System.Console.WriteLine(activity.ActionID);
System.Console.WriteLine(activity.ActionLogSummary);
System.Console.WriteLine(activity.type);
with output:
1
Add subcomponent
WOActivity
In other scenarios, you may need to refer to multiple namespaces, or mention the same namespace multiple times. Then it becomes useful to declare an alias; the following is semantically identical to the first example in this answer:
string xml = #"<b2b:Activity type=""WOActivity""
xmlns:b2b=""http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2"">
<b2b:ActionID>1</b2b:ActionID>
<b2b:ActionLog></b2b:ActionLog>
<b2b:ActionLogSummary>Add subcomponent</b2b:ActionLogSummary>
...
</b2b:Activity>";
With the only difference that we can now use b2b: as a prefix on any element as an alternative to saying xmlns=""http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2"". Note also that alias-based namespaces are not inherited, unlike xmlns= namespaces.
just delete the two generated lines:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2", IsNullable = false)]