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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:38:13+00:00 2026-06-01T10:38:13+00:00

Something which I really hate is to cast each element or attribute value from

  • 0

Something which I really hate is to cast each element or attribute value from a Xml file.

This momment, I’m creating in hundreds of modules a methods where specifies how to convert an object into a XmlFile. Believe, this is very tired. So I’m thinking in an alternative.

I was investigating about XSD, I’m not sure if this will be my salvation. I’m using Linq to Xml to save and get the values. I mean, my objects are composed like this:

- Foo1 : Foo
   - Range1 : Range
      - X : int
      - Y : int
- ...

As you can see, they have many nodes. Is there another alternative to do this? I mean, strongly types.

  • 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-01T10:38:14+00:00Added an answer on June 1, 2026 at 10:38 am

    You can try these XElement extension methods: http://searisen.com/xmllib/extensions.wiki

    Here is an example of the power of it, given this xml from another post:

    <?xml version="1.0" encoding="utf-8"?>
    <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
      <PatchCreation
        Id="224C316C-5894-4771-BABF-21A3AC1F75FF"
        CleanWorkingFolder="yes"
        OutputPath="patch.pcp"
        WholeFilesOnly="yes">
        <PatchInformation
            Description="Update Patch"
            Comments="Update Patch"
            ShortNames="no"
            Languages="1033"
            Compressed="yes"
            Manufacturer="me"/>
    
        <PatchMetadata
            AllowRemoval="yes"
            Description="Update Patch"
            ManufacturerName="me"
            TargetProductName="Update"
            MoreInfoURL="http://andrewherrick.com/"
            Classification="Update"
            DisplayName="Update Patch"/>
    
        <Family DiskId="5000"
            MediaSrcProp="Sample"
            Name="Update"
            SequenceStart="5000">
          <UpgradeImage SourceFile="c:\new.msi" Id="PatchUpgrade">
            <TargetImage SourceFile="c:\old.msi" Order="2" Id="PatchUpgrade" IgnoreMissingFiles="no" />
          </UpgradeImage>
        </Family>
    
        <PatchSequence PatchFamily="SamplePatchFamily"
            Sequence="1.0.0.0"
            Supersede="yes" />
      </PatchCreation>
    </Wix>
    

    This sets the value of the UpgradeImage tag’s SourceFile Attribute and the TargetImage tag inside the UpgradeImage and its SourceFile.

    XElement wix = XElement.Load(xmlFile1.FullName);
    wix.Set("PatchCreation/Family/UpgradeImage/SourceFile", "upgrade path", true)
       .Set("TargetImage/SourceFile", "target path", true);
    

    You can also get their values in the same fashion (no casts).

    string upgradeSource = wix.Get("PatchCreation/Family/UpgradeImage/SourceFile", string.Empty);
    string targetSource = wix.Get("PatchCreation/Family/UpgradeImage/TargetImage/SourceFile", string.Empty);
    

    Or this can be written as:

    XElement upgradeImage = wix.GetElement("PatchCreation/Family/UpgradeImage");
    string upgradeSource = upgradeImage.Get("SourceFile", string.Empty);
    string targetSource = upgradeImage.Get("TargetImage/SourceFile", string.Empty);
    

    To get a list of integers:

    <root>
     <path>
      <list>
        <value>1</value>
        <value>12</value>
        <value>13</value>
        <value>14</value>
        <value>15</value>
       </list>
      </path>
    </root>
    

    Use the GetEnumerable() method:

    List<int> list = root
        .GetEnumerable("path/list/value", xvalue => xvalue.Get(null, int.MinValue));
        .ToList();
    

    To set a new list of ints:

    var list2 = new int[] { 1, 3, 4, 5, 6, 7, 8, 9, 0 };
    root.SetEnumerable("path/list", list2, a => new XElement("value", a));
    

    Which results in this new xml:

    <root>
      <path>
        <list>
          <value>1</value>
          <value>3</value>
          <value>4</value>
          <value>5</value>
          <value>6</value>
          <value>7</value>
          <value>8</value>
          <value>9</value>
          <value>0</value>
        </list>
      </path>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please can someone clarify something which I should really know by now? If I
I've been using IronPython for a while now, but something which really hampers my
I came across something new which i really found hard to understand.. Here is
today I ran into this problem which really bugs me, as almost the code
Hopefully someone can see something which I've missed out or something, because this is
When answering this question I made some research which really confuses me. I noticed
I'm trying to do something which really ought to be quite easy, but it's
This is going to be a really dumb question, and I almost hate myself
I'm trying to setup a bean factory in spring, something which should be really
I've stumbled upon something which confuses me. Before I though that if you save

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.