ASMX Service XML request deserializing partially - c#

I know this old tech, if it was up to me life would be different. I am relatively new to this old tech, I am extending an existing service at work and I am experiencing an odd issue. I have 3 service endpoints and 2 of them are working as expected but my issue with the third one is that some how the "framework" fails to deserialize the xml request.
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<UpdateSale xmlns="http://tokenws.netgen.co.za/">
<p_objrequest>
<transactionTypeId>1</transactionTypeId>
<tenderTypeId>1</tenderTypeId>
<standardHeader>
<requestId xmlns="">1_8</requestId>
<localeId xmlns="" />
<systemId xmlns="">asdf</systemId>
<batchReference xmlns="">11</batchReference>
</standardHeader>
<account>
<accountId xmlns="">123</accountId>
<pin xmlns="" >123</pin>
</account>
<amount>
<valueCode xmlns="">ZAR</valueCode>
<enteredAmount xmlns="">30</enteredAmount>
<nsfAllowed xmlns="">N</nsfAllowed>
</amount>
<lineItems>
<LineItem>
<productCode>1</productCode>
<categoryCode>1</categoryCode>
<qty>1</qty>
<price>50</price>
<discountedPrice>0</discountedPrice>
<description>Buffet Breakfast</description>
</LineItem>
</lineItems>
</p_objrequest>
<netCredentials>
<UserName xmlns="http://tempuri.org/">123</UserName>
<Password xmlns="http://tempuri.org/">123</Password>
</netCredentials>
</UpdateSale>
</soap:Body>
</soap:Envelope>
Above is the xml, the netCredentials get deserialized correctly but p_objrequest comes as null.
How can I resolve this?
Below is the class:
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tokenws.netgen.co.za/")]
public class Sale
{
private int transactionTypeIdField;
private int tenderTypeIdField;
private RequestStandardHeaderComponent standardHeaderField;
private AccountComponent accountField;
private string activatingField;
private AmountComponent amountField;
private CustomerInfoComponent customerInfoField;
private PromotionCode[] promotionCodesField;
private QuestionAndAnswer[] questionsAndAnswersField;
private LineItem[] lineItemsField;
private string includeTipField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public int transactionTypeId
{
get
{
return this.transactionTypeIdField;
}
set
{
this.transactionTypeIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public int tenderTypeId
{
get
{
return this.tenderTypeIdField;
}
set
{
this.tenderTypeIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 2)]
public RequestStandardHeaderComponent standardHeader
{
get
{
return this.standardHeaderField;
}
set
{
this.standardHeaderField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 3)]
public AccountComponent account
{
get
{
return this.accountField;
}
set
{
this.accountField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 4)]
public string activating
{
get
{
return this.activatingField;
}
set
{
this.activatingField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 5)]
public AmountComponent amount
{
get
{
return this.amountField;
}
set
{
this.amountField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 6)]
public CustomerInfoComponent customerInfo
{
get
{
return this.customerInfoField;
}
set
{
this.customerInfoField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Order = 7)]
public PromotionCode[] promotionCodes
{
get
{
return this.promotionCodesField;
}
set
{
this.promotionCodesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Order = 8)]
public QuestionAndAnswer[] questionsAndAnswers
{
get
{
return this.questionsAndAnswersField;
}
set
{
this.questionsAndAnswersField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Order = 9)]
public LineItem[] lineItems
{
get
{
return this.lineItemsField;
}
set
{
this.lineItemsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 10)]
public string includeTip
{
get
{
return this.includeTipField;
}
set
{
this.includeTipField = value;
}
}
}

The tag name and the class/property names must match including case. See below :
[XmlRoot(ElementName= "UpdateSale", Namespace="http://tokenws.netgen.co.za/")]
public class Sale
{
}
[XmlRoot(ElementName= "netCredentials")]
public class NetCredentials
{
[XmlElement(ElementName="UserName",Namespace="http://tempuri.org/")]
public Name userName { get; set;}
[XmlElement(ElementName="Password",Namespace="http://tempuri.org/")]
public Name password { get; set;}
}
public class Name
{
[XmlText]
public string name { get; set;}
}

Related

Deserialize a XML to C# class from a wsdl

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;
}

List<Model> comes back as Null

new to ASP.net and have a question involving a model that has multiple variables of the same type.
I have Order model that can have 0 to n Detail models attached to it. When I POST the http request I get all the information for the Order except the Detail model always comes back as null. Or more specifically the ".count" method on the List is 0.
How can I get it to post all the Details for my Model?
Order Model:
public struct shipTo
{
public string id;
public string Line1;
public int PostalCode;
public string City;
public string State;
public string CountryCode;
}
namespace USS_EDIv2.Models
{
public class Order
{
public Int64 SalesOrderNumber { get; set; }
public Int64 PurchaseOrderNumber { get; set; }
public Int64 BranchPlant { get; set; }
public shipTo ShipTo;
public Int64 Quantity { get; set; }
public string UOM { get; set; }
public List<Detail> Detail = new List<Detail>();
}
}
Detail Model:
namespace USS_EDIv2.Models
{
public class Detail
{
public string LineNumber;
public string GradeItem;
public string Quantity;
public string UOM;
public string RequestDate;
public string Status;
public Detail()
{
LineNumber = "1";
GradeItem = "1";
Quantity = "1";
UOM = "1";
RequestDate = "1";
Status = "1";
}
}
}
Repository class:
namespace USS_EDIv2.Services
{
public class SalesRepository
{
public DateTime currentTime = System.DateTime.Now;
private const string CacheKey = "4041tmtTEST1337";
public string xml;
public int i;
public Order[] GetAllSales()
{
var ctx = HttpContext.Current;
if (ctx != null)
{
return (Order[])ctx.Cache[CacheKey];
}
return new Order[]
{
new Order
{
}
};
}
public SalesRepository()
{
var ctx = HttpContext.Current;
if (ctx != null)
{
if (ctx.Cache[CacheKey] == null)
{
var sales = new Order[]
{
};
{
};
ctx.Cache[CacheKey] = sales;
//ctx.Cache.Remove("4041tmtTEST1337");
}
}
}
public bool SaveSale(Order sales)
{
var ctx = HttpContext.Current;
if (ctx != null)
{
try
{
var currentData = ((Order[])ctx.Cache[CacheKey]).ToList();
currentData.Add(sales);
ctx.Cache[CacheKey] = currentData.ToArray();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
return false;
}
}
}
And my Controller:
namespace USS_EDIv2.Controllers
{
public class NewOrderController : ApiController
{
public DateTime currentTime = System.DateTime.Now;
private SalesRepository salesRepository;
public int i;
public NewOrderController()
{
this.salesRepository = new SalesRepository();
}
public Order[] Get()
{
return salesRepository.GetAllSales();
}
public HttpResponseMessage Post(Order sale)
{
var ctx = HttpContext.Current;
this.salesRepository.SaveSale(sale);
var response = Request.CreateResponse<Order> (System.Net.HttpStatusCode.Created, sale);
return response;
}
}
}
This is the XML data being requested:
<Order>
<SalesOrderNumber>1294288</SalesOrderNumber>
<PurchaseOrderNumber>81896</PurchaseOrderNumber>
<BranchPlant>9701</BranchPlant>
<ShipTo id="string">
<Line1>RAIL TRACK #769 SPOT 00</Line1>
<PostalCode>79765</PostalCode>
<City>MIDLAND COUNTY</City>
<State>TX</State>
<CountryCode>US</CountryCode>
</ShipTo>
<Quantity>75</Quantity>
<UOM>TN</UOM>
<Detail created="2015-12-14T13:59:57.84" action="Create">
<LineNumber>1.0</LineNumber>
<GradeItem>97010B00000</GradeItem>
<Quantity>25.000</Quantity>
<UOM>TN</UOM>
<RequestDate>2015-07-11</RequestDate>
<Status>Open</Status>
</Detail>
<Detail created="2015-12-14T13:59:57.84" action="Create">
<LineNumber>2.0</LineNumber>
<GradeItem>97010B00000</GradeItem>
<Quantity>25</Quantity>
<UOM>TN</UOM>
<RequestDate>2002-02-07</RequestDate>
<Status>Open</Status>
</Detail>
<Detail created="2015-05-22T02:29:50.78" action="Create">
<LineNumber>3.0</LineNumber>
<GradeItem>97010B00000</GradeItem>
<Quantity>25</Quantity>
<UOM>TN</UOM>
<RequestDate>2015-07-11</RequestDate>
<Status>Open</Status>
</Detail>
</Order>
And this is my current response:
<ArrayOfOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Order>
<ShipTo>
<Line1>RAIL TRACK #769 SPOT 00</Line1>
<PostalCode>79765</PostalCode>
<City>MIDLAND COUNTY</City>
<State>TX</State>
<CountryCode>US</CountryCode>
</ShipTo>
************HERE LIES THE PROBLEM
<Detail/>
************
<SalesOrderNumber>1294288</SalesOrderNumber>
<PurchaseOrderNumber>81896</PurchaseOrderNumber>
<BranchPlant>9701</BranchPlant>
<Quantity>75</Quantity>
<UOM>TN</UOM>
</Order>
</ArrayOfOrder>
I also have no control over changing the format of the incoming xml data. Any pointer in the right direction would be helpful please.
Debug:
[debug] (http://imgur.com/RVnpZ0S)
Okay,
So I solved this by copying the XML data onto my clipboard, creating a new class and then going to Edit -> Paste Special -> Paste XML as classes
and it came up with this:
namespace USS_EDIv2.Models
{
public class Orders
{
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Order
{
private uint salesOrderNumberField;
private uint purchaseOrderNumberField;
private ushort branchPlantField;
private OrderShipTo shipToField;
private byte quantityField;
private string uOMField;
private OrderDetail[] detailField;
/// <remarks/>
public uint SalesOrderNumber
{
get
{
return this.salesOrderNumberField;
}
set
{
this.salesOrderNumberField = value;
}
}
/// <remarks/>
public uint PurchaseOrderNumber
{
get
{
return this.purchaseOrderNumberField;
}
set
{
this.purchaseOrderNumberField = value;
}
}
/// <remarks/>
public ushort BranchPlant
{
get
{
return this.branchPlantField;
}
set
{
this.branchPlantField = value;
}
}
/// <remarks/>
public OrderShipTo ShipTo
{
get
{
return this.shipToField;
}
set
{
this.shipToField = value;
}
}
/// <remarks/>
public byte Quantity
{
get
{
return this.quantityField;
}
set
{
this.quantityField = value;
}
}
/// <remarks/>
public string UOM
{
get
{
return this.uOMField;
}
set
{
this.uOMField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Detail")]
public OrderDetail[] Detail
{
get
{
return this.detailField;
}
set
{
this.detailField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderShipTo
{
private string line1Field;
private uint postalCodeField;
private string cityField;
private string stateField;
private string countryCodeField;
private string idField;
/// <remarks/>
public string Line1
{
get
{
return this.line1Field;
}
set
{
this.line1Field = value;
}
}
/// <remarks/>
public uint PostalCode
{
get
{
return this.postalCodeField;
}
set
{
this.postalCodeField = value;
}
}
/// <remarks/>
public string City
{
get
{
return this.cityField;
}
set
{
this.cityField = value;
}
}
/// <remarks/>
public string State
{
get
{
return this.stateField;
}
set
{
this.stateField = value;
}
}
/// <remarks/>
public string CountryCode
{
get
{
return this.countryCodeField;
}
set
{
this.countryCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetail
{
private decimal lineNumberField;
private string gradeItemField;
private decimal quantityField;
private string uOMField;
private System.DateTime requestDateField;
private string statusField;
private System.DateTime createdField;
private string actionField;
/// <remarks/>
public decimal LineNumber
{
get
{
return this.lineNumberField;
}
set
{
this.lineNumberField = value;
}
}
/// <remarks/>
public string GradeItem
{
get
{
return this.gradeItemField;
}
set
{
this.gradeItemField = value;
}
}
/// <remarks/>
public decimal Quantity
{
get
{
return this.quantityField;
}
set
{
this.quantityField = value;
}
}
/// <remarks/>
public string UOM
{
get
{
return this.uOMField;
}
set
{
this.uOMField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime RequestDate
{
get
{
return this.requestDateField;
}
set
{
this.requestDateField = value;
}
}
/// <remarks/>
public string Status
{
get
{
return this.statusField;
}
set
{
this.statusField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime created
{
get
{
return this.createdField;
}
set
{
this.createdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string action
{
get
{
return this.actionField;
}
set
{
this.actionField = value;
}
}
}
Just in case anyone ever stumbles across this problem. What was giving me hours of headache took 3 clicks to solve... Thanks all for your replies.

Generate C# class from XSD sitemap with images

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.

C# deserialization null elements

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)]

How to Deserializes this XML document to generated C# class

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);
}

Categories