Click here to Skip to main content
14,691,000 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I have a problem statement where I want to retrieve the child nodes of the Particular ID
using Linq to XML I am able to get the whole node data along with the other data, how can I extract the child node from Environment.

Here's my XML file structure :
<ProjectEnvironments>
<ProjectEnvironment>
    <ProjectId>22</ProjectId>
    <ProjectName>Proj22</ProjectName>
    <GroupName>Group</GroupName>
    <ProjectApIUrl>http://localhost.dell.com/PremierToolsUX#/Gvt/API</ProjectApIUrl>
    <ProjectUrl>http://localhost.dell.com/PremierToolsUX#/Gvt</ProjectUrl>
    <Environments>
      <Environment>
        <EnvId>22</EnvId>
        <EnvName>Proj22</EnvName>
      </Environment>
      <Environment>
        <EnvId>33</EnvId>
        <EnvName>Proj33</EnvName>
      </Environment>
    </Environments>
  </ProjectEnvironment>
</ProjectEnvironments>


using the below code I am able to get the whole XML data based on the ProjectId, how can I get the data of Environments i.e. all EnvId and EnvName for the same node of ProjectId ?

XDocument _XDocument = XDocument.Load(@"C:\test.xml");
var items = from item in _XDocument.Elements("ProjectEnvironments").Elements("ProjectEnvironment")
                                where item != null && (item.Element("ProjectId").Value == projectId)
                                select item;


to get the Environments I tried the below code which is giving object reference error

var Envs = from itemz in _XDocument.Descendants("ProjectEnvironments").Descendants("ProjectEnvironment").Descendants("Environments").Descendants("Environment")
           where item != null && (item.Element("EnvId").Value == _environments[0].EnvId)
           select item;


Could anyone help me in getting this solved?
Thanks in Advance

What I have tried:

Here's my XML file structure : 
<pre>
<ProjectEnvironments>
<ProjectEnvironment>
    <ProjectId>22</ProjectId>
    <ProjectName>Proj22</ProjectName>
    <GroupName>Group</GroupName>
    <ProjectApIUrl>http://localhost.dell.com/PremierToolsUX#/Gvt/API</ProjectApIUrl>
    <ProjectUrl>http://localhost.dell.com/PremierToolsUX#/Gvt</ProjectUrl>
    <Environments>
      <Environment>
        <EnvId>22</EnvId>
        <EnvName>Proj22</EnvName>
      </Environment>
      <Environment>
        <EnvId>33</EnvId>
        <EnvName>Proj33</EnvName>
      </Environment>
    </Environments>
  </ProjectEnvironment>
</ProjectEnvironments>


using the below code I am able to get the whole XML data based on the ProjectId, how can I get the data of Environments i.e. all EnvId and EnvName for the same node of ProjectId ?

XDocument _XDocument = XDocument.Load(@"C:\test.xml");
var items = from item in _XDocument.Elements("ProjectEnvironments").Elements("ProjectEnvironment")
                                where item != null && (item.Element("ProjectId").Value == projectId)
                                select item;


to get the Environments I tried the below code which is giving object reference error

var Envs = from itemz in _XDocument.Descendants("ProjectEnvironments").Descendants("ProjectEnvironment").Descendants("Environments").Descendants("Environment")
           where item != null && (item.Element("EnvId").Value == _environments[0].EnvId)
           select item;


Could anyone help me in getting this solved?
Posted
Updated 27-Dec-19 0:42am
v2

1 solution

Try this:
XDocument xdoc = XDocument.Load(@"C:\test.xml");
var envs = from lv1 in xdoc.Descendants("Environment") where lv1.Element("EnvId").Value == "22" select new { lv1 };

foreach (var env in envs)
{
    Debug.Print("val = " + env.lv1.Element("EnvName").Value + Environment.NewLine);
}
   
v2
Comments
Nischal Bhatt 27-Dec-19 10:16am
   
Thanks RickZeeland,

This will give me the data for single EnvId, how can I change this query so that It returns the list of EnvId and EnvName? I'm stuck at this scenario
RickZeeland 27-Dec-19 11:03am
   
Then you can use:
var envs = from lv1 in xdoc.Descendants("Environment") select new { lv1 };

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900