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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:42:18+00:00 2026-05-23T09:42:18+00:00

Is there any better way doing this? I have to get both property values.

  • 0

Is there any better way doing this?
I have to get both property values.
XML always has only these 2 properties.

My xml:

<Template name="filename.txt">
  <Property name="recordSeparator">\r\n</Property>
  <Property name="fieldCount">16</Property>
</Template>

Linq:

            var property = from template in xml.Descendants("Template")
                       select new
                                  {
                                      recordDelim = template.Elements("Property").Where(prop => prop.Attribute("name").Value == "recordSeparator")
                                                    .Select(f => new { f.Value }),
                                      fieldCount = template.Elements("Property").Where(prop => prop.Attribute("name").Value == "fieldCount")
                                                    .Select(f => new { f.Value })
                                  };
  • 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-23T09:42:19+00:00Added an answer on May 23, 2026 at 9:42 am

    “Better way” depends on what exactly are you trying to achieve – performance, simplicity, etc.?

    I guess I would create a class that contains what you are trying to get with anonymous classes.

    public class Item {
        public String Separator { get; set; }
        public int FieldCount { get; set; }
    }
    

    and then I would modify the LINQ to:

    var templates = from template in xml.Descendants("Template")
                    let children = template.Elements("Property")
                    select new Item() {
                        Separator = children.First(tag=>tag.Attribute("name").Value == "recordSeparator").Value,
                        FieldCount = Int32.Parse(children.First(tag=>tag.Attribute("name").Value == "fieldCount").Value)
                    };
    
    List<Item> items = templates.ToList();
    

    Note that this will cause NullReference exception in case your Template tag does not contain two Property tags, each with specified attributes.

    Also it will throw an exception in parsing the integer from a FieldCount if it’s not a number.

    Idea:

    If the xml generated is your own, and you can change it’s format, why not do something like:

    <Template>
      <Name>filename.txt</Name>
      <RecordSeparator>\r\n</RecordSeparator>
      <FieldCount>16</FieldCount>
    </Template>
    

    It’s easier to read and to parse, and it’s a little bit shorter.

    In the end, I think this is how I would do it:

    Having this class:

    public class Item 
    {
       public String FileName { get; set; }
       public String Separator { get; set; }
       public int FieldCount { get; set; }
    }
    

    and this private method:

    private Item GetItemFromTemplate(XElement node) 
    {
        return new Item() {
            FileName = node.Element("Name").Value,
            Separator = node.Element("RecordSeparator").Value,
            FieldCount = Int32.Parse(node.Element("FieldCount").Value)
        }; 
    }    
    

    I could do in code:

    XDocument doc = XDocument.Load("myfile.txt");
    
    List<Item> items = (from template in doc.Elements("Template")
                       select GetItemFromTemplate(template)).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any better way to make this query work? I'm looking for a
Is there any better way to get take a string such as (123) 455-2344
is there any way to write this foreach in linq or another better way,
Having the hours and minutes, is there any easier or better way to set
Is there any better alternative for doing string formatting in VC6, with syntax checking
Or is there any better suited 3rd party control for this purpose?
I have lots and lots of data in various structures. Are there any better
Are there any better ways to use constants in a JSP than: <%@ page
Do I use varchar(36) or are there any better ways to do it?
Is there any considerations to determine which is better practice for creating an object

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.