Serializing XML to Class C#

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?
flynnyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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
flynnyAuthor Commented:
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
Fernando SotoRetiredCommented:
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
Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.