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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:10:13+00:00 2026-06-18T07:10:13+00:00

I have a XML document that looks like this: <Person> <LastName>LastName1</LastName> <FirstName>FirstName1</FirstName> <MiddleName>MiddleName1</MiddleName> </Person>

  • 0

I have a XML document that looks like this:

<Person>
  <LastName>LastName1</LastName>
  <FirstName>FirstName1</FirstName>
  <MiddleName>MiddleName1</MiddleName>
</Person>

Originally I had a method to create this structure like below:

public XElement ToXML()
{
  return new XElement("Person",
    new XElement(this.LastName.ToXML()),
    new XElement(this.FirstName.ToXML()),
    new XElement(this.MiddleName.ToXML()));
}

The problem is there are a lot of other nodes other than just Person that use the Name values within the root. So what I tried doing was refactoring out the FirstName, LastName, and MiddleName elements to be in their own reusable class with a ToXMLDoc() method that returns those elements as a XDocument instead of a XElement (since the root will be dictated by the class needing the name children; might be Person, Employee, etc.)

This is what my new ToXMLDoc returns:

return new XDocument(new XElement(this.LastName.ToXML()),
                     new XElement(this.FirstName.ToXML()),
                     new XElement(this.MiddleName.ToXML()));

My problem is I want to now add this content to within the root XElement added by my Person class. I tried doing something like below, but I’m not using the constructor properly and getting a “Ambiguous constructor reference” error.

return new XElement("Person",
    foreach (XElement xe in NameType.ToXMLDoc().Nodes())
    {
      new XElement(xe.Value);
    }
);

How can I take the contents from the ToXMLDoc() method and add them to the XElement node being created for Person? Any help is appreciated, thanks!

  • 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-18T07:10:14+00:00Added an answer on June 18, 2026 at 7:10 am

    You’re currently trying to embed a foreach loop within a constructor call. That’s not going to work – but it’s actually pretty simple:

    return new XElement("Person", NameType.ToXMLDoc()
                                          .Nodes()
                                          .Select(xe => new XElement(xe.Value));
    

    Are you sure you don’t just want to copy the elements wholesale though? In which case it would just be:

    return new XElement("Person", NameType.ToXMLDoc().Nodes());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple XML document that looks like this: <Person> <LastName>LastName1</LastName> <FirstName>FirstName1</FirstName> <MiddleName>MiddleName1</MiddleName>
I have an xml document that looks like this. <foo> <bar type=artist/> Bob Marley
I have an XML document that looks like this <Elements> <Element> <DisplayName /> <Type
So i have an XML document that looks like this: <?xml version=1.0 encoding=UTF-8?> <gesmes:Envelope
Why would you have a rootnode in an XML document that looks like this:
I have an XML document that looks like this. It may be that it's
I have an xml formatted document that looks like this: <?xml version=1.0 encoding=windows-1250?> <
I have an xml document with multiple nodes that looks something like this: <Result>
I have an XML document that looks like this: <file> <name>NAME_OF_FILE</name> </file> <file> <name>NAME_OF_FILE</name>
I have to parse an XML document that looks like this: <?xml version=1.0 encoding=UTF-8

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.