1
<?xml version="1.0" encoding="utf-8" ?>
<SiteMap>
    <Site FacID="Default">
      <Rec0>
            <NumGeos>
                <RMin>0</RMin>
                <YMin>1</YMin>
                <YMax>4</YMax>
                <RMax>5</RMax>
            </NumGeos>
            <GeoAvg>
                <RMin>10</RMin>
                <YMin>20</YMin>
                <YMax>60</YMax>
                <RMax>70</RMax>
            </GeoAvg>
            <L1AGC>
                <RMin>2500</RMin>
                <YMin>2600</YMin>
                <YMax>3400</YMax>
                <RMax>3500</RMax>
            </L1AGC>
            <NumL1s>
                <RMin>0</RMin>
                <YMin>1</YMin>
                <YMax>14</YMax>
                <RMax>15</RMax>
            </NumL1s>
            <L1Avg>
                <RMin>10</RMin>
                <YMin>20</YMin>
                <YMax>60</YMax>
                <RMax>70</RMax>
            </L1Avg>
            <L2AGC>
                <RMin>2500</RMin>
                <YMin>2600</YMin>
                <YMax>3400</YMax>
                <RMax>3500</RMax>
            </L2AGC>
            <NumL2s>
                <RMin>0</RMin>
                <YMin>1</YMin>
                <YMax>14</YMax>
                <RMax>15</RMax>
            </NumL2s>
            <L2Avg>
                <RMin>10</RMin>
                <YMin>20</YMin>
                <YMax>60</YMax>
                <RMax>70</RMax>
            </L2Avg>
        </Rec0>
    </Site>
    <Site FacID="BET">
        <Rec1>
            <NumGeos>
                <RMin>0</RMin>
                <YMin>1</YMin>
                <YMax>4</YMax>
                <RMax>5</RMax>
            </NumGeos>
            <GeoAvg>
                <RMin>10</RMin>
                <YMin>20</YMin>
                <YMax>60</YMax>
                <RMax>70</RMax>
            </GeoAvg>
            <L1AGC>
                <RMin>2500</RMin>
                <YMin>2600</YMin>
                <YMax>3400</YMax>
                <RMax>3500</RMax>
            </L1AGC>
            <NumL1s>
                <RMin>0</RMin>
                <YMin>1</YMin>
                <YMax>14</YMax>
                <RMax>15</RMax>
            </NumL1s>
            <L1Avg>
                <RMin>10</RMin>
                <YMin>20</YMin>
                <YMax>60</YMax>
                <RMax>70</RMax>
            </L1Avg>
            <L2AGC>
                <RMin>2500</RMin>
                <YMin>2600</YMin>
                <YMax>3400</YMax>
                <RMax>3500</RMax>
            </L2AGC>
            <NumL2s>
                <RMin>0</RMin>
                <YMin>1</YMin>
                <YMax>14</YMax>
                <RMax>15</RMax>
            </NumL2s>
            <L2Avg>
                <RMin>10</RMin>
                <YMin>20</YMin>
                <YMax>60</YMax>
                <RMax>70</RMax>
            </L2Avg>
        </Rec1>
    </Site>
</SiteMap>

I'm trying to store the values listed in the xml file above so that I can refer to them like this:

int x = (Site["BET"].Rec1.GeoAvg.RMin == null 
      ? Site["Default"].Rec0.GeoAvg.RMin 
      : Site["BET"].Rec1.GeoAvg.RMin)

Or at least something similar to that. Anyone know an easy (and fast) way to do that? Thank you so much for your help!!!

H H
  • 263,252
  • 30
  • 330
  • 514
Kasey Krehbiel
  • 423
  • 5
  • 12
  • 2
    Have you tried Deserializing to objects as in http://stackoverflow.com/questions/226599/deserializing-xml-to-objects-in-c-sharp ? This is what I would do in this case – Bob2Chiv Mar 19 '12 at 13:48

2 Answers2

1

The main part would be a ToDicionary(), something like

var sites = doc.Descendants("Site").ToDictionary(
        e => e.Attribute("FacID").Value, 
        e => new Site { Rec1 = new Rec1 { ... } } );
H H
  • 263,252
  • 30
  • 330
  • 514
1

OK, so instead of trying to use Linq to do this, I used this:

Generate C# class from XML

Then I just referred to the elements in the new class using dot notation.

Community
  • 1
  • 1
Kasey Krehbiel
  • 423
  • 5
  • 12