Convert an XML file to a Entity class

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi Guys,
I have an xml file as CompanyLogoDB.xml
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <<span style="font-family:Consolas; color:#a31515; font-size:9.5pt Logos<span style="font-family:Consolas; color:blue; font-size:9.5pt >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt Logo<span style="font-family:Consolas; color:blue; font-size:9.5pt >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt imageNo<span style="font-family:Consolas; color:blue; font-size:9.5pt ><span style="font-family:Consolas; font-size:9.5pt 1<span style="color:blue </<span style="color:#a31515 imageNo<span style="color:blue >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt companyLogo<span style="font-family:Consolas; color:blue; font-size:9.5pt ><span style="font-family:Consolas; font-size:9.5pt "C:arti.agarwaProjectsLogos
QuizLogos QuizImagesthumbs_audi.jpeg"<span style="color:blue </<span style="color:#a31515 companyLogo<span style="color:blue >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt conpanyName<span style="font-family:Consolas; color:blue; font-size:9.5pt ><span style="font-family:Consolas; font-size:9.5pt Audi<span style="color:blue </<span style="color:#a31515 conpanyName<span style="color:blue >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt hint<span style="font-family:Consolas; color:blue; font-size:9.5pt ><span style="font-family:Consolas; font-size:9.5pt _U__<span style="color:blue </<span style="color:#a31515 hint<span style="color:blue >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt isCorrect<span style="font-family:Consolas; color:blue; font-size:9.5pt ><span style="font-family:Consolas; font-size:9.5pt false<span style="color:blue </<span style="color:#a31515 isCorrect<span style="color:blue >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
</<span style="font-family:Consolas; color:#a31515; font-size:9.5pt Logo<span style="font-family:Consolas; color:blue; font-size:9.5pt >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt Logo<span style="font-family:Consolas; color:blue; font-size:9.5pt >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt imageNo<span style="font-family:Consolas; color:blue; font-size:9.5pt ><span style="font-family:Consolas; font-size:9.5pt 2<span style="color:blue </<span style="color:#a31515 imageNo<span style="color:blue >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt companyLogo<span style="font-family:Consolas; color:blue; font-size:9.5pt ><span style="font-family:Consolas; font-size:9.5pt C:arti.agarwaProjectsLogos
QuizLogos QuizImagesthumbs_honda.jpeg<span style="color:blue </<span style="color:#a31515 companyLogo<span style="color:blue >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt conpanyName<span style="font-family:Consolas; color:blue; font-size:9.5pt ><span style="font-family:Consolas; font-size:9.5pt Honda<span style="color:blue </<span style="color:#a31515 conpanyName<span style="color:blue >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt hint<span style="font-family:Consolas; color:blue; font-size:9.5pt ><span style="font-family:Consolas; font-size:9.5pt _O___<span style="color:blue </<span style="color:#a31515 hint<span style="color:blue >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt isCorrect<span style="font-family:Consolas; color:blue; font-size:9.5pt ><span style="font-family:Consolas; font-size:9.5pt false<span style="color:blue </<span style="color:#a31515 isCorrect<span style="color:blue >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
</<span style="font-family:Consolas; color:#a31515; font-size:9.5pt Logo<span style="font-family:Consolas; color:blue; font-size:9.5pt >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt <span>
<<span style="font-family:Consolas; color:#a31515; font-size:9.5pt Logo<span style="font-family:Consolas; color:blue; font-size:9.5pt >
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt </<span style="font-family:Consolas; color:#a31515; font-size:9.5pt Logos<span style="font-family:Consolas; color:blue; font-size:9.5pt >
There are 500’s of entries like this.
I have an Entity “LogoBE.cs”
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt namespace<span style="font-family:Consolas; font-size:9.5pt Logos_Quiz
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt {
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green //Logo Class containing all the properties of a company logo
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:blue class
<span style="color:#2b91af Logo
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green //Unique Image No.
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
[<span style="color:#2b91af XmlElement(ElementName = <span style="color:#a31515
"imageNo")]
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:blue int ImageNo {
<span style="color:blue get; <span style="color:blue set; }
<p style="text-autospace:
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green //Image of the company logo
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
[<span style="color:#2b91af XmlElement(ElementName = <span style="color:#a31515
"companyLogo")]
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:#2b91af Image CompanyLogo {
<span style="color:blue get; <span style="color:blue set; }
<p style="text-autospace:
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green //Hint for the company logo
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
[<span style="color:#2b91af XmlElement(ElementName = <span style="color:#a31515
"hint")]
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:blue string Hint {
<span style="color:blue get; <span style="color:blue set; }
<p style="text-autospace:
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green //Name of the company
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span> [<span style="color:#2b91af XmlElement(ElementName =
<span style="color:#a31515 "conpanyName")]
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:blue string CompanyName {
<span style="color:blue get; <span style="color:blue set; }
<p style="text-autospace:
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green //Answer given by user is correct or incorrect
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
[<span style="color:#2b91af XmlElement(ElementName = <span style="color:#a31515
"isCorrect")]
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:blue bool IsCorrect {
<span style="color:blue get; <span style="color:blue set; }
<p style="text-autospace:
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
}
<p style="text-autospace:
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
[<span style="color:#2b91af XmlTypeAttribute(AnonymousType = <span style="color:blue
true)]
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:blue class
<span style="color:#2b91af LogosData
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
[<span style="color:#2b91af XmlElement(<span style="color:#a31515 "Logo")]
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:#2b91af List<<span style="color:#2b91af Logo> logosList {
<span style="color:blue get; <span style="color:blue set; }
<p style="text-autospace:
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public LogosData()
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
logosList = <span style="color:blue new <span style="color:#2b91af
List<<span style="color:#2b91af Logo>();
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
}
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt <span>
}
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt }
Now I need to convert the XML into the list of logo class. The code I am using to do the same is :
<p style="text-autospace: <span style="font-family:Consolas; color:#2b91af; font-size:9.5pt List<span style="font-family:Consolas; font-size:9.5pt <<span style="color:#2b91af Logo> logosList =
<span style="color:blue new <span style="color:#2b91af List<<span style="color:#2b91af Logo>();<span>

<p style="text-autospace: <span style="font-family:Consolas; color:#2b91af; font-size:9.5pt XmlTextReader<span style="font-family:Consolas; font-size:9.5pt reader =
<span style="color:blue new <span style="color:#2b91af XmlTextReader(<span style="color:#a31515 @"C:Logos QuizLogos QuizCompanyLogoDB.xml");
<p style="text-autospace:
<p style="text-autospace: <span style="font-family:Consolas; color:blue; font-size:9.5pt object<span style="font-family:Consolas; font-size:9.5pt myXmlClass = (<span style="color:blue object)(<span style="color:blue new
<span style="color:#2b91af XmlSerializer(<span style="color:blue typeof(<span style="color:#2b91af List<<span style="color:#2b91af Logo>))).Deserialize(reader);
<p style="text-autospace: <span style="font-family:Consolas; font-size:9.5pt logosList = (<span style="color:#2b91af List<<span style="color:#2b91af Logo>)myXmlClass;
<p style="text-autospace:
<p style="text-autospace: <span style="font-size:10pt This code is giving error onto the last line as the
<span style="color:blue object myXmlClass is coming as null.
<p style="text-autospace: <span style="font-size:10pt Please help so that I can read the XML with the most efficient way & gives the best performance.
Thanks for your help in advance.

View the full article
 
Back
Top