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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:38:37+00:00 2026-06-14T11:38:37+00:00

I have a class that various different XML schemes are created from. I create

  • 0

I have a class that various different XML schemes are created from. I create the various dynamic XDocuments via one (Very long) statement using conditional operators for optional elements and attributes.

I now need to convert the XDocuments back to the class but as they are coming from different schemes many elements and sub elements may be optional. The only way I know of doing this is to use a lot of if statements.

This approach doesn’t seem very LINQ and uses a great deal more code than when I create the XDocument so I wondered if there is a better way to do this?

An example would be to get

<?xml version="1.0"?>
<root xmlns="somenamespace">
    <object attribute1="This is Optional" attribute2="This is required">
        <element1>Required</element1>
        <element1>Optional</element1>
        <List1>
            Optional List Of Elements
        </List1>
        <List2>
            Required List Of Elements
        </List2>
    </object>
</root>

Into

public class Object()
{
    public string Attribute1;
    public string Attribute2;
    public string Element1;
    public string Element2;
    public List<ListItem1> List1;
    public List<ListItem2> List2;
}

In a more LINQ friendly way than this:

public bool ParseXDocument(string xml)
{
    XNamespace xn = "somenamespace";            
    XDocument document = XDocument.Parse(xml);

    XElement elementRoot = description.Element(xn + "root");
    if (elementRoot != null)
    {
        //Get Object Element
        XElement elementObject = elementRoot.Element(xn + "object");
        if(elementObject != null)
        {
            if(elementObject.Attribute(xn + "attribute1") != null)
            {
                Attribute1 = elementObject.Attribute(xn + "attribute1");
            }
            if(elementObject.Attribute(xn + "attribute2") != null)
            {
                Attribute2 = elementObject.Attribute(xn + "attribute2");
            }
            else
            {
                //This is a required Attribute so return false
                return false;
            }
            //If, If/Elses get deeper and deeper for the next elements and lists etc.... 
        }
        else
        {
            //Object is a required element so return false
            return false;
        }
    }
    else
    {
        //Root is a required element so return false
        return false;
    }
    return true;
}

Update: Just to clarify the ParseXDocument method is inside the “Object” class. Every time an xml document is received the Object class instance has some or all of it’s values updated.

  • 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-14T11:38:38+00:00Added an answer on June 14, 2026 at 11:38 am
    • Have a look at XElement and XAttribute Type Conversions.

    • The document root is always non-null.

    • Validate the object afterwards, so you don’t have to implement validation twice (once when parsing and once before saving).

    Code:

    private static readonly XNamespace xn = "somenamespace";
    
    public bool ParseXDocument(string xml)
    {
        XDocument document = XDocument.Parse(xml);
    
        var obj = document.Root.Element(xn + "object");
        if (obj == null)
            return false;
    
        Attribute1 = (string)obj.Attribute("attribute1");
        Attribute2 = (string)obj.Attribute("attribute2");
        Element1 = (string)obj.Element(xn + "element1");
        Element2 = (string)obj.Elements(xn + "element1").ElementAtOrDefault(1);
        // ...
    
        return Validate();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a Javascript class/object that allow me to have various method:
I have a class with a member Address that I read from an XML
I have a ListView that uses different XML files to create Views and make
I have a class that has an Action<string> called DisplayData ; In various points
I have class that represents users. Users are divided into two groups with different
I have a class that I instantiate to save or load xml data. For
I have an application that is driven by a configuration XML: various app properties
I have a class that has a dozen or so properties that represent various
I have a listener class that accepts GUI change events in one method. The
I have a legacy HTTP/XML service that I need to interact with for various

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.