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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:09:10+00:00 2026-05-28T14:09:10+00:00

I have a class with some data members (int, string, bool, Point). These data

  • 0

I have a class with some data members (int, string, bool, Point).
These data members have public properties {get; set;}.

In my code I create an instance of this class using the default constructor.
So my Object’s data members have values of 0, string.Empty, false, {0,0} respectively.

How do I initialize these values of the field data
from an XML file, using LINQ to XML
without creating a new instance of the object.

I just want to read in the XML, and set the values of my Object.

  • 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-28T14:09:11+00:00Added an answer on May 28, 2026 at 2:09 pm

    It’s pretty easy using Linq to XML. Here’s a contrived example, it gets a little tougher if the document is longer and you need to validate the input, but the basic idea is the same

    var root = XDocument.Parse(@"<data>
    <num>3</num>
    <str>string</str>
    <boo>true</boo>
    <point x=""3"" y=""5"" />
    </data>").Root;
    
    public class MyObject()
    {
      public class Point 
      {
        int X {get;set;}
        int Y  {get;set;}
      }
    
      public MyObject() {
       Point = new Point();
      }
      public int Num {get;set;}
      public string Str {get;set;}
      public bool Boo {get;set;}
      public Point Point {get; private set;}
    }
    
    var myObj = root.Select(new MyObject
    {
        Num = int.Parse(root.Element("num").Value),
        Str = root.Element("str").Value,
        Boo = bool.Parse(root.Element("boo").Value),
        Point.X = int.Parse(root.Element("point").Attribute("x").Value),
        Point.Y = int.Parse(root.Element("point").Attribute("y").Value)
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class in system-C with some data members as such: long double
Suppose I have a class that processes some data: class SomeClass { public: void
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
I have a class with number of private data members (some of them static),
I need to have some data members get some values when I create an
I have the following business object: public class MyObject { // Some public properties
I have the following scenario: public class SomeClass { // Have some other data
I have a action class for saving some data to a database.In action class
I have a class that contains data from some model. This class has metadata
I have a class 'Data' that uses a getter to access some array. If

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.