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

  • SEARCH
  • Home
  • 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 9009599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:14:31+00:00 2026-06-16T02:14:31+00:00

I’m new to linq to Xml. I have a recursive method which get as

  • 0

I’m new to linq to Xml.
I have a recursive method which get as parameter XElement root which should hold the XML data in a way it will represent the correlating subtree root of a given recursion depth.

void recursiveMethod(XElement root);
To be more specific,also look at this XML example:

<start>
      <Class>
           <Worker>
                <Name> Dan </Name>
                <Phone> 123 </Phone> 
                <Class>
                      <Address>
                           <Street> yellow brick road </Street>
                           <Zip Code> 123456 </Zip Code>
                      </Address>
                </Class>
            </Worker>
      </Class>
...
</start>    

As you can imagine, Name is value type whereas Address is a class reference.
The Xml information should be added dynamically through reflection ( in top down approach).

To make the long story short imagine that I’m in the middle of investigating Worker Class and reached Address Class and want to “drill down”, so I want to call my recursive method with the right reference of child nodes of the current Worker class as the new XElement root, so I will be able to add to it what I found by reflection in the Address Class one recursion depth below.

Note that this reference should be of XElement type.

How can I do that?

EDIT: If you have another idea of doing all this stuff but not with XElement I’ll be happy to hear about also, although I prefer it with XElement parameter.

Another issue:
I’ve started implementing it in a naive way like iterating through all fields (variable of FieldInfo[]), and if I had encounterd value type(IsValueType) I was doing something like

 root.Add(new XElement("Field",
                      new XElement("Type", ...),
                      new XElement("Variable Name", ...),
                      new XElement("Value", ...)));     

So ,just for general knowledge:
1. Was there a way to get only the reference of a node to its decendants ,so that in lower recursion level I’ll be able to do another root.Add(…) as above but this root will be a reference to children of previous root? (Which means doing the whole operation without Linq syntax)

2.I’ve managed to get private fields value through reflection without working with properties, is it problematic? Should I always take values through properties in reflection?

  • 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-06-16T02:14:32+00:00Added an answer on June 16, 2026 at 2:14 am

    This extension method will build XElement in required format:

    public static class Extensions
    {
        public static XElement ToXml<T>(this T obj)
        {
            Type type = typeof(T);
    
            return new XElement("Class",
                        new XElement(type.Name,
                            from pi in type.GetProperties()
                            where !pi.GetIndexParameters().Any()
                            let value = (dynamic)pi.GetValue(obj, null)
                            select pi.PropertyType.IsPrimitive || 
                                   pi.PropertyType == typeof(string) ?
                                    new XElement(pi.Name, value) : 
                                    Extensions.ToXml(value)
                            )
                        );
        }
    }
    

    What happens here:

    • We get public properties of passed object (you can add BindingFlags to filter properties).
    • Next I verify if property has index parameters and skip such properties.
    • Next I get property value as dynamic object. It’s important, otherwise property value type will be inferred as object during recursive call of ToXml<T> method.
    • I check if property type is primitive type (int, byte, long, single, double, etc) or string
    • For primitive types we write element with property value. For other types (complex) we recursively start building XElement

    Usage:

    Worker worker = new Worker()
    {
        Name = "Serge",
        Phone = "911",
        Address = new Address() { Street = "Elm street", ZipCode = 666 }
    };
    
    XElement xml = worker.ToXml();
    

    Result:

    <Class>
      <Worker>
        <Name>Serge</Name>
        <Phone>911</Phone>
        <Class>
          <Address>
            <Street>Elm street</Street>
            <ZipCode>666</ZipCode>
          </Address>
        </Class>
      </Worker>
    </Class>
    

    BUT you should be careful with situations when two objects refer each other (infinite recursion will happen in this case)

    In this case you can use something like dictionary or hashset to store all objects which already exist in your xml:

    Type type = obj.GetType();
    if (set.Contains(obj))
        return new XElement("Class", new XAttribute("name", type.Name));
    set.Add(obj);
    return new XElement("Class", ...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.