Solved

Serializing XML to Class C#

Posted on 2014-11-12
4
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

695 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