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.
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'm writing generic method in Android app, and I have a problem with missing properties.
My class has 7 public properties but when I'm trying to get all properties I'm getting list with one property.
Here is my code:
T item = (T)Activator.CreateInstance(typeof(T));
var properties = item.GetType().GetProperties();
Problem occurs in all classes.
I have the same code (also the same classes) in similar application for iOS and everything works fine.
Here is sample class:
public partial class DB_USER {
private int iD_USERField;
private int iD_ACTORField;
private System.Nullable<long> iD_DISTRIBUTORField;
private string lOGINField;
private string pASSWORDField;
private bool iS_BLOCKEDField;
private string dESCRIPTIONField;
/// <remarks/>
public int ID_USER {
get {
return this.iD_OPERATORField;
}
set {
this.iD_OPERATORField = value;
}
}
/// <remarks/>
public int ID_ACTOR {
get {
return this.iD_ACTORField;
}
set {
this.iD_ACTORField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public System.Nullable<long> ID_DISTRIBUTOR {
get {
return this.iD_DISTRIBUTORField;
}
set {
this.iD_DISTRIBUTORField = value;
}
}
/// <remarks/>
public string LOGIN {
get {
return this.lOGINField;
}
set {
this.lOGINField = value;
}
}
/// <remarks/>
public string PASSWORD {
get {
return this.pASSWORDField;
}
set {
this.pASSWORDField = value;
}
}
/// <remarks/>
public bool IS_BLOCKED {
get {
return this.iS_BLOCKEDField;
}
set {
this.iS_BLOCKEDField = value;
}
}
/// <remarks/>
public string DESCRIPTION {
get {
return this.dESCRIPTIONField;
}
set {
this.dESCRIPTIONField = value;
}
}
}
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?
I have a fairly messy XML structure that I need to process and display to the end user. The XML structure holds information about Bars of metal and the Cuts to be made for each bar. The goal is to process this file, display to the user a list of the bars grouped by bar type. They can select a bar from this list where a gridview of some sort will display the cuts for that bar. They can then select a cut and the label information for that cut will be used to create a label with a barcode and some other information.
The structure of the XML can be seen below:
<BODY>
<BAR>
<BRAN>ALSPEC</BRAN>
<SYST>Hunter 100mm Flush Glazed</SYST>
<CODE>AS308</CODE>
<DESC>Door Adaptor</DESC>
<DICL>NOTRE DAME GLOSS</DICL>
<DOCL>notre dame gloss p/coat</DOCL>
<LEN> 6500</LEN>
<STS>1</STS>
<POS> 0</POS>
<SVL> 0</SVL>
<IVL> 0</IVL>
<VROT> 0</VROT>
<VU1S> 0</VU1S>
<VU1D> 0</VU1D>
<VU2S> 0</VU2S>
<VU2D> 0</VU2D>
<LENR> 201.3</LENR>
<H> 26.5</H>
<MLT> 1</MLT>
<SCP> 0</SCP>
<BRS> 0</BRS>
<IFS> 0</IFS>
<BS1L> 0</BS1L>
<BS1R> 0</BS1R>
<BS2L> 0</BS2L>
<BS2R> 0</BS2R>
<ENTH> 0</ENTH>
<CUT>
<NUM> 1</NUM>
<TYPE></TYPE>
<ANGL> 45</ANGL>
<ANGR> 90</ANGR>
<AB1> 90</AB1>
<AB2> 90</AB2>
<IL> 2064</IL>
<OL> 2090.5</OL>
<BCOD>000000231/5/44</BCOD>
<CSNA></CSNA>
<CSNU></CSNU>
<TINA></TINA>
<DESC>Jamb - Right</DESC>
<STAT> 1</STAT>
<LBL>Job# 760 Item# 5</LBL>
<LBL>2090.5 mm</LBL>
<LBL>W2-D8/T15</LBL>
<LBL>Jamb - Right</LBL>
</CUT>
<CUT>
<NUM> 1</NUM>
<TYPE></TYPE>
<ANGL> 45</ANGL>
<ANGR> 90</ANGR>
<AB1> 90</AB1>
<AB2> 90</AB2>
<IL> 2064</IL>
<OL> 2090.5</OL>
<BCOD>000000231/2/45</BCOD>
<CSNA></CSNA>
<CSNU></CSNU>
<TINA></TINA>
<DESC>Jamb - Right</DESC>
<STAT> 1</STAT>
<LBL>Job# 760 Item# 2</LBL>
<LBL>2090.5 mm</LBL>
<LBL>D8/T23</LBL>
<LBL>Jamb - Right</LBL>
</CUT>
<CUT>
<NUM> 1</NUM>
<TYPE></TYPE>
<ANGL> 90</ANGL>
<ANGR> 45</ANGR>
<AB1> 90</AB1>
<AB2> 90</AB2>
<IL> 2064</IL>
<OL> 2090.5</OL>
<BCOD>000000231/1/43</BCOD>
<CSNA></CSNA>
<CSNU></CSNU>
<TINA></TINA>
<DESC>Jamb - Left</DESC>
<STAT> 1</STAT>
<LBL>Job# 760 Item# 1</LBL>
<LBL>2090.5 mm</LBL>
<LBL>D8/T24</LBL>
<LBL>Jamb - Left</LBL>
</CUT>
</BAR>
</BODY>
The result I am looking for is something like below i.e a tree view on the left and a datagridview on the right displaying all the cuts in the job, filtered by the tree view selection (only cuts from the selected tree view level down).
GUI Layout
To achieve this I am trying to use LINQ to XML to parse the XML file into a class i have created:
public List<Bars> bars = new List<Bars>();
public class Bars
{
public int Bar_id { set; get; }
public string Brand { set; get; }
public string System { set; get; }
public string Code { set; get; }
public string Description { set; get; }
public string Length { set; get; }
public string Status { set; get; }
public string NumBars { set; get; }
public List<Cuts> Cuts { set; get; }
}
public class Cuts
{
public int Cut_id { set; get; }
public int Bar_id { set; get; }
public string AngleL { set; get; }
public string AngleR { set; get; }
public string LenInn { set; get; }
public string LenOut { set; get; }
public string Barcode { set; get; }
public string Description { set; get; }
public string Status { set; get; }
public string Label1 { set; get; }
public string Label2 { set; get; }
public string Label3 { set; get; }
public string Label4 { set; get; }
}
I'm actually looking for some advise on weather or not this is the correct way to handle this kind of project (LINQ to XML). Should I be using data sets or some other form or data storage.
I am new to XML and also to relational database type stuff so any advice will be greatly appreciated. Thanks in advance!
Will
Given a choice, Iwould thoroughly recommend using XDocument(LINQ to XML). It's much simpler to create documents and process them. Personally, my preferred choice in dealing large Xml.
var result = doc.Descendants("BAR")
.Select((e, i)=> new Bars()
{
Bar_id = i +1,
Brand = e.Element("BRAN").Value,
System = e.Element("SYST").Value,
Code = e.Element("CODE").Value,
Description = e.Element("DESC").Value,
Length =e.Element("LEN").Value,
Status =e.Element("STS").Value,
// remaining fileds
Cuts = e.Descendants("CUT").Select(c=> new Cuts()
{
Cut_id = int.Parse(c.Element("NUM").Value),
Bar_id = i+1,
AngleL = c.Element("ANGL").Value,
AngleR = c.Element("ANGR").Value,
LenInn = c.Element("IL").Value,
LenOut = c.Element("OL").Value,
Barcode = c.Element("BCOD").Value,
Description = c.Element("DESC").Value,
Status = c.Element("STAT").Value,
Label1 = c.Elements("LBL").First().Value,
Label2 = c.Elements("LBL").Skip(1).First().Value,
Label3 = c.Elements("LBL").Skip(2).First().Value,
Label4 = c.Elements("LBL").Skip(3).First().Value,
}).ToList(),
}).ToList();
Working Demo
Since you wanted some suggestion here is an easy way of doing it. When it comes to this kind of situations, serialisation is the easiest option. VS has inbuilt functionality to code generate classes for this kind of xml structures.
You can use Edit > Paste Special feature for this purpose. So your classes will look like;
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class BODY
{
private BODYBAR bARField;
/// <remarks/>
public BODYBAR BAR
{
get
{
return this.bARField;
}
set
{
this.bARField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class BODYBAR
{
private string bRANField;
private string sYSTField;
private string cODEField;
private string dESCField;
private string dICLField;
private string dOCLField;
private ushort lENField;
private byte sTSField;
private byte pOSField;
private byte sVLField;
private byte iVLField;
private byte vROTField;
private byte vU1SField;
private byte vU1DField;
private byte vU2SField;
private byte vU2DField;
private decimal lENRField;
private decimal hField;
private byte mLTField;
private byte sCPField;
private byte bRSField;
private byte iFSField;
private byte bS1LField;
private byte bS1RField;
private byte bS2LField;
private byte bS2RField;
private byte eNTHField;
private BODYBARCUT[] cUTField;
/// <remarks/>
public string BRAN
{
get
{
return this.bRANField;
}
set
{
this.bRANField = value;
}
}
/// <remarks/>
public string SYST
{
get
{
return this.sYSTField;
}
set
{
this.sYSTField = value;
}
}
/// <remarks/>
public string CODE
{
get
{
return this.cODEField;
}
set
{
this.cODEField = value;
}
}
/// <remarks/>
public string DESC
{
get
{
return this.dESCField;
}
set
{
this.dESCField = value;
}
}
/// <remarks/>
public string DICL
{
get
{
return this.dICLField;
}
set
{
this.dICLField = value;
}
}
/// <remarks/>
public string DOCL
{
get
{
return this.dOCLField;
}
set
{
this.dOCLField = value;
}
}
/// <remarks/>
public ushort LEN
{
get
{
return this.lENField;
}
set
{
this.lENField = value;
}
}
/// <remarks/>
public byte STS
{
get
{
return this.sTSField;
}
set
{
this.sTSField = value;
}
}
/// <remarks/>
public byte POS
{
get
{
return this.pOSField;
}
set
{
this.pOSField = value;
}
}
/// <remarks/>
public byte SVL
{
get
{
return this.sVLField;
}
set
{
this.sVLField = value;
}
}
/// <remarks/>
public byte IVL
{
get
{
return this.iVLField;
}
set
{
this.iVLField = value;
}
}
/// <remarks/>
public byte VROT
{
get
{
return this.vROTField;
}
set
{
this.vROTField = value;
}
}
/// <remarks/>
public byte VU1S
{
get
{
return this.vU1SField;
}
set
{
this.vU1SField = value;
}
}
/// <remarks/>
public byte VU1D
{
get
{
return this.vU1DField;
}
set
{
this.vU1DField = value;
}
}
/// <remarks/>
public byte VU2S
{
get
{
return this.vU2SField;
}
set
{
this.vU2SField = value;
}
}
/// <remarks/>
public byte VU2D
{
get
{
return this.vU2DField;
}
set
{
this.vU2DField = value;
}
}
/// <remarks/>
public decimal LENR
{
get
{
return this.lENRField;
}
set
{
this.lENRField = value;
}
}
/// <remarks/>
public decimal H
{
get
{
return this.hField;
}
set
{
this.hField = value;
}
}
/// <remarks/>
public byte MLT
{
get
{
return this.mLTField;
}
set
{
this.mLTField = value;
}
}
/// <remarks/>
public byte SCP
{
get
{
return this.sCPField;
}
set
{
this.sCPField = value;
}
}
/// <remarks/>
public byte BRS
{
get
{
return this.bRSField;
}
set
{
this.bRSField = value;
}
}
/// <remarks/>
public byte IFS
{
get
{
return this.iFSField;
}
set
{
this.iFSField = value;
}
}
/// <remarks/>
public byte BS1L
{
get
{
return this.bS1LField;
}
set
{
this.bS1LField = value;
}
}
/// <remarks/>
public byte BS1R
{
get
{
return this.bS1RField;
}
set
{
this.bS1RField = value;
}
}
/// <remarks/>
public byte BS2L
{
get
{
return this.bS2LField;
}
set
{
this.bS2LField = value;
}
}
/// <remarks/>
public byte BS2R
{
get
{
return this.bS2RField;
}
set
{
this.bS2RField = value;
}
}
/// <remarks/>
public byte ENTH
{
get
{
return this.eNTHField;
}
set
{
this.eNTHField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("CUT")]
public BODYBARCUT[] CUT
{
get
{
return this.cUTField;
}
set
{
this.cUTField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class BODYBARCUT
{
private byte nUMField;
private object tYPEField;
private byte aNGLField;
private byte aNGRField;
private byte aB1Field;
private byte aB2Field;
private ushort ilField;
private decimal olField;
private string bCODField;
private object cSNAField;
private object cSNUField;
private object tINAField;
private string dESCField;
private byte sTATField;
private string[] lBLField;
/// <remarks/>
public byte NUM
{
get
{
return this.nUMField;
}
set
{
this.nUMField = value;
}
}
/// <remarks/>
public object TYPE
{
get
{
return this.tYPEField;
}
set
{
this.tYPEField = value;
}
}
/// <remarks/>
public byte ANGL
{
get
{
return this.aNGLField;
}
set
{
this.aNGLField = value;
}
}
/// <remarks/>
public byte ANGR
{
get
{
return this.aNGRField;
}
set
{
this.aNGRField = value;
}
}
/// <remarks/>
public byte AB1
{
get
{
return this.aB1Field;
}
set
{
this.aB1Field = value;
}
}
/// <remarks/>
public byte AB2
{
get
{
return this.aB2Field;
}
set
{
this.aB2Field = value;
}
}
/// <remarks/>
public ushort IL
{
get
{
return this.ilField;
}
set
{
this.ilField = value;
}
}
/// <remarks/>
public decimal OL
{
get
{
return this.olField;
}
set
{
this.olField = value;
}
}
/// <remarks/>
public string BCOD
{
get
{
return this.bCODField;
}
set
{
this.bCODField = value;
}
}
/// <remarks/>
public object CSNA
{
get
{
return this.cSNAField;
}
set
{
this.cSNAField = value;
}
}
/// <remarks/>
public object CSNU
{
get
{
return this.cSNUField;
}
set
{
this.cSNUField = value;
}
}
/// <remarks/>
public object TINA
{
get
{
return this.tINAField;
}
set
{
this.tINAField = value;
}
}
/// <remarks/>
public string DESC
{
get
{
return this.dESCField;
}
set
{
this.dESCField = value;
}
}
/// <remarks/>
public byte STAT
{
get
{
return this.sTATField;
}
set
{
this.sTATField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("LBL")]
public string[] LBL
{
get
{
return this.lBLField;
}
set
{
this.lBLField = value;
}
}
}
Then you can simply deserialise;
BODY objBody = new BODY();
var xml = File.ReadAllText(#"C:\XMLFile2.xml");
using (var reader = new StringReader(xml))
{
var serialiser = new XmlSerializer(typeof(BODY));
objBody = (BODY)serialiser.Deserialize(reader);
}
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)]