Linq to xml and collections

R

Robert Bravery

Hi all,

I have a contact class that has as one of its properties a segment list
collection
public List<Segment> SegmentCollection { get; set; }

In the Segment class there is a segmentname property
public class Segment
{
public string SegmentName { get; set; }
}

In my zml I have the following:
<Contact>
<Id>1</Id>
<FirstName>Darrel</FirstName>
<LastName>Schreyer</LastName>
<UserName>Schreyer</UserName>
<Email>[email protected]</Email>
<Title>Mr</Title>
<CompanyName>Win on the Web</CompanyName>
<Position>Developer</Position>
<Branch>Johannesburg</Branch>
<Segments>
<Segment>
<SegmentName>IT</SegmentName>
</Segment>
<Segment>
<SegmentName>HR</SegmentName>
</Segment>
</Segments>

I can get all the other elements but cannot get the segment element into my
segment collection of my contact class
Can some one show me how using linq


Thanks
Robert
 
S

Simon Hart [MVP]

You just need to change your code slightly, you don't need to use LINQ.

IE:

public class Contact
{
public Segments Segments
{
get;
set;
}


}

public class Segments
{
public List<Segment> Segment
{
get;
set;
}

public string SegmentName
{
get;
set;
}
}

You could nest the Segments class within the Contact class but in this case
with have to have different names, currently they would have to exisit in
diff namespaces or else you'd get a name conflict problem.

Serializing the above should generate the desired result.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top