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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:10:44+00:00 2026-06-03T02:10:44+00:00

How to load XML Elements using LINQ from XDocument into a c# class. I

  • 0

How to load XML Elements using LINQ from XDocument into a c# class. I don’t want to use XDocument.Descendants because the settings are not repeated and occur only once in the XML.

This works but I think there must be another way where I don’t have to use IEnumerable or use ToArray() and use the first element [0]. Any suggestions?

CODE

public class BackupItem  // class needed so LINQ reads XML into an editable DbGrid
{
  public bool Backup { get; set; }
  public bool IncludeSubDir { get; set; }
  public string BackupLabel { get; set; }
  public string BackupPath { get; set; }
}

public class BackupSettings  
{
  public bool IncludeDateStamp { get; set; }
  public bool DatePrefix { get; set; }
  public bool DateSuffix { get; set; }
  public string DateFormat { get; set; }
  public bool ZipCompress { get; set; }
  public bool ZipPrefix { get; set; }
  public bool ZipSuffix { get; set; }
  public string ZipText { get; set; }
  public bool BackupToSiblingFolder { get; set; }
  public string SiblingFolder { get; set; }
  public bool BackupToFolder { get; set; }
  public bool IncludeFullPath { get; set; }
  public string BackupFolder { get; set; }
}


  // use a LINQ query to load xml file into datagridview and make datagrid editable (create class with get/set)
  var q = from arg in GvXMLDoc.Descendants("BackupItem")
          select new BackupItem()
          {
            Backup = (bool)arg.Element("IncludeDirectory"),
            IncludeSubDir = (bool)arg.Element("IncludeSubDirectories"),
            BackupLabel = (string)arg.Element("BackupLabel"),
            BackupPath = (string)arg.Element("BackupPath")
          };
  dataGridView1.DataSource = q.ToList();

    // load global variable
    IEnumerable<BackupSettings> GvBackupSettings = from arg in GvXMLDoc.Element("Backup").Elements("Settings")
                                                   select new BackupSettings()
            {
              IncludeDateStamp = (bool)arg.Element("IncludeDateStamp"),
              DatePrefix = (bool)arg.Element("DatePrefix"),
              DateSuffix = (bool)arg.Element("DateSuffix"),
              DateFormat = (string)arg.Element("DateFormat"),
              ZipCompress = (bool)arg.Element("ZipCompress"),
              ZipPrefix = (bool)arg.Element("ZipPrefix"),
              ZipSuffix = (bool)arg.Element("ZipSuffix"),
              ZipText = (string)arg.Element("ZipText"),
              BackupToSiblingFolder = (bool)arg.Element("BackupToSiblingFolder"),
              SiblingFolder = (string)arg.Element("SiblingFolder"),
              BackupToFolder = (bool)arg.Element("BackupToFolder"),
              IncludeFullPath = (bool)arg.Element("IncludeFullPath"),
              BackupFolder = (string)arg.Element("BackupFolder")
            };

I can access the values but it seems a very ‘messy way’.

var s = GvBackupSettings.ToArray()[0].DateSuffix;
var t = GvBackupSettings.ToArray()[0].DateFormat;

XML FILE

<?xml version='1.0'?>
<Backup>
  <Settings>
    <IncludeDateStamp>true</IncludeDateStamp>
    <DatePrefix>false</DatePrefix>
    <DateSuffix>true</DateSuffix>
    <DateFormat>yyyy-MM-dd h.m.s</DateFormat>
    <ZipCompress>false</ZipCompress>
    <ZipPrefix>false</ZipPrefix>
    <ZipSuffix>false</ZipSuffix>
    <ZipText></ZipText>
    <BackupToSiblingFolder>true</BackupToSiblingFolder>
    <SiblingFolder>backups</SiblingFolder>
    <BackupToFolder>false</BackupToFolder>
    <IncludeFullPath>true</IncludeFullPath>
    <BackupFolder>C:\\backup</BackupFolder>
  </Settings>
  <BackupItem>
    <IncludeDirectory>true</IncludeDirectory>
    <IncludeSubDirectories>true</IncludeSubDirectories>
    <BackupLabel>Backup1</BackupLabel>
    <BackupPath>C:\TestFiles\Xml\Samples</BackupPath>
  </BackupItem>
  <BackupItem>
    <IncludeDirectory>true</IncludeDirectory>
    <IncludeSubDirectories>false</IncludeSubDirectories>
    <BackupLabel>Backup2</BackupLabel>
    <BackupPath>C:\TestFiles\Xml\Samples</BackupPath>
  </BackupItem>
</Backup>

EDIT 1
I also am trying to deserialize the XML (thanks for idea) so I don’t have to cast each item. This does not work.. I don’t want to have to run the XSD tool either and have a huge class file…

  XmlRootAttribute rootAttribute = new XmlRootAttribute("Backup");
  XmlSerializer deserializer = new XmlSerializer((typeof(BackupSettings)), rootAttribute);
  GvBackupSettings = (BackupSettings)deserializer.Deserialize(XmlReader.Create(GvXMLFileName));

EDIT 2
Ended up serializing and deserializing xml as suggested below using stardard XSD.exe tool to generate the c# class. Especially as I had to read and write to same xml file. Note: Make sure you verify/modify the generated xsd file first.

  • 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-03T02:10:45+00:00Added an answer on June 3, 2026 at 2:10 am

    You don’t need a query if you just need the first element:

    XElement settings = GvXMLDoc.Element("Backup").Element("Settings");
    
    BackupSettings GvBackupSettings = new BackupSettings
    {
        IncludeDateStamp = (bool)settings.Element("IncludeDateStamp"),
        DatePrefix = (bool)settings.Element("DatePrefix"),
        ...
    };
    
    var s = GvBackupSettings.DateSuffix;
    var t = GvBackupSettings.DateFormat;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using linq-to-xml to search elements. var doc = XDocument.Load(reader); var ns =
I am trying to load multiple elements with the same name from XML into
I need to load a .xml file from a URL adress into an NSData
I'm using the following code to load a list of objects from XML using
Is it possible to parse & load an xml file with optional elements/attributes using
I am writing a simple XML file parser using LINQ to XML. I want
I am reading data from XML file using from..select linq query, the data is
I am trying to extract values from an XML file using linq to create
I am trying to extract information from an XML file into an object using
I want to read a xml file using Linq. This xml file is composed

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.