Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4109794
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:50:52+00:00 2026-05-20T21:50:52+00:00

public struct Parameter { public Parameter(string name, string type, string parenttype) { this.Name =

  • 0
public struct Parameter 
{
    public Parameter(string name, string type, string parenttype)  
    {        
        this.Name = name;
        this.Type = type;
        this.ParentType = parenttype;        
    }
    public string Name;
    public string Type;
    public string ParentType;
}

Following values are stored in the array of Parameter:

Name        Type                 ParentType
-------------------------------------------------------
composite   CompositeType        
isThisTest  boolean              
BoolValue   boolean              CompositeType
StringValue string               CompositeType
AnotherType AnotherCompositeType CompositeType
account     string               AnotherCompositeType
startdate   date                 AnotherCompositeType

I want to read this to build an xml. something like:

<composite>
    <BoolValue>boolean</BoolValue>
    <StringValue>string</StringValue>
    <AnotherType>
        <account>string</account>
        <startdate>date</startdate>
    </AnotherType>    
<composite>
<isThisTest>boolean</isThisTest>

I am using the following logic to read the values:

foreach (Parameter parameter in parameters)
{
    sb.Append("        <" + parameter.Name + ">");
    //HERE: need to check parenttype and get all it's child elements
    //
    sb.Append("</" + parameter.Name + ">" + CrLf);
}

Is there a simpler way to read the array to get the parents and thier child? May be using LINQ? I still on .Net 3.5. Appreciate any suggestions with example code 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:50:52+00:00Added an answer on May 20, 2026 at 9:50 pm

    You could write a little recursive method to deal with this :

    IEnumerable<XElement> GetChildren ( string rootType, List<Parameter> parameters )
    {
        return from p in parameters
            where p.ParentType == rootType
            let children = GetChildren ( p.Type, parameters )
            select  children.Count() == 0 ? 
                new XElement ( p.Name, p.Type ) :
                new XElement ( p.Name, children );
    }
    

    Each call builds up an Enumerable of XElements which contains the parameters whose parent has the passed in type. The select recurses into the method again finding the children for each Element.

    Note that this does assume that the data is correctly formed. If two parameters has eachother as a parent you will get a Stack Overflow.

    The magic is in the XElements class (Linq to Xml) that accepts enumerables of XElements to build up the tree like Xml structure.

    The first call, pass null (or use default parameters if using C# 4) as the rootType. Use like :

    void Main()
    {
        var parameters = new List<Parameter> {
            new Parameter {Name = "composite", Type = "CompositeType" },
            new Parameter {Name = "isThisTest", Type = "boolean" },
            new Parameter {Name = "BoolValue", Type = "boolean", ParentType = "CompositeType" },
            new Parameter {Name = "StringValue", Type = "string", ParentType = "CompositeType" },
            new Parameter {Name = "AnotherType", Type = "AnotherCompositeType", ParentType = "CompositeType" },
            new Parameter {Name = "account", Type = "string", ParentType = "AnotherCompositeType" },
            new Parameter {Name = "startdate", Type = "date", ParentType = "AnotherCompositeType" }
        };
    
        foreach ( var r in GetChildren ( null, parameters ) )
        {
            Console.WriteLine ( r );
        }
    
    }
    

    Output :

    <composite>
      <BoolValue>boolean</BoolValue>
      <StringValue>string</StringValue>
      <AnotherType>
        <account>string</account>
        <startdate>date</startdate>
      </AnotherType>
    </composite> 
    <isThisTest>boolean</isThisTest>
    

    Edit

    In response to your comment, XElement gives you two options for outputting as a string.

    ToString() will output formatted Xml.

    ToString(SaveOptions) allows you to specify formatted or unformatted output as well as ommitting duplicate namespaces.

    I’m sure you could probably adapt the solution to use StringBuilder if you really had to, although it probably wouldn’t be as elegant..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a struct like this: public struct SomeStruct { public SomeStruct(String stringProperty, Int32 intProperty)
I have a structure in C#: public struct UserInfo { public string str1 {
If I define a struct in C# using automatic properties like this: public struct
I have some generic types, like the following: public struct Tuple<T1, T2> { ...
I get an error when I compile this code: using System; public struct Vector2
I have this struct: struct Map { public int Size; public Map ( int
Is the following code portable? template<typename In> struct input_sequence_range : public pair<In,In> { input_sequence_range(In
public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);
Option Explicit On Option Strict On Public Class Class1 Dim l As List(Of A)
public class MyClass { public int Age; public int ID; } public void MyMethod()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.