Solved

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

Posted on 2015-01-15
8
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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