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 7439749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:45:02+00:00 2026-05-29T10:45:02+00:00

How can I access the XML element nameSur with the Value=Company Name(This is what

  • 0

How can I access the XML element nameSur with the Value=Company Name(This is what I want back, just [Company Name]). I keep getting Object reference not set to an instance of an object. in the from children in lv1.Element("IndividualName").Descendants() line. Any advise will be appreciated.

static public string GetAttributeAgencyAction2(string parFileName)
{
    string retVal = string.Empty;
    XNamespace aw = "profile.information.4.0";
    XDocument xmlDoc = null;

    using (StreamReader oReader = new StreamReader(parFileName, Encoding.GetEncoding("ISO-8859-1")))
    {
        xmlDoc = XDocument.Load(oReader);
    }

    var theme = "CORPORATE"; 

    var lv1s = from lv1 in xmlDoc.Descendants(aw + "Profile")
               where lv1.Attribute("profileType").Value.Equals(theme)
               //where lv1.Element("IndividualName").Value == "IndividualName"
               from children in lv1.Element("IndividualName").Descendants()
               select new
               {
                   Header = lv1.Attribute("profileType").Value,
                   elemento = children.Element("IndividualName").Value,
               };

    //Loop through results 
    foreach (var lv1 in lv1s)
    {
        Console.WriteLine(lv1.Header + " "+lv1.elemento);
        retVal = lv1.Header;
    }
    return retVal;
}

Here is my XML

     <Profile xmlns="profile.information.4.0" profileType="CORPORATE" gender="UNKNOWN">
    <creatorCode>DELPHI</creatorCode>
    <createdDate>2012-01-24T19:35:10.000</createdDate>
    <lastUpdaterCode>DELPHI</lastUpdaterCode>
    <lastUpdated>2012-01-30T12:17:01.000</lastUpdated>
    <genericName>Bonafont</genericName>
    <IndividualName>
      <nameSur>Company Name</nameSur>
    </IndividualName>
    <mfResort>5924</mfResort>
    <mfResortProfileID>1249468</mfResortProfileID>
    <mfContactLast>Ernert</mfContactLast>
    <mfContactFirst>Ramillo</mfContactFirst>
    <mfAllowMail>NO</mfAllowMail>
    <mfAllowEMail>NO</mfAllowEMail>
    <mfGuestPriv>NO</mfGuestPriv>
    </Profile>
  • 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-05-29T10:45:03+00:00Added an answer on May 29, 2026 at 10:45 am

    Just use the namespace you already defined:

    var lv1s = from lv1 in xmlDoc.Descendants(aw + "Profile")
                where lv1.Attribute("profileType").Value.Equals(theme)
                //where lv1.Element("IndividualName").Value == "IndividualName"
                from child in lv1.Element(aw + "IndividualName").Descendants()
                select new
                {
                    Header = lv1.Attribute("profileType").Value,
                    elemento = child.Value,
                };
    

    Edit:

    Assuming there is only a single IndividualName element for each Profile I would restructure your query like this to check for null values:

    var lv1s = from lv1 in xmlDoc.Descendants(aw + "Profile")
               where lv1.Attribute("profileType").Value.Equals(theme) && lv1.Element(aw + "IndividualName") != null
                select new 
                {
                    Header = lv1.Attribute("profileType").Value,
                    elemento = (string)lv1.Element(aw + "IndividualName").Element(aw+"nameSur")
                };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I parse an XML document like this: <feed> <item> <element-name>Element value</element-name> </item>
How can I access this element: <input type=submit value=Save as XML onclick=some code goes
I know that I can access an XML Element with tag name 'page' like
I am trying to access the text value of a xml element. I am
How can I access current xml document uri in XSLT?
As far as I know I can access the web.xml <context-param> s by making
I have a .xml file in my App_Data folder, I can access it fine
Given the XML/RDF example below taken from the W3C website, how can I access
there is a xml document that i'm parsing to array.. how can i access
So I want to access the child element in a structure that looks like

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.