Solved

asp.net c#: using XDocument object to obtain value of an element

Posted on 2015-01-15
8
347 Views
Last Modified: 2015-01-24
on a RadGridData_NeedDataSource function I'm obtaining the values from my xml
However, I'm stuck in the value from a non attribute "gcode" (see xml below)

<Response>
      <agency>
            <pccode name="fx02" title="tester" active="true">
                  <pos currency="USD" city="MIA">
                        <gcode>FX</gcode>
                  </pos>
            </pccode>
      </agency>
</Response>


i currently have

var pccode = from item in xDoc.Descendants("pccode")
                               let pos = item.Element("pos")
                               let data = item.Element("data")
                               select new
                               {
                                     Position       = item.ElementsBeforeSelf("pccode").Count(),
                                     name             = (string)item.Attribute("name") != null ? (string)item.Attribute("name"): "",
                                     title             = (string)item.Attribute("title") != null ? (string)item.Attribute("title") : "",
                                     active               = (string)item.Attribute("active") != null ? (string)item.Attribute("active") : "",
                                     currency       = pos != null ? (string)pos.Attribute("currency") : "",
                                     city             = pos != null ? (string)pos.Attribute("city") : "",
                                     data             = data != null && data.HasElements ? (string)data.ToString() : ""
                               };

 return pccode;//IEnumerable


how do i code to get gcode?
0
Comment
Question by:badtz7229
  • 4
  • 4
8 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40552194
Hi badtz7229;

Seeming that your XML document does not have a data node I am assuming that you want the data in the result set to be what is in the gcode node. If that is the case the code below will work.

var pccode = from item in xDoc.Descendants("pccode")
             let pos = item.Element("pos")
             let data = pos.Element("gcode")
             select new
             {
                   Position = item.ElementsBeforeSelf("pccode").Count(),
                   name  = (string)item.Attribute("name") != null ? (string)item.Attribute("name"): "",
                   title  = (string)item.Attribute("title") != null ? (string)item.Attribute("title") : "",
                   active    = (string)item.Attribute("active") != null ? (string)item.Attribute("active") : "",
                   currency = pos != null ? (string)pos.Attribute("currency") : "",
                   city  = pos != null ? (string)pos.Attribute("city") : "",
                   data  = data != null ? (string)data.Value : ""
             };

Open in new window

0
 

Author Comment

by:badtz7229
ID: 40552239
@Fernando Soto
i tried ur solution, and i have data in the event that the node exists, so i can't replace that.

however, i tried it and though i don't get any error the <gcode> element disappears on my
RadGridData_UpdateCommand.
i'm not referencing it on Update. in fact, i only want to retrieve existing value but it disappears from xml
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40552393
@Badtz7229
I am not understanding what you mean by this, "i tried ur solution, and i have data in the event that the node exists, so i can't replace that. ", please  explane.

I do not know how it would disappear from the XML the query is not modifying the XML., to this part of your question, "however, i tried it and though i don't get any error the <gcode> element disappears on my RadGridData_UpdateCommand.
i'm not referencing it on Update. in fact, i only want to retrieve existing value but it disappears from xm".
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:badtz7229
ID: 40552516
what i meant was that you replaced
 let data = item.Element("data")
with
let data = pos.Element("gcode")

and although my example xml does not contain a data node, there is the possibility of it existing in future, so i cannot remove and replace data for gcode.
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 40552720
See if this is what you are looking for, I only added a line in the Select clause of the query.

var pccode = from item in xDoc.Descendants("pccode")
             let pos = item.Element("pos")
             let data =item.Element("data")
             select new
             {
                   Position = item.ElementsBeforeSelf("pccode").Count(),
                   name  = (string)item.Attribute("name") != null ? (string)item.Attribute("name"): "",
                   title  = (string)item.Attribute("title") != null ? (string)item.Attribute("title") : "",
                   active    = (string)item.Attribute("active") != null ? (string)item.Attribute("active") : "",
                   currency = pos != null ? (string)pos.Attribute("currency") : "",
                   city  = pos != null ? (string)pos.Attribute("city") : "",
                   data  = data != null && data.HasElements ? (string)data.ToString() : "",
                   gcode = item.XPathSelectElement("./pos/gcode").Value
             };

Open in new window

0
 

Accepted Solution

by:
badtz7229 earned 0 total points
ID: 40557941
i tried the following and it worked.
var pccode = from item in xDoc.Descendants("pccode")
             let pos = item.Element("pos")
             let data =item.Element("data")
             let gcode = item.Element("gcode")			 
             select new
             {
                   Position = item.ElementsBeforeSelf("pccode").Count(),
                   name  = (string)item.Attribute("name") != null ? (string)item.Attribute("name"): "",
                   title  = (string)item.Attribute("title") != null ? (string)item.Attribute("title") : "",
                   active    = (string)item.Attribute("active") != null ? (string)item.Attribute("active") : "",
                   currency = pos != null ? (string)pos.Attribute("currency") : "",
                   city  = pos != null ? (string)pos.Attribute("city") : "",
                   data  = data != null && data.HasElements ? (string)data.ToString() : "",
                   gcode = gcode != null ? gcode.Value : "TEST",
             }; 

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40557959
There are many ways to do the same thing it is just a matter of coding style, I did it in the select clause and you created an extra local variable to do the same thing.
0
 

Author Closing Comment

by:badtz7229
ID: 40567966
thx so much
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question