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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:33:39+00:00 2026-05-13T06:33:39+00:00

I was trying out parsing XML using Fluent interface as described here . The

  • 0

I was trying out parsing XML using Fluent interface as described here.

The example is good but gives one compiler error that I am not able to fix.

The error is in the protected constructor below:

protected DynamicXml( IEnumerable<XElement> elements )
{
    _elements = new List<XElement>( elements ); // error on this line
}

The two compiler errors I get on this line are:

 Argument 1: cannot convert from 
'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' to   
'System.Xml.Linq.XElement'

and

The best overloaded Add method    
'System.Collections.Generic.List<System.Xml.Linq.XElement>.
Add(System.Xml.Linq.XElement)' for the collection initializer 
has some invalid arguments

I have copy/pasted the full code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Dynamic;
using System.Xml.Linq;
using System.Collections;

namespace FluentXML
{
    public class DynamicXml : DynamicObject, IEnumerable
    {
        private readonly List<XElement> _elements;

        public DynamicXml(string text)
        {
            var doc = XDocument.Parse(text);
            _elements = new List<XElement> { doc.Root };
        }

        protected DynamicXml(XElement element)
        {
            _elements = new List<XElement> { element };
        }

        protected DynamicXml(IEnumerable<XElement> iteratorElem)
        {
            _elements = new List<XElement> { iteratorElem };
        } 

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;
            if (binder.Name == "Value")
                result = _elements[0].Value;
            else if (binder.Name == "Count")
                result = _elements.Count;
            else
            {
                var attr = _elements[0].Attribute(
                    XName.Get(binder.Name));
                if (attr != null)
                    result = attr;
                else
                {
                    var items = _elements.Descendants(
                        XName.Get(binder.Name));
                    if (items == null || items.Count() == 0)
                        return false;
                    result = new DynamicXml(items);
                }
            }
            return true;
        }

        public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
        {
            int ndx = (int)indexes[0];
            result = new DynamicXml(_elements[ndx]);
            return true;
        }

        public IEnumerator GetEnumerator()
        {
            foreach (var element in _elements)
                yield return new DynamicXml(element);
        }
    }
}

namespace FluentXML
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlString = FluentXML.Properties.Resources.InputString;
            dynamic dx = new DynamicXml(xmlString);
            Console.WriteLine("PublicationDate='{0}'", dx.pubdate.Value);
            Console.WriteLine("BookCount='{0}'", dx.book.Count);
            foreach (dynamic d in dx.book)
            {
                Console.WriteLine("----- Begin Book -----");
                Console.WriteLine("Price='{0}'", d.price.Value);
                Console.WriteLine("Title='{0}'", d.title.Value);
                Console.WriteLine("AuthorCount='{0}'", d.authors.author.Count);
                foreach (dynamic a in d.authors.author)
                {
                    Console.WriteLine("   ---- Begin Author ----");
                    Console.WriteLine("   EmailAddress='{0}'", a.email.address.Value);
                    Console.WriteLine("   FirstName='{0}'", a.name.first.Value);
                    Console.WriteLine("   MiddleName='{0}'", a.name.middle.Value);
                    Console.WriteLine("   LastName='{0}'", a.name.last.Value);
                    Console.WriteLine("   ----- End Author -----");
                }
                Console.WriteLine("------ End Book ------");
            }
        }
    }
}
  • 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-13T06:33:39+00:00Added an answer on May 13, 2026 at 6:33 am
        protected DynamicXml(IEnumerable<XElement> iteratorElem)
        {
            _elements = new List<XElement> { iteratorElem };
        } 
    

    is the sequence initializer. This would create a list with one element “iteratorElem” which isn’t of type XElement (as the error says).

    Calling:

        protected DynamicXml(IEnumerable<XElement> iteratorElem)
        {
            _elements = new List<XElement>(iteratorElem);
        } 
    

    should work, though. You could also write iteratorElem.ToList()

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

Sidebar

Ask A Question

Stats

  • Questions 310k
  • Answers 310k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To prevent CKEditor from removing content, the protectedSource setting will… May 13, 2026 at 10:07 pm
  • Editorial Team
    Editorial Team added an answer $('<input>').attr('type','hidden').appendTo('form'); To answer your second question: $('<input>').attr({ type: 'hidden', id:… May 13, 2026 at 10:07 pm
  • Editorial Team
    Editorial Team added an answer Not always, but usually this can be a sign that… May 13, 2026 at 10:07 pm

Related Questions

I have an XML feed (which I don't control) and I am trying to
Looking at Java and C# they manage to do some wicked processing based on
I'm using VB 2008 and I'm trying to add a xmlns=mynamespace attribute to an
I am currently working on a website that will have a high volume of
I'm trying to work out what the best method to passing a large number

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.