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>
有帮助吗?

解决方案

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")
            };
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top