Solved

Serializing XML to Class C#

Posted on 2014-11-12
4
147 Views
Last Modified: 2014-12-09
Hi all I have a xml file which I want to serialize to a class.

Now I want to serialize an array of items, before I have had to encase the items in a parent node for example

in the xml file
<myclass>
<item1></item1>
<item2></item2>
<parent>
<child></child>
<child></child>
<child></child>
</parent>
</myclass>

Open in new window


and in the class

[XmlArray("parent")]
[XmlArrayItem("child", typeof(myclass))]
public myclass[] children { get; set; }

Open in new window


Now is it possible to lose the encasing element? i.e. the the parent node? and load seelcted elements into an array? Or do I need this preceeding element?
0
Comment
Question by:flynny
  • 3
4 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40437470
So you have a XML file and you want to create a class with its data, is that correct?

What do you want this class to look like? Can you post the class definition please.
0
 

Author Comment

by:flynny
ID: 40437686
Hi Fernando,

thanks for the reply.

Basically I want to create the array as above, but in the XML i would ideally like to remove the parent node wrapping the items. i.e. so i have for example

<myclass>
<element1></element1>
<element2></element2>
<element3></element3>
<element3></element3>
<element3></element3>
</myclass>

Open in new window


so the class would be as follows

myclass {

string element1
string element2;
element3[] elements;

}

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40437722
So you are saying that the XML has this format. It has only one element1 and one element2 and multiple values of element3 and no other nodes are defined, correct? Also are these the actual node names that will be in the document? If they are not what will be the mapping of the nodes in the XML to what you want in the class?

<myclass>
<element1></element1>
<element2></element2>
<element3></element3>
<element3></element3>
<element3></element3>
</myclass>

Open in new window

0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40437890
Hi RadhaKrishnaKiJaya;

Here is some sample code of how this may be Accomplished.

// Load the XML into memory. Change the file path and file name on the next line
XElement elements = XElement.Load("C:/working directory/myclass.xml");

// Create a new instance of the myclass
myclass mc = new myclass();
// fill its members with the correct values
mc.element1 = elements.Elements().Where( e1 => e1.Name == "element1").Select(e1 => e1.Value).FirstOrDefault();
mc.element2 = elements.Elements().Where( e2 => e2.Name == "element2").Select(e2 => e2.Value).FirstOrDefault();
mc.elements = elements.Elements().Where( e3 => e3.Name == "element3").Select(e3 => e3.Value).ToArray();


// Using this version of myclass
public class myclass 
{
    public string element1 { get; set; }
    public string element2 { get; set; }
    public string[] elements { get; set; }
}

// Using this version of the XML file.
<myclass>
  <element1>element1</element1>
  <element2>element2</element2>
  <element3>element3</element3>
  <element3>element3</element3>
  <element3>element3</element3>
</myclass>

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Moving code from one TFS branch to another 3 25
InputLanguage 1 27
List<PaisEntity> - show some 1 24
Winform Module - What is the ASP.Net equiv 2 23
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question