Solved

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

Posted on 2015-01-15
8
328 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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
@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 62

Expert Comment

by:Fernando Soto
Comment Utility
@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
 

Author Comment

by:badtz7229
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
Comment Utility
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
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
thx so much
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now