Solved

Serializing XML to Class C#

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What's the most efficient way to load up a couple of large table in C#? 5 36
coded character set iptc field within a jpeg 1 68
Problem to error 4 43
Host asp.net pages 5 25
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

911 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

22 Experts available now in Live!

Get 1:1 Help Now