4

I'm having such a brain fail. I have selected the elements and the values I need, I am just struggling with making this return an anonymous type:

Here is the XML:

<r25:space xl:href="space.xml?space_id=244" id="BRJDMjQ0" crc="00000023" status="est">
  <r25:space_id>244</r25:space_id>
  <r25:space_name>BEC*103</r25:space_name>
  <r25:formal_name>Branson Education Center 103</r25:formal_name>
  <r25:partition_id />
  <r25:partition_name />
  <r25:favorite>F</r25:favorite>
  <r25:max_capacity>24</r25:max_capacity>
  <r25:fill_ratio />
  <r25:last_mod_user>kleierd</r25:last_mod_user>
  <r25:last_mod_dt>2009-11-19T15:35:33</r25:last_mod_dt>
</r25:space>

I need the value of "space_id" and "space_name" which I can get with this:

var ids = from id in xml.Descendants()
                  where id.Name.LocalName == "space_id" || id.Name.LocalName == "space_name"
                  select (string)id.Value;

But I would really like it like this:

var ids = from id in xml.Descendants()
                  where id.Name.LocalName == "space_id" || id.Name.LocalName == "space_name"
                  select new
                  { 
                      theId = //The id val would go here,
                      theName = //The name goes here
                  };

1 Answer 1

3
var ids = from id in xml.Descendants()
          where id.Name.LocalName == "space_id" || id.Name.LocalName == "space_name"
          select new
          { 
                theId = id.Value,
                theName = id.Name.LocalName
          };

ids will hold values:

theId     theName 
244       pace_id 
BEC*103   space_name 

this will select the same nodes:

XNamespace r25 = "yourSchemaDefinition";

var ids = xml.Descendants(r25 + "space_id")
             .Union(xml.Descendants(r25 + "space_name"))
             .Select(id => new
                           { 
                              theId = id.Value,
                              theName = id.Name.LocalName
                           });

NOTE: I've added r25 schema definition to your xml root node

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.