Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8191613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:57:25+00:00 2026-06-07T03:57:25+00:00

I’m trying to parse out an complex XML file using LINQ. The files contains

  • 0

I’m trying to parse out an complex XML file using LINQ. The files contains thousands of records, each with hundreds of fields. I need to parse out certain parts of information about each drug and store it in a database.

Edit:
I’m very sorry all, but the originally posted XML was in fact not accurate. I was unaware of the fact that the attributes would alter the process. I’ve updated the question to accurately portray the true nature of XML file.

Here’s a sample of the XML:

<<drugs xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://drugbank.ca" xs:schemaLocation="http://www.drugbank.ca/docs/drugbank.xsd" schemaVersion="1.4">
   <drug>
      <name>foo</name>
      <indication>Some info here</indication>
      <half-life>1 to 3 hours</half-life>
      <protein-binding>90%</protein-binding>
        // hundreds of other elements
      <properties>
         <property>
            <kind>logP/hydrophobicity</kind>
            <value>-0.777</value>
         </property>
         <property>
            <kind>Molecular Weight</kind>
            <value>6963.4250</value>
         </property>
         <property>
            <kind>Molecular Formula</kind>
            <value>C287H440N80O110S6</value>
         </property>
         //dozens of other properties
      </properties>
   </drug>
   // thousands of more drugs
</drugs>

I’m pretty fuzzy on the actual querying, as this is my first time working with LINQ. I’m familiar with SQL, so the concept of complex queries aren’t difficult for me, but I haven’t been able to find any documentation that I can understand that helps with this issue. The query that I have so far is as follows:

XDocument xdoc = XDocument.Load(@"drugbank.xml");

var d = from drugs in xdoc.Descendants("drug")
                        select new
                        {
                            name = drugs.Element("name").Value,
                            indication = drugs.Element("indication").Value,
                            halflife = drugs.Element("half-life").Value,
                            proteinBinding = drugs.Element("protein-binding").Value,
                        };

The first issue is (theoretically) resolved. On to…

The second issue is the fact that I need to extract some of the properties (namely, hydrophobicity, molecular weight, and molecular formula), but where I’m confused is that the property kind and property value are stored in two different XElements. How can I get the property values restricted to the fields that I care about?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:57:26+00:00Added an answer on June 7, 2026 at 3:57 am

    You can do a subquery to get the properties into another property of the outer generic object. If you want them nested:

    XNamespace defaultNS = "http://drugbank.ca";
    
    var d = from drugs in xdoc.Descendants(defaultNS + "drug")
            select new
            {
                name = drugs.Element(defaultNS + "name").Value,
                indication = drugs.Element(defaultNS + "indication").Value,
                halflife = drugs.Element(defaultNS + "half-life").Value,
                proteinBinding = drugs.Element(defaultNS + "protein-binding").Value,
                Properties = (from property in drugs.Element(defaultNS + "properties").Elements(defaultNS + "property")
                              let kind = property.Element(defaultNS + "kind").Value
                              where kind == "logP/hydrophobicity" || kind == "Molecular Weight" || kind == "Molecular Formula"
                              select new { Kind = kind, Value = property.Element(defaultNS + "value").Value })
            };
    

    Or flattened:

    XNamespace defaultNS = "http://drugbank.ca";
    
    var d = from drugs in xdoc.Descendants(defaultNS + "drug")
            let properties = drugs.Element(defaultNS + "properties").Elements(defaultNS + "property")
            select new
            {
                name = drugs.Element(defaultNS + "name").Value,
                indication = drugs.Element(defaultNS + "indication").Value,
                halflife = drugs.Element(defaultNS + "half-life").Value,
                proteinBinding = drugs.Element(defaultNS + "protein-binding").Value,
                hydrophobicity = (from property in properties
                              let kind = property.Element(defaultNS + "kind").Value
                              where kind == "logP/hydrophobicity"
                              select property.Element(defaultNS + "value").Value).FirstOrdefaultNS(),
                molecularWeight = (from property in properties
                              let kind = property.Element(defaultNS + "kind").Value
                              where kind == "Molecular Weight" || kind == "Molecular Formula"
                              select property.Element(defaultNS + "value").Value).FirstOrdefaultNS(),
                molecularFormula = (from property in properties
                              let kind = property.Element(defaultNS + "kind").Value
                              where kind == "Molecular Formula"
                              select property.Element(defaultNS + "value").Value).FirstOrdefaultNS()
            };
    

    Also, a very useful reference that can help you learn about Linq is 101 LINQ Samples.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.