How to map C# object to SQLite database table? - c#

I have the following SQLite table
And I have the following C# classes:
[XmlRoot(ElementName = "articlessql", Namespace = "http://tempuri.org/DataSet1.xsd")]
public class Article
{
[XmlElement(ElementName = "id", Namespace = "http://tempuri.org/DataSet1.xsd")]
public string Id { get; set; }
[XmlElement(ElementName = "name", Namespace = "http://tempuri.org/DataSet1.xsd")]
public string Name { get; set; }
[XmlElement(ElementName = "quantity", Namespace = "http://tempuri.org/DataSet1.xsd")]
public string Quantity { get; set; }
[XmlElement(ElementName = "sell_price", Namespace = "http://tempuri.org/DataSet1.xsd")]
public string SellPrice { get; set; }
[XmlElement(ElementName = "sellid", Namespace = "http://tempuri.org/DataSet1.xsd")]
public string SellId { get; set; }
[XmlElement(ElementName = "whole_price", Namespace = "http://tempuri.org/DataSet1.xsd")]
public string WholePrice { get; set; }
[XmlElement(ElementName = "group", Namespace = "http://tempuri.org/DataSet1.xsd")]
public string Group { get; set; }
[XmlElement(ElementName = "barcode", Namespace = "http://tempuri.org/DataSet1.xsd")]
public string Barcode { get; set; }
[XmlElement(ElementName = "measure", Namespace = "http://tempuri.org/DataSet1.xsd")]
public string Measure { get; set; }
}
[XmlRoot(ElementName = "DataSet1", Namespace = "http://tempuri.org/DataSet1.xsd")]
public class Articles
{
[XmlElement(ElementName = "articlessql", Namespace = "http://tempuri.org/DataSet1.xsd")]
public List<Article> AllArticles { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
}
I get list of articles like that:
XmlSerializer deserializer = new XmlSerializer(typeof(Articles));
TextReader textReader = new StreamReader(xmlFilePath);
Articles articles = new Articles();
articles = (Articles)deserializer.Deserialize(textReader);
textReader.Close();
But when I try to insert the list of articles in the table articles like that:
using (var connection = new SQLiteConnection(System.IO.Path.Combine(apkFolder, "MobileSell.db")))
{
connection.InsertAll(articles.AllArticles);
}
It throws exception:
SQLite.SQLiteException: no such table: Article at SQLite.SQLite3.Prepare2
My question is how can I make the program understand that Article object must be a single row in table articles

did you read SQLite specification?
SQLite has attributes like [Column] and [Table] attributes. You should use those. Like this:
[Table("ContactInfo")] // SQLite attribute
[DataContract(Name = "ContactInfo", Namespace = "")]
public sealed class ContactInfo : DatabaseTableBase
{
/// <summary>
/// Unique Id of the codebook.
/// </summary>
[PrimaryKey] // SQLite attribute
[AutoIncrement] // SQLite attribute
[Column("UniqueId")] // SQLite attribute
[IgnoreDataMember]
public override long UniqueId { get; set; }
[Column("ClaimId")] // SQLite attribute
[NotNull] // SQLite attribute
public long ClaimId { get; set; }
[Column("ContactType")] // SQLite attribute
[NotNull] // SQLite attribute
public ContactTypes ContactType { get; set; }
public ContactInfo()
{
}
}
In your case, you will be annotating with attributes your class Article
Hope it helps.

Related

Batch Data Extraction Script

apologies for the newbie question as I am very new to the world of programming etc.
I have a large XML file containing (see link below). It contains an ID number for legal entities (LEI) followed by the ID for their respective parent company.
Example from the XML file
The yellow is the entity LEI number and the green is the parent company LEI.
I would like to create some sort of batch script or GUI so I can enter a list of entity (green) LEI numbers and then given an output of all the corresponding parent LEI numbers.
Here is the file for anyone wondering: https://leidata.gleif.org/api/v1/concatenated-files/rr/20171025/zip
I am very inexperienced so I am not sure where to start.
Many thanks
Copy the entire file content to clipboard, then open your visual studio project and go to menu -> edit -> paste special -> paste as xml classes. This will generate a bunch of classes in the file you have currently open. Save this file. Then you use this code to load the file and return the loaded data:
public static RelationshipData LoadFile(string fileName)
{
var serializer = new XmlSerializer(typeof(Items));
RelationshipData data;
using (Stream r = new FileStream(filename, FileMode.Open))
{
data = (RelationshipData)serializer.Deserialize(r);
}
return data;
}
Now you should be able to access the file content using the properties of the returned data object. Haven't used this method with xml-data that uses prefixes, but I think this will be handled too...
With this data you should build up a dictionary or something like that that suits your needs that maps one ID to the other ID.
You should really start reading why and how this works...
NOT TESTED
I think I got most of the elements. You need to do some careful checking since there are a lot of nodes. I added a writer so I can use Beyond Compare to check if I got all the nodes. Fixed some errors. Still need some minor tweaks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string INPUT_FILENAME = #"c:\temp\test.xml";
const string OUTPUT_FILENAME = #"c:\temp\test1.xml";
static void Main(string[] args)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.CheckCharacters = false;
XmlReader reader = XmlReader.Create(INPUT_FILENAME, settings);
XmlSerializer serializer = new XmlSerializer(typeof(RelationshipDataRelationshipData));
RelationshipDataRelationshipData relationshipDataRelationshipData = (RelationshipDataRelationshipData)serializer.Deserialize(reader);
XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.Indent = true;
XmlWriter writer = XmlWriter.Create(OUTPUT_FILENAME, wSettings);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("rr", "http://www.gleif.org/data/schema/rr/2016");
ns.Add("gleif", "http://www.gleif.org/concatenated-file/header-extension/2.0");
serializer.Serialize(writer, relationshipDataRelationshipData, ns);
writer.Flush();
writer.Close();
}
}
[XmlRoot(ElementName = "RelationshipData", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipDataRelationshipData
{
[XmlElement("Header")]
public Header header { get; set; }
[XmlElement("RelationshipRecords", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public RelationshipRecords relationshipRecords { get; set; }
}
[XmlRoot(ElementName = "Header", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class Header
{
[XmlElement("ContentDate")]
public DateTime ContentDate { get; set; }
[XmlElement("FileContent")]
public string FileContent { get; set; }
[XmlElement("RecordCount")]
public int RecordCount { get; set; }
[XmlElement("Extension", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public Extension extension { get; set; }
}
[XmlRoot(ElementName = "Extension", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class Extension
{
[XmlElement(ElementName = "Sources", Namespace = "http://www.gleif.org/concatenated-file/header-extension/2.0")]
public Sourses sources { get; set; }
}
[XmlRoot(ElementName = "Sources", Namespace = "http://www.gleif.org/concatenated-file/header-extension/2.0")]
public class Sourses
{
[XmlElement("Source")]
public List<Source> source { get; set; }
}
[XmlRoot(ElementName = "Source", Namespace = "http://www.gleif.org/concatenated-file/header-extension/2.0")]
public class Source
{
[XmlElement("ContentDate")]
public DateTime ContentDate { get; set; }
[XmlElement("Originator")]
public string Originator { get; set; }
[XmlElement("RecordCount")]
public int RecordCount { get; set; }
}
[XmlRoot(ElementName = "RelationshipRecords", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipRecords
{
[XmlElement("RelationshipRecord")]
public List<RelationshipRecord> relationshipRecord { get; set; }
}
[XmlRoot(ElementName = "RelationshipRecord", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipRecord
{
[XmlElement("Relationship")]
public Relationship relationship { get; set; }
[XmlElement("Registration")]
public Registration registration { get; set; }
[XmlElement("Extension", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public Extension extension { get; set; }
}
[XmlRoot(ElementName = "Relationship", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class Relationship
{
[XmlElement("StartNode")]
public Node StartNode { get; set; }
[XmlElement("EndNode")]
public Node EndNode { get; set; }
public string RelationshipType { get; set; }
[XmlElement("RelationshipPeriods")]
public RelationshipPeriods relationshipPeriods { get; set; }
public string RelationshipStatus { get; set; }
[XmlElement("RelationshipQualifiers")]
public RelationshipQualifiers relationshipQualifiers { get; set; }
}
[XmlRoot(ElementName = "Node", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class Node
{
public string NodeID { get; set; }
public string NodeIDType { get; set; }
}
[XmlRoot(ElementName = "class RelationshipQualifiers", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipQualifiers
{
[XmlElement("RelationshipQualifier")]
public RelationshipQualifier relationshipQualifier { get; set; }
}
[XmlRoot(ElementName = "class RelationshipQualifier", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipQualifier
{
public string QualifierDimension { get; set; }
public string QualifierCategory { get; set; }
}
[XmlRoot(ElementName = "Registration", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class Registration
{
public DateTime InitialRegistrationDate { get; set; }
public DateTime LastUpdateDate { get; set; }
public string RegistrationStatus { get; set; }
public DateTime NextRenewalDate { get; set; }
public string ManagingLOU { get; set; }
public string ValidationSources { get; set; }
public string ValidationDocuments { get; set; }
}
[XmlRoot(ElementName = "class RelationshipPeriods", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipPeriods
{
[XmlElement("RelationshipPeriod")]
public List<RelationshipPeriod> relationshipPeriod { get; set; }
}
[XmlRoot(ElementName = "class RelationshipPeriod", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipPeriod
{
[XmlElement("StartDate")]
public DateTime StartDate { get; set; }
[XmlElement("EndDate")]
public DateTime EndDate { get; set; }
[XmlElement("PeriodType")]
public string PeriodType { get; set; }
}
[XmlRoot(ElementName = "class RelationshipQuantifiers", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipQuantifiers
{
[XmlElement("RelationshipQuantifier")]
public List<RelationshipQuantifier> relationshipQuantifier { get; set; }
}
[XmlRoot(ElementName = "class RelationshipQuantifier", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipQuantifier
{
[XmlElement("MeasurementMethod")]
public DateTime MeasurementMethod { get; set; }
[XmlElement("QuantifierAmount")]
public decimal QuantifierAmount { get; set; }
[XmlElement("QuantifierUnits")]
public string QuantifierUnits { get; set; }
}
}

XML Serialization issue - duplicate XmlRoot node

I am working with a third party XML API, that returns XML responses. With the API, I am trying to import orders. So, two scenario could occur when I import order with this API:
Success - e.g. xml response: http://pastebin.com/ZcvvYMX6
Failure - e.g. xml response: http://pastebin.com/iVjqMKAR
What I am working on is a wrapper for this API and I have a method called ImportOrders. Within this method, I am trying to deserialize the xml to an object so that I can return a common response.
For example; I will have a common response DTO class like this:
public class ImportOrderResponse
{
bool IsError { get; set; }
string ErrorMsg { get; set; }
string Result { get; set; }
}
and the ImportOrders method will return response conditionally like this:
if (apiResult.Contains("importFailures"))
{
// todo: deserialize
return new ImportOrderResponse()
{
IsError = true,
ErrorMsg = "todo - get failureMessage"
};
}
else
{
// todo: deserialize
return new ImportOrderResponse()
{
IsError = false,
Result = "todo - haven't worked out what i need to return"
};
}
Using this site http://xmltocsharp.azurewebsites.net I've generated the classes used for deserialization like this:
ImportSuccess.cs
[XmlRoot(ElementName = "import")]
public class ImportSuccess
{
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "operation")]
public string Operation { get; set; }
[XmlAttribute(AttributeName = "entity")]
public string Entity { get; set; }
[XmlAttribute(AttributeName = "externalReference")]
public string ExternalReference { get; set; }
[XmlAttribute(AttributeName = "item")]
public string Item { get; set; }
[XmlAttribute(AttributeName = "queryTime")]
public string QueryTime { get; set; }
}
[XmlRoot(ElementName = "importSuccesses")]
public class ImportSuccesses
{
[XmlElement(ElementName = "import")]
public List<ImportSuccess> Import { get; set; }
}
[XmlRoot(ElementName = "importResult")]
public class ImportResult
{
[XmlElement(ElementName = "importSuccesses")]
public ImportSuccesses ImportSuccesses { get; set; }
[XmlElement(ElementName = "importFailures")]
public string ImportFailures { get; set; }
[XmlElement(ElementName = "importDuplicates")]
public string ImportDuplicates { get; set; }
}
ImportFailure.cs
[XmlRoot(ElementName = "import")]
public class ImportFailure
{
[XmlElement(ElementName = "failureMessage")]
public string FailureMessage { get; set; }
[XmlElement(ElementName = "failureDetail")]
public string FailureDetail { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "operation")]
public string Operation { get; set; }
[XmlAttribute(AttributeName = "externalReference")]
public string ExternalReference { get; set; }
[XmlAttribute(AttributeName = "queryTime")]
public string QueryTime { get; set; }
}
[XmlRoot(ElementName = "importFailures")]
public class ImportFailures
{
[XmlElement(ElementName = "import")]
public ImportFailure Import { get; set; }
}
[XmlRoot(ElementName = "importResult")]
public class ImportResult
{
[XmlElement(ElementName = "importSuccesses")]
public string ImportSuccesses { get; set; }
[XmlElement(ElementName = "importFailures")]
public ImportFailures ImportFailures { get; set; }
[XmlElement(ElementName = "importDuplicates")]
public string ImportDuplicates { get; set; }
}
But now I have a problem. Both the success & failure response contains the node importResult but has varying fields/element names.
Now I have an error because I have a duplicate ImportResult class.
How can I solve this? What is the correct way to handle this scenario?
I believe I was being silly... I was going about this with a wrong approach.
What I did was;
Call api with correct data for a success response, took the xml and created a class
call api with wrong/bad data for a failure response, took the xml and tried to create another class
After some digging around, I realised that the response structure is very close for success/failure.
So, I called the api and instead of sending one order; I sent 3 orders for 3 specific scenario. This resulted in a single XML response like this:
http://pastebin.com/sNgWAwq1
Now I have a good structure of the XML for (Success/Failure & Duplicates). Using this XML, I've generated the following class (for deserialisation purpose):
[XmlRoot(ElementName = "import")]
public class Import
{
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "operation")]
public string Operation { get; set; }
[XmlAttribute(AttributeName = "entity")]
public string Entity { get; set; }
[XmlAttribute(AttributeName = "externalReference")]
public string ExternalReference { get; set; }
[XmlAttribute(AttributeName = "item")]
public string Item { get; set; }
[XmlAttribute(AttributeName = "queryTime")]
public string QueryTime { get; set; }
[XmlElement(ElementName = "failureMessage")]
public string FailureMessage { get; set; }
[XmlElement(ElementName = "failureDetail")]
public string FailureDetail { get; set; }
[XmlElement(ElementName = "duplicateMessage")]
public string DuplicateMessage { get; set; }
[XmlElement(ElementName = "duplicateDetail")]
public string DuplicateDetail { get; set; }
}
[XmlRoot(ElementName = "importSuccesses")]
public class ImportSuccesses
{
[XmlElement(ElementName = "import")]
public List<Import> Import { get; set; }
}
[XmlRoot(ElementName = "importFailures")]
public class ImportFailures
{
[XmlElement(ElementName = "import")]
public Import Import { get; set; }
}
[XmlRoot(ElementName = "importDuplicates")]
public class ImportDuplicates
{
[XmlElement(ElementName = "import")]
public Import Import { get; set; }
}
[XmlRoot(ElementName = "importResult")]
public class OrderImportResponse
{
[XmlElement(ElementName = "importSuccesses")]
public ImportSuccesses ImportSuccesses { get; set; }
[XmlElement(ElementName = "importFailures")]
public ImportFailures ImportFailures { get; set; }
[XmlElement(ElementName = "importDuplicates")]
public ImportDuplicates ImportDuplicates { get; set; }
}
Now, this can work for me. I can call the api and deserialise the response to the OrderImportResponse class (without checking what kind of xml response it was) and it will have all three scenario responses I am looking for.

Deserialize xml does not pick up element

I am trying to deserialize some xml. Here it is:
<FooBars xmlns="http://foos">
<Id1 xmlns="http://bars">2</Id1>
<Id2 xmlns="http://bars">7</Id2>
<Info xmlns="http://bars">
<Data>
<Field1>text1</Field1>
<Field2>text2</Field2>
<Field3>text3</Field3>
</Data>
<Data>
<Field1>text5</Field1>
<Field2>text6</Field2>
<Field3>text7</Field3>
</Data>
</Info>
</FooBars>
I tried this:
var myData = (FooBars)serializer.Deserialize(foobars.CreateReader());
...
[XmlRoot(ElementName = "FooBars", Namespace = "http://foos")]
public class FooBars
{
[XmlElement(ElementName = "Id1", Namespace = "http://bars")]
public string Id1 { get; set; }
[XmlElement(ElementName = "Id2", Namespace = "http://bars")]
public string Id2 { get; set; }
[XmlElement(ElementName = "Info", Namespace = "http://bars")]
public List<Data> Info { get; set; }
}
public class Data
{
[XmlElement(ElementName = "Field1")]
public string Field1 { get; set; }
[XmlElement(ElementName = "Field2")]
public string Field2 { get; set; }
}
But it looks like the Data class is not considered part of the xml, since it is not able to read it. I am getting all the other elements (ids) but not the things defined in Data.
Where am I wrong?
Presuming missing </Info> tag is a typo, All you need is XmlArray and XmlArrayItem
[XmlRoot(ElementName = "FooBars", Namespace = "http://foos")]
public class FooBars
{
[XmlElement(ElementName = "Id1", Namespace = "http://bars")]
public string Id1 { get; set; }
[XmlElement(ElementName = "Id2", Namespace = "http://bars")]
public string Id2 { get; set; }
[XmlArray(ElementName = "Info", Namespace = "http://bars"), XmlArrayItem("Data")] //<--
public List<Data> Info { get; set; }
}
The XML is missing a closing tag for Info. Also, you need to defined the Field3 property in the Data class and add the namespace 'http://bars' to it.
If your xml is missing the info closing tag, meant to contain the Data elements then your classes should look something like:
[XmlRoot(ElementName = "FooBars", Namespace = "http://foos")]
public class FooBars
{
[XmlElement(ElementName = "Id1", Namespace = "http://bars")]
public string Id1 { get; set; }
[XmlElement(ElementName = "Id2", Namespace = "http://bars")]
public string Id2 { get; set; }
[XmlElement(ElementName = "Info", Namespace = "http://bars")]
public Info Information { get;set; }
}
public class Info {
[XmlElement(ElementName = "Data", Namespace = "")]
public Info[] Info { get; set; }
}
public class Data
{
[XmlElement(ElementName = "Field1")]
public string Field1 { get; set; }
[XmlElement(ElementName = "Field2")]
public string Field2 { get; set; }
}
Notice how in your xml the info object basically contains all the data, not the FooBars like you designed the classes.
via LinqPad - you can see the Field properties are null in the myData.Info property, and that is your problem, right? UPDATED:
void Main()
{
string xmlString;
string path = #"C:\Temp\exampleXmlSO.xml";
using (StreamReader streamReader = File.OpenText(path))
{
xmlString = streamReader.ReadToEnd();
}
XmlSerializer serializer = new XmlSerializer(typeof(FooBars));
using (StringReader stringReader = new StringReader(xmlString))
{
var myData = (FooBars)serializer.Deserialize(stringReader);
Console.WriteLine(myData);
}
}
[XmlRoot(ElementName = "FooBars", Namespace = "http://foos")]
public class FooBars
{
[XmlElement(ElementName = "Id1", Namespace = "http://bars")]
public string Id1 { get; set; }
[XmlElement(ElementName = "Id2", Namespace = "http://bars")]
public string Id2 { get; set; }
[XmlArray(ElementName = "Info", Namespace = "http://bars"), XmlArrayItem("Data")] //<--
public List<Data> Info { get; set; }
}
[Serializable]
public class Data
{
[XmlElement(ElementName = "Field1")]
public string Field1 { get; set; }
[XmlElement(ElementName = "Field2")]
public string Field2 { get; set; }
[XmlIgnore]
public string Field3 { get; set; }
}

.NET Xml deserialization, issue/error with xsi:type attribute

VS2008, .NET Framework 3.5
We're utilizing the WebEx Xml API. Here's a sample Xml response from their web service that I'm trying to deserialize into .NET classes.
<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" xmlns:com="http://www.webex.com/schemas/2002/06/common"
xmlns:event="http://www.webex.com/schemas/2002/06/service/event"><serv:header><serv:response><serv:result>SUCCESS</serv:result><serv:gsbStatus>PRIMARY</s
erv:gsbStatus></serv:response></serv:header>
<serv:body>
<serv:bodyContent xsi:type="event:lstsummaryEventResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<event:matchingRecords>
<serv:total>2</serv:total>
<serv:returned>2</serv:returned>
<serv:startFrom>1</serv:startFrom>
</event:matchingRecords>
<event:event>
<event:sessionKey>999999</event:sessionKey>
<event:sessionName>Test Event 1</event:sessionName>
<event:sessionType>129</event:sessionType>
<event:hostWebExID>SomeName</event:hostWebExID>
<event:startDate>03/28/2012 14:30:00</event:startDate>
<event:endDate>03/28/2012 14:45:00</event:endDate>
<event:timeZoneID>11</event:timeZoneID>
<event:duration>15</event:duration>
<event:description></event:description>
<event:status>NOT_INPROGRESS</event:status>
<event:panelists></event:panelists>
<event:listStatus>PUBLIC</event:listStatus>
</event:event>
</serv:bodyContent>
</serv:body>
</serv:message>
Here's the class that we're deserializing into:
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Collections.Generic;
namespace Masonite.MTier.WebEx
{
[Serializable()]
[XmlRoot("message", Namespace = "http://www.webex.com/schemas/2002/06/service")]
public class lstsummaryEventResponsexx
{
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
public lstsummaryEventResponsexx()
{
xmlns.Add("serv", "http://www.webex.com/schemas/2002/06/service");
xmlns.Add("com", "http://www.webex.com/schemas/2002/06/common");
xmlns.Add("event", "http://www.webex.com/schemas/2002/06/service/event");
}
[XmlElement(ElementName = "header")]
public Header header { get; set; }
[XmlElement(ElementName = "body")]
public Body body { get; set; }
[Serializable()]
[XmlRoot("header")]
public class Header
{
[XmlElement(ElementName = "response")]
public Response response { get; set; }
}
[Serializable()]
[XmlRoot("body")]
[XmlInclude(typeof(lstsummaryEventResponse))]
public class Body
{
[XmlElement(ElementName = "bodyContent", Form = XmlSchemaForm.Qualified)]
public BodyContent bodyContent { get; set; }
}
[Serializable()]
public class lstsummaryEventResponse
{
}
[Serializable()]
[XmlRoot("response")]
public class Response
{
[XmlElement(ElementName = "result")]
public string result { get; set; }
[XmlElement(ElementName = "reason")]
public string reason { get; set; }
[XmlElement(ElementName = "gsbStatus")]
public string gsbStatus { get; set; }
[XmlElement(ElementName = "exceptionID")]
public string exceptionID { get; set; }
}
[Serializable()]
[XmlRoot("bodyContent")]
public class BodyContent
{
[XmlElement(ElementName = "matchingRecords", Namespace = "http://www.webex.com/schemas/2002/06/service/event")]
public MatchingRecords matchingRecords { get; set; }
[XmlElement(ElementName = "event", Namespace = "http://www.webex.com/schemas/2002/06/service/event")]
public List<EventSummary> events { get; set; }
}
[Serializable()]
[XmlRoot("matchingRecords")]
public class MatchingRecords
{
[XmlElement(ElementName = "total", Namespace = "http://www.webex.com/schemas/2002/06/service")]
public int total { get; set; }
[XmlElement(ElementName = "returned", Namespace = "http://www.webex.com/schemas/2002/06/service")]
public int returned { get; set; }
[XmlElement(ElementName = "startFrom", Namespace = "http://www.webex.com/schemas/2002/06/service")]
public int startFrom { get; set; }
}
[Serializable()]
[XmlRoot("event")]
public class EventSummary
{
[XmlElement(ElementName = "sessionKey")]
public long sessionKey { get; set; }
[XmlElement(ElementName = "sessionName")]
public string sessionName { get; set; }
[XmlElement(ElementName = "sessionType")]
public int sessionType { get; set; }
[XmlElement(ElementName = "hostWebExID")]
public string hostWebExID { get; set; }
[XmlElement(ElementName = "startDate")]
public string startDate { get; set; }
[XmlElement(ElementName = "endDate")]
public string endDate { get; set; }
[XmlElement(ElementName = "timeZoneID")]
public int timeZoneID { get; set; }
[XmlElement(ElementName = "duration")]
public int duration { get; set; }
[XmlElement(ElementName = "description")]
public string description { get; set; }
[XmlElement(ElementName = "status")]
public string status { get; set; }
[XmlElement(ElementName = "panelists")]
public string panelists { get; set; }
[XmlElement(ElementName = "listStatus")]
public listingType listStatus { get; set; }
}
}
}
The error I'm receiving:
The specified type was not recognized: name='lstsummaryEventResponse', namespace='http://www.webex.com/schemas/2002/06/service/event', at <bodyContent xmlns='http://www.webex.com/schemas/2002/06/service'>
I'm not sure how to provide the type lstsummaryEventResponse for the Deserialize method. I added another serializable class to my class above using that name, but get the same error. Any thoughts?
BodyContent can have the type event:lstsummaryEventResponse - so you have to declare the corresponding class, and then decorate the declaration of BodyContent as follows:
[Serializable()]
[XmlRoot("bodyContent")]
[XmlInclude("lstsummaryEventResponse")]
public class BodyContent {
}
Having said that, creating C# class with a serialization corresponding to some arbitrary XML is pretty tricky, I am not sure it is right approach

Google Picasa API XML deserialization - follow up

Im trying to write a class to deserialize the picasa album feed. It has been pretty good so far, got some help to get started (here) and I'm almost there. I have a problem with two fields right now.
The gphoto:bytesUsed and gphoto:numphotosremaining want deserialize. I can't find anything wrong with it. Thankfull for any suggestions.
Url to feed: https://picasaweb.google.com/data/feed/api/user/{username}
Class code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace WindowsFormsApplication4
{
[XmlType("feed", Namespace = "http://www.w3.org/2005/Atom")]
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class picasa
{
[XmlAttribute]
public string id { get; set; }
[XmlAttribute]
public string title { get; set; }
[XmlAttribute]
public string icon { get; set; }
[XmlElement("link")]
public FeedLink[] links { get; set; }
[XmlElement("generator")]
public FeedGenerator generator { get; set; }
[XmlElement("author")]
public FeedAuthor author { get; set; }
[XmlElement(Namespace = "http://a9.com/-/spec/opensearch/1.1/")]
public int totalResults { get; set; }
[XmlElement("entry")]
public FeedEntry[] entries { get; set; }
[XmlType("entry", Namespace = "http://www.w3.org/2005/Atom")]
public partial class FeedEntry
{
[XmlElement]
public string id { get; set; }
[XmlElement]
public FeedCategory category { get; set; }
[XmlElement]
public string title { get; set; }
[XmlElement]
public string summary { get; set; }
[XmlElement]
public string rights { get; set; }
[XmlElement("link")]
public FeedLink[] links { get; set; }
[XmlElement("author")]
public FeedAuthor author { get; set; }
[XmlElement(ElementName = "id", Namespace = "http://schemas.google.com/photos/2007")]
public string gid { get; set; }
[XmlElement(ElementName = "name", Namespace = "http://schemas.google.com/photos/2007")]
public string gname { get; set; }
[XmlElement(ElementName = "location", Namespace = "http://schemas.google.com/photos/2007")]
public string glocation { get; set; }
[XmlElement(ElementName = "access", Namespace = "http://schemas.google.com/photos/2007")]
public string gaccess { get; set; }
[XmlElement(ElementName = "timestamp", Namespace = "http://schemas.google.com/photos/2007")]
public string gtimestamp { get; set; }
[XmlElement(ElementName = "numphotos", Namespace = "http://schemas.google.com/photos/2007")]
public int gnumphotos { get; set; }
[XmlElement(ElementName = "numphotosremaining", Namespace = "http://schemas.google.com/photos/2007")]
public int gnumphotosremaining { get; set; }
[XmlElement(ElementName = "bytesUsed", Namespace = "http://schemas.google.com/photos/2007")]
public int gbytesUsed { get; set; }
[XmlElement(ElementName = "user", Namespace = "http://schemas.google.com/photos/2007")]
public string guser { get; set; }
[XmlElement(ElementName = "nickname", Namespace = "http://schemas.google.com/photos/2007")]
public string gnickname { get; set; }
[XmlElement(ElementName = "commentingEnabled", Namespace = "http://schemas.google.com/photos/2007")]
public string gcommentingEnabled { get; set; }
[XmlElement(ElementName = "commentCount", Namespace = "http://schemas.google.com/photos/2007")]
public int gcommentCount { get; set; }
[XmlElement(ElementName = "group", Namespace = "http://search.yahoo.com/mrss/")]
public MediaGroup mgroup { get; set; }
}
#region XML types
[XmlType("author", Namespace = "http://www.w3.org/2005/Atom")]
public partial class FeedAuthor
{
[XmlElement(ElementName = "name", Namespace = "http://www.w3.org/2005/Atom")]
public string name { get; set; }
[XmlElement(ElementName = "uri", Namespace = "http://www.w3.org/2005/Atom")]
public string uri { get; set; }
}
[XmlType("category", Namespace = "http://www.w3.org/2005/Atom")]
public partial class FeedCategory
{
[XmlAttribute(AttributeName = "scheme", Namespace = "http://www.w3.org/2005/Atom")]
public string scheme { get; set; }
[XmlAttribute(AttributeName = "term", Namespace = "http://www.w3.org/2005/Atom")]
public string term { get; set; }
}
[XmlType("link", Namespace = "http://www.w3.org/2005/Atom")]
public partial class FeedLink
{
[XmlAttribute(AttributeName = "rel", Namespace = "http://www.w3.org/2005/Atom")]
public string rel { get; set; }
[XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2005/Atom")]
public string type { get; set; }
[XmlAttribute(AttributeName = "href", Namespace = "http://www.w3.org/2005/Atom")]
public string href { get; set; }
}
[XmlType("generator", Namespace = "http://www.w3.org/2005/Atom")]
public partial class FeedGenerator
{
[XmlAttribute(AttributeName = "version", Namespace = "http://www.w3.org/2005/Atom")]
public string version { get; set; }
[XmlAttribute(AttributeName = "uri", Namespace = "http://www.w3.org/2005/Atom")]
public string uri { get; set; }
[XmlText]
public string text { get; set; }
}
[XmlType("group", Namespace = "http://search.yahoo.com/mrss/")]
public partial class MediaGroup
{
[XmlElement(ElementName = "content", Namespace = "http://search.yahoo.com/mrss/")]
public MediaContent mgcontent { get; set; }
[XmlElement(ElementName = "credit", Namespace = "http://search.yahoo.com/mrss/")]
public string mgcredit { get; set; }
[XmlElement(ElementName = "description", Namespace = "http://search.yahoo.com/mrss/")]
public MediaDescription mgdescription { get; set; }
}
[XmlType("content", Namespace = "http://search.yahoo.com/mrss/")]
public partial class MediaContent
{
[XmlAttribute(AttributeName = "url", Namespace = "http://search.yahoo.com/mrss/")]
public string url { get; set; }
[XmlAttribute(AttributeName = "type", Namespace = "http://search.yahoo.com/mrss/")]
public string type { get; set; }
[XmlAttribute(AttributeName = "medium", Namespace = "http://search.yahoo.com/mrss/")]
public string medium { get; set; }
}
[XmlType("description", Namespace = "http://search.yahoo.com/mrss/")]
public partial class MediaDescription
{
[XmlAttribute("type", Namespace="http://search.yahoo.com/mrss/")]
public string type {get;set;}
[XmlText]
public string description { get; set; }
}
#endregion
}
}
Your solution could be dramatically simplified making use of the .NET library for the Google Data API. Plus here is a tutorial that you might find interesting: Creating your first Picasa Web C# Application.

Categories