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 8365811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:45:05+00:00 2026-06-09T12:45:05+00:00

I am having some issues with my DynamicXML object, it is parsing XML great,

  • 0

I am having some issues with my DynamicXML object, it is parsing XML great, and for single elements works great, but when I have multiple children elements its falling over.
I am obviously missing something, but I dont know what.

Here is the class:

public class DynamicXml : DynamicObject
{
    readonly XElement element;

    public DynamicXml(string xml)
    {
        element = XElement.Parse(xml);
    }

    public DynamicXml(XElement xElement)
    {
        element = xElement;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (element == null)
        {
            result = null;
            return false;
        }

        var sub = element.Element(binder.Name);

        if (sub == null)
        {
            result = null;
            return false;
        }
        else
        {
            result = new DynamicXml(sub);
            return true;
        }
    }

    public override string ToString()
    {
        return element != null ? element.Value : string.Empty;
    }

    public override bool TryConvert(ConvertBinder binder, out object result)
    {
        try
        {
            result = Convert.ChangeType(element.Value, binder.ReturnType);
            return true;
        }
        catch { }

        return base.TryConvert(binder, out result);
    }
}

This is how I would use it

var response = "<response><foo>some xml here</foo></response>"
result = new DynamicXml(response);

var newObj = new SumObject();
newObj.Foo = result.foo;

Now this works fine, but if my xml were:

<response>
   <foo>
     <bahs>
        <bah>value here</bah>
        <bah>value here</bah>
        <bah>value here</bah>
     </bahs>
   </foo>
<response>

I would want to use some thing like:

for(var bahs in result.foo.bahs)
{
   //magic code here
}

but this isnt what is happening I just get an error:

Cannot implicitly convert type 'DynamicXml' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)

Many thanks for any help.

  • 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-09T12:45:07+00:00Added an answer on June 9, 2026 at 12:45 pm

    I figured out the solution, I needed to implement IEnumerable and GetEnumerator()

    here is the solution class:

        public class DynamicXml : DynamicObject, IEnumerable
        {
            readonly XElement element;
    
            public DynamicXml(string xml)
            {
                element = XElement.Parse(xml);
            }
    
            public DynamicXml(XElement xElement)
            {
                element = xElement;
            }
    
            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                if (element == null)
                {
                    result = null;
                    return false;
                }
    
                var sub = element.Element(binder.Name);
    
                if (sub == null)
                {
                    result = null;
                    return false;
                }
                else
                {
                    result = new DynamicXml(sub);
                    return true;
                }
            }
    
            public override string ToString()
            {
                return element != null ? element.Value : string.Empty;
            }
    
            public IEnumerator GetEnumerator()
            {
                return element.Elements().Select(child => new DynamicXml(child)).GetEnumerator();
            }
    
            public override bool TryConvert(ConvertBinder binder, out object result)
            {
                try
                {
                    result = Convert.ChangeType(element.Value, binder.ReturnType);
                    return true;
                }
                catch { }
    
                return base.TryConvert(binder, out result);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some issues with consuming an XML and applying multiple conditions on
I am having some issues with multiple form submissions with a jQuery/ajax form. I
having some issues with a networking assignment. End goal is to have a C
Having some issues getting this MPMoviePlayerViewController to work. I have two sample URLs pointing
I have been having some issues retrieving JSON data from a WCF service application
I am having some issues receiving UTF-8 XML files back from DHL API. As
I've been having some issues reading an XML file in my 'res' folder and
I'm having some issues deploying a Visio addin. Running the VSTO file works on
Having some issues running my code local. I have host file setup like this:
Having some issues with C. I have this is my code: // First line

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.