Solved

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

Posted on 2015-01-15
8
348 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What are the simplest ways to create a Merge Sort? 3 29
JSON Deserialize issue 6 26
SSRS Deployment problem 5 66
vb.net convert long time to mm:ss 23 13
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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