Solved

Serializing XML to Class C#

Posted on 2014-11-12
4
135 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 62

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 62

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 62

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now