Solved

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

Posted on 2015-01-15
8
355 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

726 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